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

Adding uhttp client #7

Merged
merged 5 commits into from
Apr 17, 2024
Merged

Adding uhttp client #7

merged 5 commits into from
Apr 17, 2024

Conversation

mchavez
Copy link
Collaborator

@mchavez mchavez commented Apr 11, 2024

  • Add uhttp client

@mchavez mchavez marked this pull request as ready for review April 12, 2024 15:07
func (v *VGSClient) GetOrganizations(ctx context.Context) ([]Organization, error) {
v.InitHttpClient()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, v.endpoint+"/organizations", nil)
url, _ := url.JoinPath(v.endpoint, "/organizations")
Copy link
Contributor

Choose a reason for hiding this comment

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

This should check the error. Also the variable name "url" shadows the url library, so you might want to change the name.

v.InitHttpClient()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, v.endpoint+"/organizations", nil)
url, _ := url.JoinPath(v.endpoint, "/organizations")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

uhttp has a helper function for making requests, so this can be:

v.httpClient.NewRequest(ctx,
    http.MethodGet,
    orgUrl,
    WithHeader("Authorization", "Bearer "+v.GetToken()),
    WithAcceptJSONHeader(),
    WithJSONResponse(&organizationsAPIData),
)

...and then you don't need to add the headers separately, nor do you need to read and unmarshal the response body below. All of the requests in this file can get this treatment.

Also I'm pretty sure we don't want the content-type header here. This is a get, so we're not sending a body to the server.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yeah.. It looks weirds, but according to docs it is necessary in some cases.
https://www.verygoodsecurity.com/docs/accounts/api/#tag/users/paths/~1organizations~1{organizationId}~1members/get

@ggreer ggreer merged commit e585f5c into main Apr 17, 2024
3 checks passed
@ggreer ggreer deleted the mchavez-baton-vgs branch April 17, 2024 22:02
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