Skip to content

Commit

Permalink
feat(go): Add context support to all requests (#1527)
Browse files Browse the repository at this point in the history
## 🧭 What and Why

🎟 JIRA Ticket: https://algolia.atlassian.net/browse/APIC-696

We need to provide a way to accept users' context while respecting general conventions in Go. 

### Changes included:

- Wrapper functions with a `WithContext` suffix added for all API methods to comply with the general DX standards. 
Ex:
```

// CreateAuthentication wraps CreateAuthenticationWithContext using context.Background.
func (c *APIClient) CreateAuthentication(r ApiCreateAuthenticationRequest, opts ...Option) (*AuthenticationCreateResponse, error) {
	return c.CreateAuthenticationWithContext(context.Background(), r, opts...)
}

// @return AuthenticationCreateResponse
func (c *APIClient) CreateAuthenticationWithContext(ctx context.Context, r ApiCreateAuthenticationRequest, opts ...Option) (*AuthenticationCreateResponse, error) {
.
.
.
// actual implementation
}
```
- Playground example added to show context usage. 

## 🧪 Test
Code generation and CI pipeline should be enough.
  • Loading branch information
mehmetaligok authored May 9, 2023
1 parent 85c7c9f commit d2ad9cb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 6 additions & 1 deletion playground/go/personalization.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package main

import (
"context"
"fmt"
"time"

"github.com/algolia/algoliasearch-client-go/v4/algolia/personalization"
)

func testPersonalization(appID, apiKey string) int {
personalizationClient := personalization.NewClient(appID, apiKey, personalization.US)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()

deleteUserProfileResponse, err := personalizationClient.DeleteUserProfile(
// it will fail expectedly because of the very short timeout to showcase the context usage.
deleteUserProfileResponse, err := personalizationClient.DeleteUserProfileWithContext(ctx,
personalizationClient.NewApiDeleteUserProfileRequest("userToken"),
)
if err != nil {
Expand Down
12 changes: 10 additions & 2 deletions templates/go/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,20 @@ func (c *APIClient) NewApi{{{nickname}}}Request({{#pathParams}} {{paramName}} {{
}
}

// {{nickname}} wraps {{nickname}}WithContext using context.Background.
{{#isDeprecated}}
// Deprecated
{{/isDeprecated}}
func (c *APIClient) {{nickname}}(r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request, opts ...Option) ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}*{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}}error) {
return c.{{nickname}}WithContext(context.Background(), r, opts...)
}

// {{{description}}}{{#returnType}}
// @return {{{.}}}{{/returnType}}
{{#isDeprecated}}
// Deprecated
{{/isDeprecated}}
func (c *APIClient) {{nickname}}(r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request, opts ...Option) ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}*{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}}error) {
func (c *APIClient) {{nickname}}WithContext(ctx context.Context, r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request, opts ...Option) ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}*{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}}error) {
var (
postBody any
{{#returnType}}
Expand Down Expand Up @@ -224,7 +232,7 @@ func (c *APIClient) {{nickname}}(r {{#structPrefix}}{{&classname}}{{/structPrefi
postBody = r.{{paramName}}{{^required}}
} {{/required}}
{{/bodyParams}}
req, err := c.prepareRequest(context.Background(), requestPath, http.Method{{httpMethod}}, postBody, headers, queryParams)
req, err := c.prepareRequest(ctx, requestPath, http.Method{{httpMethod}}, postBody, headers, queryParams)
if err != nil {
return {{#returnType}}returnValue, {{/returnType}}err
}
Expand Down

0 comments on commit d2ad9cb

Please sign in to comment.