Skip to content

Commit

Permalink
chore: generated code for commit 30a1f27. [skip ci]
Browse files Browse the repository at this point in the history
Co-authored-by: Pierre Millot <pierre.millot@algolia.com>
  • Loading branch information
algolia-bot and millotp committed Jan 18, 2024
1 parent 30a1f27 commit 79f8eb5
Show file tree
Hide file tree
Showing 65 changed files with 3,860 additions and 5,962 deletions.
69 changes: 41 additions & 28 deletions clients/algoliasearch-client-go/algolia/abtesting/api_abtesting.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"net/url"
"strings"

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

type Option struct {
Expand Down Expand Up @@ -105,8 +105,9 @@ func (c *APIClient) AddABTestsWithContext(ctx context.Context, r ApiAddABTestsRe

headers := make(map[string]string)
queryParams := url.Values{}

if r.addABTestsRequest == nil {
return returnValue, reportError("addABTestsRequest is required and must be specified")
return returnValue, reportError("Parameter `addABTestsRequest` is required when calling `AddABTests`.")
}

// optional params if any
Expand All @@ -126,7 +127,7 @@ func (c *APIClient) AddABTestsWithContext(ctx context.Context, r ApiAddABTestsRe
return returnValue, err
}

res, err := c.callAPI(req, call.Write)
res, err := c.callAPI(req, false)
if err != nil {
return returnValue, err
}
Expand Down Expand Up @@ -245,12 +246,15 @@ func (c *APIClient) CustomDeleteWithContext(ctx context.Context, r ApiCustomDele
)

requestPath := "/1{path}"
requestPath = strings.ReplaceAll(requestPath, "{"+"path"+"}", url.PathEscape(parameterToString(r.path)))
requestPath = strings.ReplaceAll(requestPath, "{path}", url.PathEscape(parameterToString(r.path)))

headers := make(map[string]string)
queryParams := url.Values{}
if r.path == "" {
return returnValue, reportError("Parameter `path` is required when calling `CustomDelete`.")
}

if !isNilorEmpty(r.parameters) {
if !utils.IsNilOrEmpty(r.parameters) {
for k, v := range r.parameters {
queryParams.Set(k, parameterToString(v))
}
Expand All @@ -271,7 +275,7 @@ func (c *APIClient) CustomDeleteWithContext(ctx context.Context, r ApiCustomDele
return returnValue, err
}

res, err := c.callAPI(req, call.Write)
res, err := c.callAPI(req, false)
if err != nil {
return returnValue, err
}
Expand Down Expand Up @@ -390,12 +394,15 @@ func (c *APIClient) CustomGetWithContext(ctx context.Context, r ApiCustomGetRequ
)

requestPath := "/1{path}"
requestPath = strings.ReplaceAll(requestPath, "{"+"path"+"}", url.PathEscape(parameterToString(r.path)))
requestPath = strings.ReplaceAll(requestPath, "{path}", url.PathEscape(parameterToString(r.path)))

headers := make(map[string]string)
queryParams := url.Values{}
if r.path == "" {
return returnValue, reportError("Parameter `path` is required when calling `CustomGet`.")
}

if !isNilorEmpty(r.parameters) {
if !utils.IsNilOrEmpty(r.parameters) {
for k, v := range r.parameters {
queryParams.Set(k, parameterToString(v))
}
Expand All @@ -416,7 +423,7 @@ func (c *APIClient) CustomGetWithContext(ctx context.Context, r ApiCustomGetRequ
return returnValue, err
}

res, err := c.callAPI(req, call.Write)
res, err := c.callAPI(req, false)
if err != nil {
return returnValue, err
}
Expand Down Expand Up @@ -553,12 +560,15 @@ func (c *APIClient) CustomPostWithContext(ctx context.Context, r ApiCustomPostRe
)

requestPath := "/1{path}"
requestPath = strings.ReplaceAll(requestPath, "{"+"path"+"}", url.PathEscape(parameterToString(r.path)))
requestPath = strings.ReplaceAll(requestPath, "{path}", url.PathEscape(parameterToString(r.path)))

headers := make(map[string]string)
queryParams := url.Values{}
if r.path == "" {
return returnValue, reportError("Parameter `path` is required when calling `CustomPost`.")
}

if !isNilorEmpty(r.parameters) {
if !utils.IsNilOrEmpty(r.parameters) {
for k, v := range r.parameters {
queryParams.Set(k, parameterToString(v))
}
Expand All @@ -575,7 +585,7 @@ func (c *APIClient) CustomPostWithContext(ctx context.Context, r ApiCustomPostRe
}

// body params
if isNilorEmpty(r.body) {
if utils.IsNilOrEmpty(r.body) {
postBody = "{}"
} else {
postBody = r.body
Expand All @@ -585,7 +595,7 @@ func (c *APIClient) CustomPostWithContext(ctx context.Context, r ApiCustomPostRe
return returnValue, err
}

res, err := c.callAPI(req, call.Write)
res, err := c.callAPI(req, false)
if err != nil {
return returnValue, err
}
Expand Down Expand Up @@ -722,12 +732,15 @@ func (c *APIClient) CustomPutWithContext(ctx context.Context, r ApiCustomPutRequ
)

requestPath := "/1{path}"
requestPath = strings.ReplaceAll(requestPath, "{"+"path"+"}", url.PathEscape(parameterToString(r.path)))
requestPath = strings.ReplaceAll(requestPath, "{path}", url.PathEscape(parameterToString(r.path)))

headers := make(map[string]string)
queryParams := url.Values{}
if r.path == "" {
return returnValue, reportError("Parameter `path` is required when calling `CustomPut`.")
}

if !isNilorEmpty(r.parameters) {
if !utils.IsNilOrEmpty(r.parameters) {
for k, v := range r.parameters {
queryParams.Set(k, parameterToString(v))
}
Expand All @@ -744,7 +757,7 @@ func (c *APIClient) CustomPutWithContext(ctx context.Context, r ApiCustomPutRequ
}

// body params
if isNilorEmpty(r.body) {
if utils.IsNilOrEmpty(r.body) {
postBody = "{}"
} else {
postBody = r.body
Expand All @@ -754,7 +767,7 @@ func (c *APIClient) CustomPutWithContext(ctx context.Context, r ApiCustomPutRequ
return returnValue, err
}

res, err := c.callAPI(req, call.Write)
res, err := c.callAPI(req, false)
if err != nil {
return returnValue, err
}
Expand Down Expand Up @@ -857,7 +870,7 @@ func (c *APIClient) DeleteABTestWithContext(ctx context.Context, r ApiDeleteABTe
)

requestPath := "/2/abtests/{id}"
requestPath = strings.ReplaceAll(requestPath, "{"+"id"+"}", url.PathEscape(parameterToString(r.id)))
requestPath = strings.ReplaceAll(requestPath, "{id}", url.PathEscape(parameterToString(r.id)))

headers := make(map[string]string)
queryParams := url.Values{}
Expand All @@ -877,7 +890,7 @@ func (c *APIClient) DeleteABTestWithContext(ctx context.Context, r ApiDeleteABTe
return returnValue, err
}

res, err := c.callAPI(req, call.Write)
res, err := c.callAPI(req, false)
if err != nil {
return returnValue, err
}
Expand Down Expand Up @@ -980,7 +993,7 @@ func (c *APIClient) GetABTestWithContext(ctx context.Context, r ApiGetABTestRequ
)

requestPath := "/2/abtests/{id}"
requestPath = strings.ReplaceAll(requestPath, "{"+"id"+"}", url.PathEscape(parameterToString(r.id)))
requestPath = strings.ReplaceAll(requestPath, "{id}", url.PathEscape(parameterToString(r.id)))

headers := make(map[string]string)
queryParams := url.Values{}
Expand All @@ -1000,7 +1013,7 @@ func (c *APIClient) GetABTestWithContext(ctx context.Context, r ApiGetABTestRequ
return returnValue, err
}

res, err := c.callAPI(req, call.Write)
res, err := c.callAPI(req, false)
if err != nil {
return returnValue, err
}
Expand Down Expand Up @@ -1163,16 +1176,16 @@ func (c *APIClient) ListABTestsWithContext(ctx context.Context, r ApiListABTests
headers := make(map[string]string)
queryParams := url.Values{}

if !isNilorEmpty(r.offset) {
if !utils.IsNilOrEmpty(r.offset) {
queryParams.Set("offset", parameterToString(r.offset))
}
if !isNilorEmpty(r.limit) {
if !utils.IsNilOrEmpty(r.limit) {
queryParams.Set("limit", parameterToString(r.limit))
}
if !isNilorEmpty(r.indexPrefix) {
if !utils.IsNilOrEmpty(r.indexPrefix) {
queryParams.Set("indexPrefix", parameterToString(r.indexPrefix))
}
if !isNilorEmpty(r.indexSuffix) {
if !utils.IsNilOrEmpty(r.indexSuffix) {
queryParams.Set("indexSuffix", parameterToString(r.indexSuffix))
}

Expand All @@ -1191,7 +1204,7 @@ func (c *APIClient) ListABTestsWithContext(ctx context.Context, r ApiListABTests
return returnValue, err
}

res, err := c.callAPI(req, call.Write)
res, err := c.callAPI(req, false)
if err != nil {
return returnValue, err
}
Expand Down Expand Up @@ -1296,7 +1309,7 @@ func (c *APIClient) StopABTestWithContext(ctx context.Context, r ApiStopABTestRe
)

requestPath := "/2/abtests/{id}/stop"
requestPath = strings.ReplaceAll(requestPath, "{"+"id"+"}", url.PathEscape(parameterToString(r.id)))
requestPath = strings.ReplaceAll(requestPath, "{id}", url.PathEscape(parameterToString(r.id)))

headers := make(map[string]string)
queryParams := url.Values{}
Expand All @@ -1316,7 +1329,7 @@ func (c *APIClient) StopABTestWithContext(ctx context.Context, r ApiStopABTestRe
return returnValue, err
}

res, err := c.callAPI(req, call.Write)
res, err := c.callAPI(req, false)
if err != nil {
return returnValue, err
}
Expand Down
56 changes: 31 additions & 25 deletions clients/algoliasearch-client-go/algolia/abtesting/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ import (
"reflect"
"regexp"
"runtime"
"slices"
"strings"
"time"

"github.com/go-playground/validator/v10"

"github.com/algolia/algoliasearch-client-go/v4/algolia/call"
"github.com/algolia/algoliasearch-client-go/v4/algolia/compression"
"github.com/algolia/algoliasearch-client-go/v4/algolia/internal/transport"
"github.com/algolia/algoliasearch-client-go/v4/algolia/transport"
)

var jsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:vnd\.[^;]+\+)?json)`)
Expand All @@ -37,31 +38,33 @@ type APIClient struct {
}

// NewClient creates a new API client with appID, apiKey and region.
func NewClient(appID, apiKey string, region Region) *APIClient {
func NewClient(appID, apiKey string, region Region) (*APIClient, error) {
return NewClientWithConfig(Configuration{
AppID: appID,
ApiKey: apiKey,
Region: region,
DefaultHeader: make(map[string]string),
UserAgent: getUserAgent(),
Debug: false,
Requester: newDefaultRequester(),
Configuration: transport.Configuration{
AppID: appID,
ApiKey: apiKey,
DefaultHeader: make(map[string]string),
UserAgent: getUserAgent(),
Debug: false,
Requester: transport.NewDefaultRequester(nil),
},
Region: region,
})
}

// NewClientWithConfig creates a new API client with the given configuration to fully customize the client behaviour.
func NewClientWithConfig(cfg Configuration) *APIClient {
func NewClientWithConfig(cfg Configuration) (*APIClient, error) {
var hosts []*transport.StatefulHost

if cfg.AppID == "" {
panic("appID is required")
return nil, errors.New("`appId` is missing.")
}
if cfg.ApiKey == "" {
panic("apiKey is required")
return nil, errors.New("`apiKey` is missing.")
}
if len(cfg.Hosts) == 0 {
if cfg.Region == "" {
panic("region is required")
if cfg.Region != "" && !slices.Contains(allowedRegions[:], string(cfg.Region)) {
return nil, fmt.Errorf("`region` must be one of the following: %s", strings.Join(allowedRegions[:], ", "))
}
hosts = getDefaultHosts(cfg.Region)
} else {
Expand All @@ -70,7 +73,7 @@ func NewClientWithConfig(cfg Configuration) *APIClient {
}
}
if cfg.Requester == nil {
cfg.Requester = newDefaultRequester()
cfg.Requester = transport.NewDefaultRequester(&cfg.ConnectTimeout)
}
if cfg.UserAgent == "" {
cfg.UserAgent = getUserAgent()
Expand All @@ -84,20 +87,18 @@ func NewClientWithConfig(cfg Configuration) *APIClient {
cfg.Requester,
cfg.ReadTimeout,
cfg.WriteTimeout,
cfg.ConnectTimeout,
cfg.Compression,
),
}
}, nil
}

func getDefaultHosts(r Region) []*transport.StatefulHost {
hosts := []*transport.StatefulHost{}
switch r {
case DE, US:
hosts = append(hosts, transport.NewStatefulHost(strings.ReplaceAll("analytics.{region}.algolia.com", "{region}", string(r)), call.IsReadWrite))
default:
hosts = append(hosts, transport.NewStatefulHost("analytics.algolia.com", call.IsReadWrite))
if r == "" {
return []*transport.StatefulHost{transport.NewStatefulHost("analytics.algolia.com", call.IsReadWrite)}
}
return hosts

return []*transport.StatefulHost{transport.NewStatefulHost(strings.ReplaceAll("analytics.{region}.algolia.com", "{region}", string(r)), call.IsReadWrite)}
}

func getUserAgent() string {
Expand All @@ -121,7 +122,7 @@ func (c *APIClient) AddDefaultHeader(key string, value string) {
}

// callAPI do the request.
func (c *APIClient) callAPI(request *http.Request, kind call.Kind) (*http.Response, error) {
func (c *APIClient) callAPI(request *http.Request, useReadTransporter bool) (*http.Response, error) {
if c.cfg.Debug {
dump, err := httputil.DumpRequestOut(request, true)
if err != nil {
Expand All @@ -130,7 +131,12 @@ func (c *APIClient) callAPI(request *http.Request, kind call.Kind) (*http.Respon
log.Printf("\n%s\n", string(dump))
}

resp, err := c.transport.Request(request.Context(), request, kind)
callKind := call.Write
if useReadTransporter || request.Method == http.MethodGet {
callKind = call.Read
}

resp, err := c.transport.Request(request.Context(), request, callKind)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 79f8eb5

Please sign in to comment.