Skip to content

Commit

Permalink
chore(guides): add/update code snippets for Kotlin, Dart and Java (#1984
Browse files Browse the repository at this point in the history
)
  • Loading branch information
aallam authored Oct 16, 2023
1 parent 57c9e1f commit 4060bb3
Show file tree
Hide file tree
Showing 19 changed files with 1,247 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.algolia.client.configuration.ClientOptions
import com.algolia.client.model.recommend.GetRecommendationsParams
import com.algolia.client.model.recommend.RecommendationModels
import com.algolia.client.model.recommend.RecommendationsQuery
import com.algolia.client.model.recommend.RecommendationsRequest
import io.github.cdimascio.dotenv.Dotenv
import io.ktor.client.plugins.logging.*
import kotlin.system.exitProcess
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,45 @@ client.waitForTask("<SOURCE_INDEX_NAME>", response.getTaskID());
```

</TabItem>

<TabItem value="kotlin">

```kotlin
val response = client.operationIndex(
indexName = "<SOURCE_INDEX_NAME>",
operationIndexParams = OperationIndexParams(
operation = OperationType.Copy,
destination = "<DESTINATION_INDEX_NAME>"
),
)

// Poll the task status until it's done
client.waitTask("<SOURCE_INDEX_NAME>", response.taskID)
```

</TabItem>

<TabItem value="dart">

```dart
var response = await client.operationIndex(
indexName: '<SOURCE_INDEX_NAME>',
operationIndexParams: OperationIndexParams(
operation: OperationType.copy,
destination: '<DESTINATION_INDEX_NAME>'
),
);
// Poll the task status until it's done
await client.waitTask('<SOURCE_INDEX_NAME>', response.taskID);
```

</TabItem>

<TabItem value="go">
// TBD
</TabItem>

</TabsLanguage>

## Rename/move an index
Expand Down Expand Up @@ -138,6 +177,44 @@ UpdatedAtResponse response = client.operationIndex(
client.waitForTask("<SOURCE_INDEX_NAME>", response.getTaskID());
```

</TabItem>

<TabItem value="kotlin">

```kotlin
val response = client.operationIndex(
indexName = "<SOURCE_INDEX_NAME>",
operationIndexParams = OperationIndexParams(
operation = OperationType.Move,
destination = "<DESTINATION_INDEX_NAME>"
),
)

// Poll the task status until it's done
client.waitTask("<SOURCE_INDEX_NAME>", response.taskID)
```

</TabItem>

<TabItem value="dart">

```dart
var response = await client.operationIndex(
indexName: '<SOURCE_INDEX_NAME>',
operationIndexParams: OperationIndexParams(
operation: OperationType.move,
destination: '<DESTINATION_INDEX_NAME>'
),
);
// Poll the task status until it's done
await client.waitTask('<SOURCE_INDEX_NAME>', response.taskID);
```

</TabItem>

<TabItem value="go">
// TBD
</TabItem>
</TabsLanguage>

