Skip to content

Commit e65cb20

Browse files
committed
fix the playground
1 parent f76f2f9 commit e65cb20

File tree

8 files changed

+40
-27
lines changed

8 files changed

+40
-27
lines changed

playground/go/analytics.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56

67
"github.com/algolia/algoliasearch-client-go/v4/algolia/next/analytics"
78
)
89

9-
func testAnalytics(appID, apiKey string) int {
10+
func testAnalytics(ctx context.Context, appID, apiKey string) int {
1011
indexName := getEnvWithDefault("ANALYTICS_INDEX", "test_index")
1112
analyticsClient, err := analytics.NewClient(appID, apiKey, analytics.US)
1213
if err != nil {
1314
panic(err)
1415
}
1516

16-
getTopFilterForAttributeResponse, err := analyticsClient.GetTopFilterForAttribute("myAttribute1,myAttribute2", indexName, nil)
17+
getTopFilterForAttributeResponse, err := analyticsClient.GetTopFilterForAttribute(ctx, "myAttribute1,myAttribute2", indexName, nil)
1718
if err != nil {
1819
fmt.Printf("request error with GetTopFilterForAttribute: %v\n", err)
1920
return 1

playground/go/ingestion.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56

67
"github.com/algolia/algoliasearch-client-go/v4/algolia/next/ingestion"
78
)
89

9-
func testIngestion(appID, apiKey string) int {
10+
func testIngestion(ctx context.Context, appID, apiKey string) int {
1011
ingestionClient, err := ingestion.NewClient(appID, apiKey, ingestion.US)
1112
if err != nil {
1213
panic(err)
1314
}
1415

1516
// another example to generate payload for a request.
16-
res, err := ingestionClient.ListTasks(nil)
17+
res, err := ingestionClient.ListTasks(ctx, nil)
1718
if err != nil {
1819
fmt.Printf("request error: %v\n", err)
1920

playground/go/insights.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56

67
"github.com/algolia/algoliasearch-client-go/v4/algolia/next/insights"
78
)
89

9-
func testInsights(appID, apiKey string) int {
10+
func testInsights(ctx context.Context, appID, apiKey string) int {
1011
insightsClient, err := insights.NewClient(appID, apiKey, insights.US)
1112
if err != nil {
1213
panic(err)
@@ -20,7 +21,7 @@ func testInsights(appID, apiKey string) int {
2021
"myToken",
2122
insights.WithClickedObjectIDsTimestamp(1234567890))),
2223
}
23-
eventsResponse, err := insightsClient.PushEvents(events)
24+
eventsResponse, err := insightsClient.PushEvents(ctx, events)
2425
if err != nil {
2526
fmt.Printf("request error with PushEvents: %v\n", err)
2627
return 1

playground/go/main.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"flag"
56
"fmt"
67
"os"
@@ -10,7 +11,11 @@ import (
1011

1112
func main() {
1213
fmt.Println("Go playground")
13-
godotenv.Load("../.env")
14+
err := godotenv.Load("../.env")
15+
if err != nil {
16+
panic(fmt.Errorf("error loading .env file: %w", err))
17+
}
18+
1419
appID := os.Getenv("ALGOLIA_APPLICATION_ID")
1520
apiKey := os.Getenv("ALGOLIA_ADMIN_KEY")
1621

@@ -27,21 +32,23 @@ func main() {
2732

2833
// debug.Enable()
2934

35+
ctx := context.Background()
36+
3037
switch client {
3138
case "ingestion":
32-
returnCode = testIngestion(appID, apiKey)
39+
returnCode = testIngestion(ctx, appID, apiKey)
3340
case "search":
34-
returnCode = testSearch(appID, apiKey)
41+
returnCode = testSearch(ctx, appID, apiKey)
3542
case "analytics":
36-
returnCode = testAnalytics(appID, apiKey)
43+
returnCode = testAnalytics(ctx, appID, apiKey)
3744
case "insights":
38-
returnCode = testInsights(appID, apiKey)
45+
returnCode = testInsights(ctx, appID, apiKey)
3946
case "personalization":
40-
returnCode = testPersonalization(appID, apiKey)
47+
returnCode = testPersonalization(ctx, appID, apiKey)
4148
case "query-suggestions":
42-
returnCode = testQuerySuggestions(appID, apiKey)
49+
returnCode = testQuerySuggestions(ctx, appID, apiKey)
4350
case "recommend":
44-
returnCode = testRecommend(appID, apiKey)
51+
returnCode = testRecommend(ctx, appID, apiKey)
4552
default:
4653
fmt.Println("Please specify a valid client name")
4754
os.Exit(1)

playground/go/personalization.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ import (
88
"github.com/algolia/algoliasearch-client-go/v4/algolia/next/personalization"
99
)
1010

11-
func testPersonalization(appID, apiKey string) int {
11+
func testPersonalization(ctx context.Context, appID, apiKey string) int {
1212
personalizationClient, err := personalization.NewClient(appID, apiKey, personalization.US)
1313
if err != nil {
1414
panic(err)
1515
}
16-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
16+
ctx, cancel := context.WithTimeout(ctx, 10*time.Millisecond)
1717
defer cancel()
1818

1919
// it will fail expectedly because of the very short timeout to showcase the context usage.
20-
deleteUserProfileResponse, err := personalizationClient.DeleteUserProfile("userToken", personalization.WithContext(ctx))
20+
deleteUserProfileResponse, err := personalizationClient.DeleteUserProfile(ctx, "userToken")
2121
if err != nil {
2222
fmt.Printf("request error with DeleteUserProfile: %v\n", err)
2323
return 1

playground/go/query-suggestions.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56

67
suggestions "github.com/algolia/algoliasearch-client-go/v4/algolia/next/query-suggestions"
78
)
89

9-
func testQuerySuggestions(appID, apiKey string) int {
10+
func testQuerySuggestions(ctx context.Context, appID, apiKey string) int {
1011
suggestionsClient, err := suggestions.NewClient(appID, apiKey, suggestions.US)
1112
if err != nil {
1213
panic(err)
1314
}
1415

15-
querySuggestionsIndex, err := suggestionsClient.GetAllConfigs()
16+
querySuggestionsIndex, err := suggestionsClient.GetAllConfigs(ctx)
1617
if err != nil {
1718
fmt.Printf("request error with GetAllConfigs: %v\n", err)
1819
return 1

playground/go/recommend.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56

67
"github.com/algolia/algoliasearch-client-go/v4/algolia/next/recommend"
78
)
89

9-
func testRecommend(appID, apiKey string) int {
10+
func testRecommend(ctx context.Context, appID, apiKey string) int {
1011
recommendClient, err := recommend.NewClient(appID, apiKey)
1112
if err != nil {
1213
panic(err)
@@ -27,7 +28,7 @@ func testRecommend(appID, apiKey string) int {
2728
},
2829
}}
2930

30-
searchResponse, err := recommendClient.GetRecommendations(params)
31+
searchResponse, err := recommendClient.GetRecommendations(ctx, params)
3132
if err != nil {
3233
fmt.Printf("request error with SearchSingleIndex: %v\n", err)
3334
return 1

playground/go/search.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56

67
"github.com/algolia/algoliasearch-client-go/v4/algolia/next/search"
78
)
89

9-
func testSearch(appID, apiKey string) int {
10+
func testSearch(ctx context.Context, appID, apiKey string) int {
1011
// indexName := getEnvWithDefault("SEARCH_INDEX", "test_index")
1112
searchClient, err := search.NewClient(appID, apiKey)
1213
if err != nil {
1314
panic(err)
1415
}
1516

16-
err = searchClient.BrowseObjects("test-flag", *search.NewEmptyBrowseParamsObject(), search.WithAggregator(func(res any, err error) {
17+
err = searchClient.BrowseObjects(ctx, "test-flag", *search.NewEmptyBrowseParamsObject(), search.WithAggregator(func(res any, err error) {
1718
if err != nil {
1819
panic(err)
1920
}
@@ -29,12 +30,12 @@ func testSearch(appID, apiKey string) int {
2930
//})))
3031

3132
// new way
32-
//searchClient.Search([]search.SearchQuery{
33+
//searchClient.Search(ctx, []search.SearchQuery{
3334
// search.NewSearchForHits("indexName").WithQuery("foo"),
3435
//}, nil)
3536

3637
/*
37-
response, err := searchClient.AddOrUpdateObject(
38+
response, err := searchClient.AddOrUpdateObject(ctx,
3839
searchClient.NewApiAddOrUpdateObjectRequest(
3940
indexName,
4041
"1",
@@ -49,12 +50,12 @@ func testSearch(appID, apiKey string) int {
4950
panic(err)
5051
}
5152
52-
_, err = searchClient.WaitForTask(indexName, *response.TaskID)
53+
_, err = searchClient.WaitForTask(ctx, indexName, *response.TaskID)
5354
if err != nil {
5455
panic(err)
5556
}
5657
57-
searchResponse, err := searchClient.Search(
58+
searchResponse, err := searchClient.Search(ctx,
5859
searchClient.NewApiSearchRequest(
5960
search.NewSearchMethodParams(
6061
[]search.SearchQuery{

0 commit comments

Comments
 (0)