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

Update extension_schema.go to accept a context.Context #289

Merged
merged 1 commit into from
Mar 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 30 additions & 4 deletions extension_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/google/go-querystring/query"
)

// ExtensionSchema represnts the object presented by the API for each extension
// schema.
type ExtensionSchema struct {
APIObject
IconURL string `json:"icon_url"`
Expand All @@ -20,34 +22,57 @@ type ExtensionSchema struct {
URL string `json:"url"`
}

// ListExtensionSchemaResponse is the object presented in response to the
// request to list all extension schemas.
type ListExtensionSchemaResponse struct {
APIListObject
ExtensionSchemas []ExtensionSchema `json:"extension_schemas"`
}

// ListExtensionSchemaOptions are the options to send with the
// ListExtensionSchema reques(s).
type ListExtensionSchemaOptions struct {
APIListObject
Query string `url:"query,omitempty"`
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@stmcallister I don't see this field documented here. Does this look like a candidate for removal as part of 1.5, or should this remain?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Although, now that I look more maybe it's implicitly supported so that you can get the total count as part of the paginated response?

Copy link
Contributor

Choose a reason for hiding this comment

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

I've reached out to our engineering team to see if we can document the query query string parameter.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@stmcallister since this something we can (and should) solve outside of this PR, are we good to merge this one to call the context.Context addition done?

}

// ListExtensionSchemas lists all of the extension schemas. Each schema
// represents a specific type of outbound extension. It's recommended to use
// ListExtensionSchemasWithContext instead.
func (c *Client) ListExtensionSchemas(o ListExtensionSchemaOptions) (*ListExtensionSchemaResponse, error) {
return c.ListExtensionSchemasWithContext(context.Background(), o)
}

// ListExtensionSchemasWithContext lists all of the extension schemas. Each
// schema represents a specific type of outbound extension.
func (c *Client) ListExtensionSchemasWithContext(ctx context.Context, o ListExtensionSchemaOptions) (*ListExtensionSchemaResponse, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}

resp, err := c.get(context.TODO(), "/extension_schemas?"+v.Encode())
resp, err := c.get(ctx, "/extension_schemas?"+v.Encode())
if err != nil {
return nil, err
}

var result ListExtensionSchemaResponse
if err := c.decodeJSON(resp, &result); err != nil {
return nil, err
}

return &result, c.decodeJSON(resp, &result)
return &result, nil
}

// GetExtensionSchema gets a single extension schema. It's recommended to use
// GetExtensionSchemaWithContext instead.
func (c *Client) GetExtensionSchema(id string) (*ExtensionSchema, error) {
resp, err := c.get(context.TODO(), "/extension_schemas/"+id)
return c.GetExtensionSchemaWithContext(context.Background(), id)
}

// GetExtensionSchemaWithContext gets a single extension schema.
func (c *Client) GetExtensionSchemaWithContext(ctx context.Context, id string) (*ExtensionSchema, error) {
resp, err := c.get(ctx, "/extension_schemas/"+id)
return getExtensionSchemaFromResponse(c, resp, err)
}

Expand All @@ -61,7 +86,8 @@ func getExtensionSchemaFromResponse(c *Client, resp *http.Response, err error) (
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}

rootNode := "extension_schema"
const rootNode = "extension_schema"

t, nodeOK := target[rootNode]
if !nodeOK {
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
Expand Down