From d2ad9cb9eda52a633964bb457de814b7ac5ec530 Mon Sep 17 00:00:00 2001 From: Mehmet Ali Gok <33124154+mehmetaligok@users.noreply.github.com> Date: Tue, 9 May 2023 14:55:19 +0200 Subject: [PATCH] feat(go): Add context support to all requests (#1527) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## ๐Ÿงญ 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. --- playground/go/personalization.go | 7 ++++++- templates/go/api.mustache | 12 ++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/playground/go/personalization.go b/playground/go/personalization.go index 0ed5384b8a..6cbd7aee32 100644 --- a/playground/go/personalization.go +++ b/playground/go/personalization.go @@ -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 { diff --git a/templates/go/api.mustache b/templates/go/api.mustache index 9c5d140fa1..7cbf6a89f7 100644 --- a/templates/go/api.mustache +++ b/templates/go/api.mustache @@ -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}} @@ -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 }