-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(go): add more playground examples (#1447)
## 🧭 What and Why 🎟 JIRA Ticket: https://algolia.atlassian.net/browse/APIC-664 More structured playground examples were added for the go client to give more examples to test. ### Changes included: - Playground examples added for generated go packages. ## 🧪 Test can be tested with `yarn docker playground go {client}`
- Loading branch information
1 parent
8f2e937
commit 968aa54
Showing
11 changed files
with
276 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/algolia/algoliasearch-client-go/v4/algolia/analytics" | ||
) | ||
|
||
func testAnalytics(appID, apiKey string) int { | ||
indexName := getEnvWithDefault("ANALYTICS_INDEX", "test_index") | ||
analyticsClient := analytics.NewClient(appID, apiKey, analytics.US) | ||
|
||
getTopFilterForAttributeResponse, err := analyticsClient.GetTopFilterForAttribute( | ||
analyticsClient.NewApiGetTopFilterForAttributeRequest("myAttribute1,myAttribute2").WithIndex(indexName), | ||
) | ||
if err != nil { | ||
fmt.Printf("request error with GetTopFilterForAttribute: %v\n", err) | ||
return 1 | ||
} | ||
|
||
printResponse(getTopFilterForAttributeResponse) | ||
|
||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/algolia/algoliasearch-client-go/v4/algolia/ingestion" | ||
) | ||
|
||
func testIngestion(appID, apiKey string) int { | ||
ingestionClient := ingestion.NewClient(appID, apiKey, ingestion.US) | ||
|
||
listAuthenticationsResponse, err := ingestionClient.GetAuthentications( | ||
ingestionClient.NewApiGetAuthenticationsRequest().WithItemsPerPage(2), | ||
) | ||
if err != nil { | ||
fmt.Printf("request error with GetAuthentications: %v\n", err) | ||
return 1 | ||
} | ||
|
||
printResponse(listAuthenticationsResponse) | ||
|
||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/algolia/algoliasearch-client-go/v4/algolia/insights" | ||
) | ||
|
||
func testInsights(appID, apiKey string) int { | ||
insightsClient := insights.NewClient(appID, apiKey, insights.US) | ||
|
||
events := insights.NewInsightEvents([]insights.InsightEvent{ | ||
*insights.NewInsightEvent("click", | ||
"myEvent", | ||
"test_index", | ||
"myToken", | ||
insights.WithInsightEventPositions([]int32{1, 2, 3}), | ||
insights.WithInsightEventQueryID("myQueryID")), | ||
}) | ||
pushEventsResponse, err := insightsClient.PushEvents( | ||
insightsClient.NewApiPushEventsRequest().WithInsightEvents(*events), | ||
) | ||
if err != nil { | ||
fmt.Printf("request error with PushEvents: %v\n", err) | ||
return 1 | ||
} | ||
|
||
printResponse(pushEventsResponse) | ||
|
||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/algolia/algoliasearch-client-go/v4/algolia/personalization" | ||
) | ||
|
||
func testPersonalization(appID, apiKey string) int { | ||
personalizationClient := personalization.NewClient(appID, apiKey, personalization.US) | ||
|
||
deleteUserProfileResponse, err := personalizationClient.DeleteUserProfile( | ||
personalizationClient.NewApiDeleteUserProfileRequest("userToken"), | ||
) | ||
if err != nil { | ||
fmt.Printf("request error with DeleteUserProfile: %v\n", err) | ||
return 1 | ||
} | ||
|
||
printResponse(deleteUserProfileResponse) | ||
|
||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/algolia/algoliasearch-client-go/v4/algolia/predict" | ||
) | ||
|
||
func testPredict(appID, apiKey string) int { | ||
predictClient := predict.NewClient(appID, apiKey, predict.US) | ||
|
||
params := predict.AllParamsAsParams(predict.NewAllParams( | ||
predict.WithAllParamsModelsToRetrieve(predict.AllowedModelsToRetrieveEnumValues), | ||
predict.WithAllParamsTypesToRetrieve(predict.AllowedTypesToRetrieveEnumValues), | ||
)) | ||
userProfile, err := predictClient.FetchUserProfile( | ||
predictClient.NewApiFetchUserProfileRequest("userId").WithParams(params), | ||
) | ||
if err != nil { | ||
fmt.Printf("request error with FetchUserProfile: %v\n", err) | ||
return 1 | ||
} | ||
|
||
printResponse(userProfile) | ||
|
||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/algolia/algoliasearch-client-go/v4/algolia/query-suggestions" | ||
) | ||
|
||
func testQuerySuggestions(appID, apiKey string) int { | ||
suggestionsClient := suggestions.NewClient(appID, apiKey, suggestions.US) | ||
|
||
querySuggestionsIndex, err := suggestionsClient.GetAllConfigs( | ||
suggestionsClient.NewApiGetAllConfigsRequest(), | ||
) | ||
if err != nil { | ||
fmt.Printf("request error with GetAllConfigs: %v\n", err) | ||
return 1 | ||
} | ||
|
||
printResponse(querySuggestionsIndex) | ||
|
||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/algolia/algoliasearch-client-go/v4/algolia/recommend" | ||
) | ||
|
||
func testRecommend(appID, apiKey string) int { | ||
recommendClient := recommend.NewClient(appID, apiKey) | ||
|
||
/* | ||
recommend.NewGetRecommendationsParams([]recommend.RecommendationsRequest{ | ||
recommend.RecommendationRequestAsRecommendationsRequest(recommend.NewRecommendationRequest(recommend.RECOMMENDATIONMODELS_BOUGHT_TOGETHER, "test_query", "test_index", 0)), | ||
}) | ||
*/ | ||
// alternative way to create the payloads, a similar approach can be used with any of the other clients | ||
params := recommend.GetRecommendationsParams{ | ||
Requests: []recommend.RecommendationsRequest{ | ||
{ | ||
RecommendationRequest: &recommend.RecommendationRequest{ | ||
Model: recommend.RECOMMENDATIONMODELS_BOUGHT_TOGETHER, | ||
ObjectID: "test_query", | ||
IndexName: "test_index", | ||
Threshold: 0, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
searchResponse, err := recommendClient.GetRecommendations( | ||
recommendClient.NewApiGetRecommendationsRequest().WithGetRecommendationsParams(params), | ||
) | ||
if err != nil { | ||
fmt.Printf("request error with SearchSingleIndex: %v\n", err) | ||
return 1 | ||
} | ||
|
||
printResponse(searchResponse) | ||
|
||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/algolia/algoliasearch-client-go/v4/algolia/search" | ||
) | ||
|
||
func testSearch(appID, apiKey string) int { | ||
indexName := getEnvWithDefault("SEARCH_INDEX", "test_index") | ||
searchClient := search.NewClient(appID, apiKey) | ||
|
||
searchParams := search.SearchParamsStringAsSearchParams(search.NewSearchParamsString(search.WithSearchParamsStringParams("query=jeans&hitsPerPage=2"))) | ||
searchResponse, err := searchClient.SearchSingleIndex(searchClient.NewApiSearchSingleIndexRequest(indexName).WithSearchParams(searchParams)) | ||
if err != nil { | ||
fmt.Printf("request error with SearchSingleIndex: %v\n", err) | ||
return 1 | ||
} | ||
|
||
printResponse(searchResponse) | ||
|
||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
func printResponse(res any) { | ||
bJson, _ := json.Marshal(res) | ||
|
||
fmt.Printf("response json: %s\n", string(bJson)) | ||
fmt.Printf("response string: %s\n", res) | ||
} | ||
|
||
func getEnvWithDefault(key, fallback string) string { | ||
if value, ok := os.LookupEnv(key); ok && value != "" { | ||
return value | ||
} | ||
return fallback | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters