Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: always use json for http requests/responses #92

Merged
merged 11 commits into from
Sep 24, 2024
Merged

Conversation

gruyaume
Copy link
Collaborator

@gruyaume gruyaume commented Sep 23, 2024

Description

Here we ensure that every API calls takes json objects as request parameters and return json objects in its responses.

Standardized responses

Each return object now has the following format

{
  "result": "...",
  "error": "...",
}

Rationale

Request parameters

HTTP Request Parameters were inconsistents. Some had to be json formatted, some didn't. For example creating a user required a the following body:

{"username":"admin", "password": "Admin1234"}

And creating a certificate required the bare certificate:

-----BEGIN CERTIFICATE REQUEST-----
MIICsTCCAZkCAQAwbDELMAkGA1UEBhMCQ0ExFDASBgNVBAgMC05vdmEgU2NvdGlh
MRAwDgYDVQQHDAdIYWxpZmF4MSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0
...

Related quality improvements

Doing so we address many of related Ben's feedback. Namely:

  • Centralize HTTP responses into writeError and writeJSON functions
  • Create separate structs for DB and API objects - Never return db structs
  • Remove tests that wrote to the developer's home directory
  • Rename HTTP handler's files so that they match their endpoint
    • handlers_health -> handlers_status (endpoint: /status)
    • handlers_users -> handlers_accounts (endpoint: /api/v1/accounts)
  • Move test certificates to files under testdata instead of having long strings in test files
  • Use numUsers to get the number of users instead of listing them and computing the list length

This is a breaking change

This is a breaking change, anything that depended on Notary's API (here luckily only the charm) will need to be adapted.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have added tests that validate the behaviour of the software
  • I validated that new and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@gruyaume gruyaume requested a review from a team as a code owner September 23, 2024 11:58
@gruyaume gruyaume marked this pull request as draft September 23, 2024 11:59
@gruyaume gruyaume marked this pull request as ready for review September 23, 2024 20:32
@gruyaume gruyaume changed the title feat: always return json responses feat: always use json for http requests/responses Sep 23, 2024
Copy link
Contributor

@kayra1 kayra1 left a comment

Choose a reason for hiding this comment

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

Thanks

internal/server/authorization_test.go Outdated Show resolved Hide resolved
internal/server/handlers_certificate_requests_test.go Outdated Show resolved Hide resolved
ui/src/app/queries.ts Outdated Show resolved Hide resolved
@gruyaume gruyaume merged commit bc6b6df into main Sep 24, 2024
13 checks passed
@gruyaume gruyaume deleted the dev-jsonresponse branch September 24, 2024 12:56
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.

2 participants