Skip to content

Commit

Permalink
Merge pull request #289 from PagerDuty/ctx_extension_schema
Browse files Browse the repository at this point in the history
Update extension_schema.go to accept a context.Context
  • Loading branch information
Scott McAllister authored Mar 29, 2021
2 parents 680c2f0 + 5c1eb08 commit 1b67a25
Showing 1 changed file with 30 additions and 4 deletions.
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"`
}

// 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

0 comments on commit 1b67a25

Please sign in to comment.