-
Notifications
You must be signed in to change notification settings - Fork 30
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
Conversation
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>
There was a problem hiding this 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: link to https://tailscale.com/api
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
// 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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, "", " ") |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
// 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 { |
There was a problem hiding this comment.
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:
if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusCreated { | |
if res.StatusCode >= http.StatusBadRequest { |
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