Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DB_ID=
PRIVATE_TOKEN=
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
7 changes: 7 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: "2"

linters:
disable-all: true
exclusions:
paths:
- examples
24 changes: 24 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Load environment variables from .env file if it exists
ifneq (,$(wildcard .env))
include .env
export
endif

# Default target
all: lint test
.PHONY: all

# Run golangci-lint
lint:
golangci-lint run ./recombee/...
.PHONY: lint

# Run unit-tests
test:
go test -short -v ./test
.PHONY: test

# Run integration tests
test-integration:
DB_ID=$(DB_ID) PRIVATE_TOKEN=$(PRIVATE_TOKEN) go test -v ./test
.PHONY: test-integration
1 change: 1 addition & 0 deletions recombee/errors/response_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package errors

import (
"fmt"

"github.com/recombee/go-api-client/v6/recombee/requests"
)

Expand Down
36 changes: 27 additions & 9 deletions recombee/recombee_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,22 @@ import (
"encoding/json"
"errors"
"fmt"
errorspkg "github.com/recombee/go-api-client/v6/recombee/errors"
requestspkg "github.com/recombee/go-api-client/v6/recombee/requests"
"io"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"time"

errorspkg "github.com/recombee/go-api-client/v6/recombee/errors"
requestspkg "github.com/recombee/go-api-client/v6/recombee/requests"
)

var (
ErrBothRegionAndBaseURI = errors.New("you cannot specify both region and baseUri")
ErrUnknownRegion = errors.New("unknown region specified")
ErrMissingRegionOrBaseURI = errors.New("you must specify either region or baseUri")
)

