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

v2: added skeleton of v2 client #89

Merged
merged 1 commit into from
Aug 2, 2024
Merged

v2: added skeleton of v2 client #89

merged 1 commit into from
Aug 2, 2024

Conversation

oxtoacart
Copy link
Contributor

This implementation will allow us to organize the various portions of the client into separate resources.

We will add these resources one by one.

Updates tailscale/corp#21867

This implementation will allow us to organize the various portions of the client into separate resources.

We will add these resources one by one.

Updates tailscale/corp#21867

Signed-off-by: Percy Wegmann <percy@tailscale.com>
Copy link
Member

@mpminardi mpminardi left a comment

Choose a reason for hiding this comment

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

LGTM overall. Left some nitpicky comments / things we can fix in future PRs.

@@ -0,0 +1,291 @@
// Package tailscale contains a basic implementation of a client for the Tailscale HTTP api. Documentation is here:
// /~https://github.com/tailscale/tailscale/blob/main/api.md
Copy link
Member

Choose a reason for hiding this comment

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

const defaultHttpClientTimeout = time.Minute
const defaultUserAgent = "tailscale-client-go"

// NewClient returns a new instance of the Client type that will perform operations against a chosen tailnet and will
Copy link
Member

Choose a reason for hiding this comment

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

nit:

Suggested change
// NewClient returns a new instance of the Client type that will perform operations against a chosen tailnet and will
// init returns a new instance of the Client type that will perform operations against a chosen tailnet and will

Tailnet string

// http is the http client to use for requests to the API server. If specified, this supercedes the above configuration.
http *http.Client
Copy link
Member

Choose a reason for hiding this comment

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

Nit: might be nice to expose this so end users can swap out the client if desired.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that's come up before. We'll need to think about how this interacts with functions like UseOAuth() that blast away the existing client.

case []byte:
bodyBytes = body
default:
bodyBytes, err = json.MarshalIndent(rof.body, "", " ")
Copy link
Member

Choose a reason for hiding this comment

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

Nit: this is in the existing v1 but do we just want to do a json.Marshal here and not indent? Not sure if the indent gains us anything and presumably its making the requests marginally larger

req.Header.Set("Content-Type", rof.contentType)
}

// c.apiKey will not be set on the client was configured with WithOAuthClientCredentials()
Copy link
Member

Choose a reason for hiding this comment

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

Nit:

Suggested change
// c.apiKey will not be set on the client was configured with WithOAuthClientCredentials()
// c.apiKey will not be set on the client was configured with UseOAuth()

return json.Unmarshal(body, out)
}

if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusCreated {
Copy link
Member

Choose a reason for hiding this comment

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

Don't have to do this in this PR but leaving a note that we'll want to adjust this logic since it's detecting valid 2XX / 3XX responses as errors (e.g., think we return 202 from some endpoints now, maybe /webhook/test?).

Potentially:

Suggested change
if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusCreated {
if res.StatusCode >= http.StatusBadRequest {

@oxtoacart oxtoacart merged commit d273e41 into main Aug 2, 2024
2 checks passed
@oxtoacart oxtoacart deleted the percy/add_v2_skeleton branch August 2, 2024 20:27
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