Skip to content

Improve api code quality - #49

Closed
somechris wants to merge 4 commits into
prooflinedev:mainfrom
somechris:code-quality-api
Closed

somechris wants to merge 4 commits into
prooflinedev:mainfrom
somechris:code-quality-api

Conversation

@somechris

Copy link
Copy Markdown

No description provided.

Several places used the same

```
if reply was not successful {
  err out
}
decode reply
```

pattern. We collect it in `try_decode` to dedupe the success checking and
a more compact code base.
Having static media types as static strings is prone to typos. So we
convert them to constants, to make typos visible at compile time.
Deduping content type checking gives similar experience across all
uses. It for example removes differences for responses having two
`Content-Type` headers.
This makes it more obvious for users where things went wrong.
@somechris somechris changed the title Code quality api Improve api code quality Nov 8, 2026
Comment thread src/api.rs
.send(self.request(Method::POST, url).json(request))
.await?;
if matches!(reply.status, StatusCode::OK | StatusCode::CREATED) {
return self.decode(&reply.body);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not switching to try_decode here, as the guard is not a plain is_success.

If that's a development relict, it could of course switch to try_decode too.

Comment thread src/api.rs
async fn get(&self, url: Url) -> Result<serde_json::Value, ApiError> {
let reply = self.read(url, "application/json").await?;
let reply = self.read(url, MEDIA_TYPE_JSON).await?;
self.decode(&reply.body)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No try_decode here, as a successful read asserts that the request succeeded.

Can be treated as micro-optimization and switched to try_decode nonetheless.

@somechris somechris closed this by deleting the head repository Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant