-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add some comments in snapshot client
- Loading branch information
Showing
2 changed files
with
13 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import ( | |
"github.com/Khan/genqlient/graphql" | ||
) | ||
|
||
// Command to generate the client code in operation.go. | ||
//go:generate go run --mod=mod github.com/Khan/[email protected] | ||
|
||
const ( | ||
|
@@ -18,31 +19,41 @@ const ( | |
ProposalStateClosed = "closed" | ||
) | ||
|
||
// Use _ to make sure we implement the interface. | ||
var _ graphql.Doer = (*client)(nil) | ||
|
||
// A graphql.Doer that adds the X-API-KEY header to requests. | ||
type client struct { | ||
httpClient *http.Client | ||
apiKey string | ||
} | ||
|
||
// Do implements [graphql.Doer]. | ||
func (c *client) Do(request *http.Request) (*http.Response, error) { | ||
// add the X-API-KEY header if it's set. | ||
if c.apiKey != "" { | ||
request.Header.Set("X-API-KEY", c.apiKey) | ||
} | ||
|
||
// do the request | ||
return c.httpClient.Do(request) | ||
} | ||
|
||
// NewClient a function that can be used to configure a [client] with options. | ||
// Options are applied in the order they are passed to the function | ||
func NewClient(endpoint string, options ...Option) (graphql.Client, error) { | ||
// build a default client | ||
instance := client{ | ||
httpClient: http.DefaultClient, | ||
} | ||
|
||
// Apply options. | ||
for _, option := range options { | ||
if err := option(&instance); err != nil { | ||
return nil, fmt.Errorf("apply option: %w", err) | ||
} | ||
} | ||
|
||
// Return the client | ||
return graphql.NewClient(endpoint, &instance), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters