-
Notifications
You must be signed in to change notification settings - Fork 0
User v2 #4
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
base: main
Are you sure you want to change the base?
User v2 #4
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,6 +9,8 @@ import ( | |||||||||||||||||||||||||||
| "io" | ||||||||||||||||||||||||||||
| "log" | ||||||||||||||||||||||||||||
| "net/http" | ||||||||||||||||||||||||||||
| "net/url" | ||||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const ( | ||||||||||||||||||||||||||||
|
|
@@ -27,10 +29,24 @@ var ( | |||||||||||||||||||||||||||
| ErrBadRequest = errors.New("error: bad request") | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Error API Response. Contains the error message as well as the type of error | ||||||||||||||||||||||||||||
| type Error struct { | ||||||||||||||||||||||||||||
| Message string `json:"message"` | ||||||||||||||||||||||||||||
| Type string `json:"type"` | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| //URLBuilder is the interface for building URLs | ||||||||||||||||||||||||||||
| //go:generate mockery --name URLBuilder | ||||||||||||||||||||||||||||
| type URLBuilder interface { | ||||||||||||||||||||||||||||
| SearchDocumentURL(vaultID string) string | ||||||||||||||||||||||||||||
| GetUserURL(userId []string) string | ||||||||||||||||||||||||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not 100% sure how I feel about this pattern. The client needs to know so much about other packages. I wonder if it would be easier if we just mocked the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO this is fine. The client only needs to know about the URLBuilder interface, not its implementation. The nice thing about this URL builders is we can mock the response using the HTTP test and actually use the http client on tests go-truevault/document/document_test.go Lines 66 to 78 in 7bde5dc
I guess the reason why it feels like the client is doing so much is that this interface is in client.go. We can move this to a separate file if that helps |
||||||||||||||||||||||||||||
| CreateUserURL() string | ||||||||||||||||||||||||||||
| ListUserURL(queryParams url.Values) string | ||||||||||||||||||||||||||||
| UpdateUserURL(userId string) string | ||||||||||||||||||||||||||||
| UpdateUserPasswordURL(userId string) string | ||||||||||||||||||||||||||||
| DeleteUserURL(userId string) string | ||||||||||||||||||||||||||||
| CreateAccessTokenURL(userId string) string | ||||||||||||||||||||||||||||
| CreateApiKeyURL(userId string) string | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| //DefaultURLBuilder implements URLBuilder interface | ||||||||||||||||||||||||||||
|
|
@@ -41,6 +57,50 @@ func (t *DefaultURLBuilder) SearchDocumentURL(vaultID string) string { | |||||||||||||||||||||||||||
| return fmt.Sprintf("https://api.truevault.com/v1/vaults/%s/search", vaultID) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // GetUserURL returns the TrueVault `Get User` route for the specified user id(s) | ||||||||||||||||||||||||||||
| func (t *DefaultURLBuilder) GetUserURL(userId []string) string { | ||||||||||||||||||||||||||||
| return fmt.Sprintf("https://api.truevault.com/v2/users/"+strings.Join(userId, ",")) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // CreateUserURL returns the TrueVault `Create User` route | ||||||||||||||||||||||||||||
| func (t *DefaultURLBuilder) CreateUserURL() string { | ||||||||||||||||||||||||||||
| return "https://api.truevault.com/v1/users" | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // UpdateUserURL returns the TrueVault `Update User` route | ||||||||||||||||||||||||||||
| func (t *DefaultURLBuilder) UpdateUserURL(userId string) string { | ||||||||||||||||||||||||||||
| return "https://api.truevault.com/v1/users/" + userId | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // UpdateUserPasswordURL returns the TrueVault `Update User Password` route | ||||||||||||||||||||||||||||
| func (t *DefaultURLBuilder) UpdateUserPasswordURL(userId string) string { | ||||||||||||||||||||||||||||
| return "https://api.truevault.com/v1/users/" + userId | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // DeleteUserURL returns the TrueVault `Delete User` route | ||||||||||||||||||||||||||||
| func (t *DefaultURLBuilder) DeleteUserURL(userId string) string { | ||||||||||||||||||||||||||||
| return "https://api.truevault.com/v1/users/" + userId | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // CreateAccessTokenURL returns the TrueVault `Create Access Token` route | ||||||||||||||||||||||||||||
| func (t *DefaultURLBuilder) CreateAccessTokenURL(userId string) string { | ||||||||||||||||||||||||||||
| return "https://api.truevault.com/v1/users/" + userId | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // CreateApiKeyURL returns the TrueVault `Create API Key` route | ||||||||||||||||||||||||||||
| func (t *DefaultURLBuilder) CreateApiKeyURL(userId string) string { | ||||||||||||||||||||||||||||
| return "https://api.truevault.com/v1/users/" + userId + "/api_key" | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // ListUserURL returns the TrueVault `List User` route | ||||||||||||||||||||||||||||
| func (t *DefaultURLBuilder) ListUserURL(queryParams url.Values) string { | ||||||||||||||||||||||||||||
| params := "?" | ||||||||||||||||||||||||||||
| if queryParams != nil { | ||||||||||||||||||||||||||||
| params += queryParams.Encode() | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| return fmt.Sprintf("https://api.truevault.com/v2/users/?%s", params) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| //Client contains the base http requirements to make requests to TrueVault | ||||||||||||||||||||||||||||
| type Client struct { | ||||||||||||||||||||||||||||
| URLBuilder URLBuilder | ||||||||||||||||||||||||||||
|
|
@@ -53,7 +113,7 @@ func New(h *http.Client, ub URLBuilder, accessTokenOrKey string) Client { | |||||||||||||||||||||||||||
| return Client{ | ||||||||||||||||||||||||||||
| httpClient: h, | ||||||||||||||||||||||||||||
| URLBuilder: ub, | ||||||||||||||||||||||||||||
| authorization: buildAuthorizationValue(accessTokenOrKey), | ||||||||||||||||||||||||||||
| authorization: "Basic " + base64.StdEncoding.EncodeToString([]byte(accessTokenOrKey+":")), | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
@@ -67,10 +127,6 @@ func (c *Client) WithNewAccessTokenOrKey(accessTokenOrKey string) Client { | |||||||||||||||||||||||||||
| return New(c.httpClient, c.URLBuilder, accessTokenOrKey) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| func buildAuthorizationValue(key string) string { | ||||||||||||||||||||||||||||
| return "Basic " + base64.StdEncoding.EncodeToString([]byte(key+":")) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // NewRequest builds an http.Request that contains the Authorization and Content-Type header | ||||||||||||||||||||||||||||
| func (c *Client) NewRequest(ctx context.Context, method, path, contentType string, body io.Reader) (*http.Request, error) { | ||||||||||||||||||||||||||||
| req, err := http.NewRequestWithContext(ctx, method, path, body) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.