// RecombeeClient is a client for interacting with the Recombee API.
Expand Down Expand Up @@ -74,7 +81,7 @@ func NewRecombeeClientWithOptions(databaseId string, privateToken string, option

if options.Region != nil {
if baseUri != "" {
return nil, errors.New("You cannot specify both region and baseUri")
return nil, ErrBothRegionAndBaseURI
}

switch *options.Region {
Expand All @@ -87,15 +94,21 @@ func NewRecombeeClientWithOptions(databaseId string, privateToken string, option
case "us-west":
baseUri = "rapi-us-west.recombee.com"
default:
return nil, errors.New("Unknown region specified")
return nil, ErrUnknownRegion
}
}

if baseUri == "" {
return nil, errors.New("You must specify either region or baseUri")
return nil, ErrMissingRegionOrBaseURI
}

return &RecombeeClient{databaseId: databaseId, privateToken: privateToken, baseUri: baseUri, useHttpsByDefault: useHttpsByDefault, httpClient: httpClient}, nil
return &RecombeeClient{
databaseId: databaseId,
privateToken: privateToken,
baseUri: baseUri,
useHttpsByDefault: useHttpsByDefault,
httpClient: httpClient,
}, nil
}

// NewRecombeeClient creates a new instance of RecombeeClient with
Expand Down Expand Up @@ -134,7 +147,7 @@ func (c *RecombeeClient) signUrlStr(urlStr string) (string, error) {
return u.String() + "&hmac_sign=" + signature, nil
}

func (client *RecombeeClient) SendRequestWithContext(ctx context.Context, request *requestspkg.ApiRequest) error {
func (client *RecombeeClient) SendRequestWithContext(ctx context.Context, request *requestspkg.ApiRequest) (err error) {

parsedUrl, err := url.Parse(request.Path)
if err != nil {
Expand Down Expand Up @@ -192,7 +205,12 @@ func (client *RecombeeClient) SendRequestWithContext(ctx context.Context, reques
}
return err
}
defer response.Body.Close()
defer func() {
closeErr := response.Body.Close()
if closeErr != nil && err == nil {
err = closeErr
}
}()

bodyBytes, err := io.ReadAll(response.Body)
if err != nil {
Expand All @@ -207,7 +225,7 @@ func (client *RecombeeClient) SendRequestWithContext(ctx context.Context, reques

// Decode the response body into the target if not nil
if request.Target != nil {
if err := json.Unmarshal(bodyBytes, request.Target); err != nil {
if err = json.Unmarshal(bodyBytes, request.Target); err != nil {
return err
}
}
Expand Down
7 changes: 3 additions & 4 deletions recombee/requests/add_bookmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package requests

import (
"context"
"fmt"
"net/http"
timepkg "time" // avoid collision with param name
)
Expand All @@ -29,7 +28,7 @@ func NewAddBookmark(client ApiClient, userId string, itemId string) *AddBookmark
return &AddBookmark{
ApiRequest{
HttpMethod: http.MethodPost,
Path: fmt.Sprintf("/bookmarks/"),
Path: "/bookmarks/",
BodyParameters: bodyParameters,
QueryParameters: queryParams,
DefaultTimeout: 3000 * timepkg.Millisecond,
Expand Down Expand Up @@ -73,12 +72,12 @@ func (r *AddBookmark) SendWithContext(ctx context.Context) (string, error) {
if err != nil {
return "", err
}
return *(r.ApiRequest.Target.(*string)), err
return *(r.Target.(*string)), err
}

// Sends the request to the Recombee API
func (r *AddBookmark) Send() (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), r.ApiRequest.DefaultTimeout)
ctx, cancel := context.WithTimeout(context.Background(), r.DefaultTimeout)
defer cancel()
return r.SendWithContext(ctx)
}
7 changes: 3 additions & 4 deletions recombee/requests/add_cart_addition.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package requests

import (
"context"
"fmt"
"net/http"
timepkg "time" // avoid collision with param name
)
Expand All @@ -29,7 +28,7 @@ func NewAddCartAddition(client ApiClient, userId string, itemId string) *AddCart
return &AddCartAddition{
ApiRequest{
HttpMethod: http.MethodPost,
Path: fmt.Sprintf("/cartadditions/"),
Path: "/cartadditions/",
BodyParameters: bodyParameters,
QueryParameters: queryParams,
DefaultTimeout: 3000 * timepkg.Millisecond,
Expand Down Expand Up @@ -87,12 +86,12 @@ func (r *AddCartAddition) SendWithContext(ctx context.Context) (string, error) {
if err != nil {
return "", err
}
return *(r.ApiRequest.Target.(*string)), err
return *(r.Target.(*string)), err
}

// Sends the request to the Recombee API
func (r *AddCartAddition) Send() (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), r.ApiRequest.DefaultTimeout)
ctx, cancel := context.WithTimeout(context.Background(), r.DefaultTimeout)
defer cancel()
return r.SendWithContext(ctx)
}
7 changes: 3 additions & 4 deletions recombee/requests/add_detail_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package requests

import (
"context"
"fmt"
"net/http"
timepkg "time" // avoid collision with param name
)
Expand All @@ -29,7 +28,7 @@ func NewAddDetailView(client ApiClient, userId string, itemId string) *AddDetail
return &AddDetailView{
ApiRequest{
HttpMethod: http.MethodPost,
Path: fmt.Sprintf("/detailviews/"),
Path: "/detailviews/",
BodyParameters: bodyParameters,
QueryParameters: queryParams,
DefaultTimeout: 3000 * timepkg.Millisecond,
Expand Down Expand Up @@ -87,12 +86,12 @@ func (r *AddDetailView) SendWithContext(ctx context.Context) (string, error) {
if err != nil {
return "", err
}
return *(r.ApiRequest.Target.(*string)), err
return *(r.Target.(*string)), err
}

// Sends the request to the Recombee API
func (r *AddDetailView) Send() (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), r.ApiRequest.DefaultTimeout)
ctx, cancel := context.WithTimeout(context.Background(), r.DefaultTimeout)
defer cancel()
return r.SendWithContext(ctx)
}
4 changes: 2 additions & 2 deletions recombee/requests/add_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ func (r *AddItem) SendWithContext(ctx context.Context) (string, error) {
if err != nil {
return "", err
}
return *(r.ApiRequest.Target.(*string)), err
return *(r.Target.(*string)), err
}

// Sends the request to the Recombee API
func (r *AddItem) Send() (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), r.ApiRequest.DefaultTimeout)
ctx, cancel := context.WithTimeout(context.Background(), r.DefaultTimeout)
defer cancel()
return r.SendWithContext(ctx)
}
4 changes: 2 additions & 2 deletions recombee/requests/add_item_property.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ func (r *AddItemProperty) SendWithContext(ctx context.Context) (string, error) {
if err != nil {
return "", err
}
return *(r.ApiRequest.Target.(*string)), err
return *(r.Target.(*string)), err
}

// Sends the request to the Recombee API
func (r *AddItemProperty) Send() (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), r.ApiRequest.DefaultTimeout)
ctx, cancel := context.WithTimeout(context.Background(), r.DefaultTimeout)
defer cancel()
return r.SendWithContext(ctx)
}
4 changes: 2 additions & 2 deletions recombee/requests/add_manual_reql_segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ func (r *AddManualReqlSegment) SendWithContext(ctx context.Context) (string, err
if err != nil {
return "", err
}
return *(r.ApiRequest.Target.(*string)), err
return *(r.Target.(*string)), err
}

// Sends the request to the Recombee API
func (r *AddManualReqlSegment) Send() (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), r.ApiRequest.DefaultTimeout)
ctx, cancel := context.WithTimeout(context.Background(), r.DefaultTimeout)
defer cancel()
return r.SendWithContext(ctx)
}
7 changes: 3 additions & 4 deletions recombee/requests/add_purchase.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package requests