Expand Down Expand Up @@ -208,5 +285,43 @@ UpdatedAtResponse response = client.operationIndex(
client.waitForTask("<SOURCE_INDEX_NAME>", response.getTaskID());
```

</TabItem>

<TabItem value="kotlin">

```kotlin
val response = client.operationIndex(
indexName = "<SOURCE_INDEX_NAME>",
operationIndexParams = OperationIndexParams(
operation = OperationType.Move,
destination = "<DESTINATION_INDEX_NAME>",
scope = listOf(ScopeType.Rules),
),
)

client.waitTask("<SOURCE_INDEX_NAME>", response.taskID)
```

</TabItem>

<TabItem value="dart">

```dart
var response = await client.operationIndex(
indexName: '<SOURCE_INDEX_NAME>',
operationIndexParams: OperationIndexParams(
operation: OperationType.move,
destination: '<DESTINATION_INDEX_NAME>',
scope: [ScopeType.rules],
),
);
await client.waitTask('<SOURCE_INDEX_NAME>', response.taskID);
```

</TabItem>

<TabItem value="go">
// TBD
</TabItem>
</TabsLanguage>
196 changes: 176 additions & 20 deletions website/docs/clients/guides/customized-client-usage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,56 @@ You can set a custom logger on the client to enable more or less debug output de

```java
import com.algolia.api.SearchClient;
import com.algolia.utils.LogLevel;
import com.algolia.config.ClientOptions;

ClientOptions options = ClientOptions.builder()
.setLogger(message -> System.out.println(message))
.build();
SearchClient client = new SearchClient(
"<YOUR_INDEX_NAME>",
"<YOUR_API_KEY>",
options
);
```

SearchClient client = new SearchClient("<YOUR_INDEX_NAME>", "<YOUR_API_KEY>");
</TabItem>

// No logs.
client.setLogLevel(LogLevel.NONE);
<TabItem value="kotlin">

```kotlin
import com.algolia.client.api.SearchClient
import com.algolia.client.configuration.ClientOptions
import io.ktor.client.plugins.logging.Logger

val client = SearchClient(
appId = "<YOUR_INDEX_NAME>",
apiKey = "<YOUR_API_KEY>",
options = ClientOptions(
logger = object : Logger {
override fun log(message: String) {
println(message)
}
},
)
)
```

// Logs request and response lines.
client.setLogLevel(LogLevel.BASIC);
</TabItem>

// Logs request and response lines and their respective headers.
client.setLogLevel(LogLevel.HEADERS);
<TabItem value="dart">

// Logs request and response lines and their respective headers and bodies (if present).
client.setLogLevel(LogLevel.BODY);
```dart
var client = SearchClient(
appId: '<YOUR_INDEX_NAME>',
apiKey: '<YOUR_API_KEY>',
options: ClientOptions(logger: print),
);
```

</TabItem>

<TabItem value="go">
// TBD
</TabItem>
</TabsLanguage>

Expand Down Expand Up @@ -101,14 +134,12 @@ import com.algolia.utils.RequestOptions;

client.search(
new SearchMethodParams()
.addRequests(SearchQuery.of(
.addRequests(
new SearchForHits()
.setIndexName("<YOUR_INDEX_NAME>")
.setQuery("<YOUR_QUERY>")
.setHitsPerPage(50)
)
),
MyObject.class,
new RequestOptions()
// This header is added to the request
.addExtraHeader("additional-header", "hello")
Expand All @@ -117,6 +148,58 @@ client.search(
);
```

</TabItem>

<TabItem value="kotlin">

```kotlin
client.search(
searchMethodParams = SearchMethodParams(
requests = listOf(
SearchForHits(
indexName = "<YOUR_INDEX_NAME>",
query = "<YOUR_QUERY>",
hitsPerPage = 50,
)
)
),
requestOptions = RequestOptions(
// This header is added to the request
headers = mapOf("additional-header" to "hello"),
// As we re-define `hitsPerPage`, it will override the value defined in the method's parameters.
urlParameters = mapOf("hitsPerPage" to 100)
)
)
```

</TabItem>

<TabItem value="dart">

```dart
await client.search(
searchMethodParams: SearchMethodParams(
requests: [
SearchForHits(
indexName: '<YOUR_INDEX_NAME>',
query: '<YOUR_QUERY>',
hitsPerPage: 50,
)
],
),
requestOptions: RequestOptions(
// This header is added to the request
headers: {'additional-header': 'hello'},
// As we re-define `hitsPerPage`, it will override the value defined in the method's parameters.
urlParameters: {'hitsPerPage': 100},
),
);
```

</TabItem>

<TabItem value="go">
// TBD
</TabItem>
</TabsLanguage>

Expand Down Expand Up @@ -159,17 +242,60 @@ Algolia\AlgoliaSearch\Support\AlgoliaAgent::addAlgoliaAgent(
<TabItem value="java">

```java
import com.algolia.utils.ClientOptions;
import com.algolia.api.SearchClient;
import com.algolia.config.ClientOptions;

// This will append `; my user agent (optional version)` to the current value of `x-algolia-agent` for every requests
ClientOptions options = ClientOptions.builder()
.addAlgoliaAgentSegment("my user agent", "optional version")
.build();
SearchClient client = new SearchClient(
"<YOUR_APP_ID>",
"<YOUR_API_KEY>",
new ClientOptions().addAlgoliaAgentSegments("my user agent", "optional version")
"<YOUR_INDEX_NAME>",
"<YOUR_API_KEY>",
options
);
```

</TabItem>

<TabItem value="kotlin">

```kotlin
val client = SearchClient(
"<YOUR_APP_ID>",
"<YOUR_API_KEY>",
ClientOptions(
algoliaAgentSegments = listOf(
AgentSegment(
value = "my user agent",
version = "optional version",
)
)
)
)
```

</TabItem>

<TabItem value="dart">

```dart
var client = SearchClient(
appId: 'YOUR_APP_ID>',
apiKey: '<YOUR_API_KEY>',
options: ClientOptions(agentSegments: [
AgentSegment(
value: 'my user agent',
version: 'optional version',
)
]),
);
```

</TabItem>

<TabItem value="go">
// TBD
</TabItem>
</TabsLanguage>

Expand Down Expand Up @@ -226,11 +352,41 @@ $client = new SearchClient($apiWrapper, $config);
import com.algolia.api.SearchClient;

SearchClient client = new SearchClient(
"<YOUR_APP_ID>",
"<YOUR_API_KEY>",
new ClientOptions().setRequester(new MyOwnRequester())
"<YOUR_APP_ID>",
"<YOUR_API_KEY>",
ClientOptions.builder().setRequester(new MyOwnRequester()).build()
);
```

</TabItem>

<TabItem value="kotlin">

```kotlin
val client = SearchClient(
"<YOUR_APP_ID>",
"<YOUR_API_KEY>",
ClientOptions(
requester = MyOwnRequester()
)
)
```

</TabItem>

<TabItem value="dart">

```dart
var client = SearchClient(
appId: "<YOUR_APP_ID>",
apiKey: "<YOUR_API_KEY>",
options: ClientOptions(requester : MyOwnRequester())
);
```

</TabItem>

<TabItem value="go">
// TBD
</TabItem>
</TabsLanguage>
Loading

0 comments on commit 4060bb3

Please sign in to comment.