import (
"context"
"fmt"
"net/http"
timepkg "time" // avoid collision with param name
)
Expand All @@ -29,7 +28,7 @@ func NewAddPurchase(client ApiClient, userId string, itemId string) *AddPurchase
return &AddPurchase{
ApiRequest{
HttpMethod: http.MethodPost,
Path: fmt.Sprintf("/purchases/"),
Path: "/purchases/",
BodyParameters: bodyParameters,
QueryParameters: queryParams,
DefaultTimeout: 3000 * timepkg.Millisecond,
Expand Down Expand Up @@ -94,12 +93,12 @@ func (r *AddPurchase) SendWithContext(ctx context.Context) (string, error) {
if err != nil {
return "", err
}
return *(r.ApiRequest.Target.(*string)), err
return *(r.Target.(*string)), err
}

// Sends the request to the Recombee API
func (r *AddPurchase) Send() (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), r.ApiRequest.DefaultTimeout)
ctx, cancel := context.WithTimeout(context.Background(), r.DefaultTimeout)
defer cancel()
return r.SendWithContext(ctx)
}
7 changes: 3 additions & 4 deletions recombee/requests/add_rating.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package requests

import (
"context"
"fmt"
"net/http"
timepkg "time" // avoid collision with param name
)
Expand All @@ -30,7 +29,7 @@ func NewAddRating(client ApiClient, userId string, itemId string, rating float64
return &AddRating{
ApiRequest{
HttpMethod: http.MethodPost,
Path: fmt.Sprintf("/ratings/"),
Path: "/ratings/",
BodyParameters: bodyParameters,
QueryParameters: queryParams,
DefaultTimeout: 3000 * timepkg.Millisecond,
Expand Down Expand Up @@ -74,12 +73,12 @@ func (r *AddRating) SendWithContext(ctx context.Context) (string, error) {
if err != nil {
return "", err
}
return *(r.ApiRequest.Target.(*string)), err
return *(r.Target.(*string)), err
}

// Sends the request to the Recombee API
func (r *AddRating) Send() (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), r.ApiRequest.DefaultTimeout)
ctx, cancel := context.WithTimeout(context.Background(), r.DefaultTimeout)
defer cancel()
return r.SendWithContext(ctx)
}
10 changes: 5 additions & 5 deletions recombee/requests/add_search_synonym.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ package requests

import (
"context"
"fmt"
"github.com/recombee/go-api-client/v6/recombee/bindings"
"net/http"
timepkg "time" // avoid collision with param name

"github.com/recombee/go-api-client/v6/recombee/bindings"
)

// AddSearchSynonym Adds a new synonym for the [Search items](https://docs.recombee.com/api#search-items).
Expand Down Expand Up @@ -36,7 +36,7 @@ func NewAddSearchSynonym(client ApiClient, term string, synonym string) *AddSear
return &AddSearchSynonym{
ApiRequest{
HttpMethod: http.MethodPost,
Path: fmt.Sprintf("/synonyms/items/"),
Path: "/synonyms/items/",
BodyParameters: bodyParameters,
QueryParameters: queryParams,
DefaultTimeout: 10000 * timepkg.Millisecond,
Expand All @@ -60,12 +60,12 @@ func (r *AddSearchSynonym) SendWithContext(ctx context.Context) (bindings.Search
if err != nil {
return bindings.SearchSynonym{}, err
}
return *(r.ApiRequest.Target.(*bindings.SearchSynonym)), err
return *(r.Target.(*bindings.SearchSynonym)), err
}

// Sends the request to the Recombee API
func (r *AddSearchSynonym) Send() (bindings.SearchSynonym, error) {
ctx, cancel := context.WithTimeout(context.Background(), r.ApiRequest.DefaultTimeout)
ctx, cancel := context.WithTimeout(context.Background(), r.DefaultTimeout)
defer cancel()
return r.SendWithContext(ctx)
}
4 changes: 2 additions & 2 deletions recombee/requests/add_series.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ func (r *AddSeries) SendWithContext(ctx context.Context) (string, error) {
if err != nil {
return "", err
}
return *(r.ApiRequest.Target.(*string)), err
return *(r.Target.(*string)), err
}

// Sends the request to the Recombee API
func (r *AddSeries) Send() (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), r.ApiRequest.DefaultTimeout)
ctx, cancel := context.WithTimeout(context.Background(), r.DefaultTimeout)
defer cancel()
return r.SendWithContext(ctx)
}
Loading