Skip to content

Commit eb83e3b

Browse files
shortcutsmorganleroimillotpFluf22
authored
docs: lot of guides (#4355)
Co-authored-by: Morgan Leroi <[email protected]> Co-authored-by: Pierre Millot <[email protected]> Co-authored-by: Thomas Raffray <[email protected]>
1 parent 9bfd097 commit eb83e3b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2983
-154
lines changed

clients/algoliasearch-client-csharp/algoliasearch/Utils/SearchClientExtensions.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,18 @@ public partial interface ISearchClient
176176
/// <inheritdoc cref="SaveObjectsAsync{T}(string, IEnumerable{T}, RequestOptions, CancellationToken)"/>
177177
List<BatchResponse> SaveObjects<T>(string indexName, IEnumerable<T> objects, bool waitForTasks = false, int batchSize = 1000, RequestOptions options = null, CancellationToken cancellationToken = default) where T : class;
178178

179+
/// <summary>
180+
/// Helper: Saves the given array of objects in the given index. The `chunkedBatch` helper is used under the hood, which creates a `batch` requests with at most 1000 objects in it.
181+
/// </summary>
182+
/// <param name="indexName">The index in which to perform the request.</param>
183+
/// <param name="objects">The list of `objects` to store in the given Algolia `indexName`.</param>
184+
/// <param name="options">Add extra http header or query parameters to Algolia.</param>
185+
/// <param name="cancellationToken">Cancellation Token to cancel the request.</param>
186+
/// <typeparam name="T"></typeparam>
187+
Task<List<BatchResponse>> SaveObjectsAsync<T>(string indexName, IEnumerable<T> objects, RequestOptions options, CancellationToken cancellationToken = default) where T : class;
188+
/// <inheritdoc cref="SaveObjectsAsync{T}(string, IEnumerable{T}, RequestOptions, CancellationToken)"/>
189+
List<BatchResponse> SaveObjects<T>(string indexName, IEnumerable<T> objects, RequestOptions options, CancellationToken cancellationToken = default) where T : class;
190+
179191
/// <summary>
180192
/// Helper: Deletes every records for the given objectIDs. The `chunkedBatch` helper is used under the hood, which creates a `batch` requests with at most 1000 objectIDs in it.
181193
/// </summary>
@@ -598,6 +610,19 @@ public List<BatchResponse> SaveObjects<T>(string indexName, IEnumerable<T> objec
598610
CancellationToken cancellationToken = default) where T : class =>
599611
AsyncHelper.RunSync(() => SaveObjectsAsync(indexName, objects, waitForTasks, batchSize, options, cancellationToken));
600612

613+
/// <inheritdoc/>
614+
public async Task<List<BatchResponse>> SaveObjectsAsync<T>(string indexName, IEnumerable<T> objects,
615+
RequestOptions options,
616+
CancellationToken cancellationToken = default) where T : class
617+
{
618+
return await SaveObjectsAsync(indexName, objects, false, 1000, options, cancellationToken).ConfigureAwait(false);
619+
}
620+
621+
/// <inheritdoc/>
622+
public List<BatchResponse> SaveObjects<T>(string indexName, IEnumerable<T> objects, RequestOptions options,
623+
CancellationToken cancellationToken = default) where T : class =>
624+
AsyncHelper.RunSync(() => SaveObjectsAsync(indexName, objects, options, cancellationToken));
625+
601626
/// <inheritdoc/>
602627
public async Task<List<BatchResponse>> DeleteObjectsAsync(string indexName, IEnumerable<String> objectIDs,
603628
bool waitForTasks = false,

generators/src/main/java/com/algolia/codegen/cts/AlgoliaCTSGenerator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ protected Builder<String, Lambda> addMustacheLambdas() {
100100
lambdas.put("escapeDollar", new EscapeDollarLambda());
101101
lambdas.put("escapeQuotes", new EscapeQuotesLambda());
102102
lambdas.put("escapeSlash", new EscapeSlashLambda());
103+
lambdas.put("escapeJSON", new EscapeJSONLambda());
103104
lambdas.put("replaceBacktick", new ReplaceBacktickLambda());
104105

105106
return lambdas;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.algolia.codegen.cts.lambda;
2+
3+
import com.samskivert.mustache.Mustache;
4+
import com.samskivert.mustache.Template;
5+
import java.io.IOException;
6+
import java.io.Writer;
7+
8+
public class EscapeJSONLambda implements Mustache.Lambda {
9+
10+
@Override
11+
public void execute(Template.Fragment fragment, Writer writer) throws IOException {
12+
String text = fragment.execute();
13+
writer.write(text.replace("\\", "\\\\").replace("\"", "\\\""));
14+
}
15+
}

generators/src/main/java/com/algolia/codegen/cts/lambda/EscapeQuotesLambda.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class EscapeQuotesLambda implements Mustache.Lambda {
1010
@Override
1111
public void execute(Template.Fragment fragment, Writer writer) throws IOException {
1212
String text = fragment.execute();
13-
writer.write(text.replace("\"", "\\\""));
13+
// replaces all occurrences of " that are not preceded by a backslash with \"
14+
writer.write(text.replaceAll("(?<!\\\\)\"", "\\\\\""));
1415
}
1516
}

generators/src/main/java/com/algolia/codegen/cts/lambda/EscapeSlashLambda.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class EscapeSlashLambda implements Mustache.Lambda {
1010
@Override
1111
public void execute(Template.Fragment fragment, Writer writer) throws IOException {
1212
String text = fragment.execute();
13-
writer.write(text.replace("\\", "\\\\"));
13+
// replace all backslashes with double backslashes, unless they are followed by a $
14+
writer.write(text.replaceAll("\\\\(?!\\$)", "\\\\\\\\"));
1415
}
1516
}

generators/src/main/java/com/algolia/codegen/cts/tests/ParametersWithDataType.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private Map<String, Object> traverseParams(
145145
}
146146

147147
testOutput.put("key", paramName);
148-
testOutput.put("useAnonymousKey", !paramName.matches("(.*)_[0-9]$") && depth != 0);
148+
testOutput.put("useAnonymousKey", !paramName.matches("(.*)_[0-9]+$") && depth != 0);
149149
testOutput.put("parent", parent);
150150
testOutput.put("isRoot", "".equals(parent));
151151
testOutput.put("objectName", getObjectNameForLanguage(baseType));
@@ -191,7 +191,7 @@ private Map<String, Object> traverseParams(
191191
private Map<String, Object> traverseParamsWithoutSpec(String paramName, Object param, String parent, int depth) throws CTSException {
192192
Map<String, Object> testOutput = createDefaultOutput();
193193
testOutput.put("key", paramName);
194-
testOutput.put("useAnonymousKey", !paramName.matches("(.*)_[0-9]$") && depth != 0);
194+
testOutput.put("useAnonymousKey", !paramName.matches("(.*)_[0-9]+$") && depth != 0);
195195
testOutput.put("parent", parent);
196196
testOutput.put("isRoot", "".equals(parent));
197197
// try to infer the type
@@ -326,7 +326,7 @@ private void handleModel(
326326
} else {
327327
while (current.getItems() != null) {
328328
current = current.getItems();
329-
typeName += "Of" + getTypeName(current);
329+
typeName += "Of" + Helpers.capitalize(getTypeName(current));
330330
isList = true;
331331
}
332332
}
@@ -492,11 +492,16 @@ private void handlePrimitive(Object param, Map<String, Object> testOutput, IJson
492492
}
493493

494494
private String getTypeName(IJsonSchemaValidationProperties param) {
495+
String typeName = param.getDataType();
495496
if (param instanceof CodegenModel parameter) {
496-
return parameter.classname;
497+
typeName = parameter.classname;
497498
}
498499

499-
return param.getDataType();
500+
if (language.equals("scala") && typeName.equals("List")) {
501+
typeName = "Seq";
502+
}
503+
504+
return typeName;
500505
}
501506

502507
private boolean isString(IJsonSchemaValidationProperties param) {
@@ -592,6 +597,8 @@ private String inferDataType(Object param, CodegenParameter spec, Map<String, Ob
592597
spec.setIsArray(true);
593598
// This is just to find the correct path in `handlePrimitive`, but it's not always the
594599
// real type
600+
// FIXME: this set voluntarily the type to string, which will fail
601+
// We need to infer the real type
595602
CodegenProperty baseItems = new CodegenProperty();
596603
baseItems.dataType = "String";
597604
spec.setItems(baseItems);
@@ -702,6 +709,10 @@ private IJsonSchemaValidationProperties findMatchingOneOf(Object param, CodegenM
702709
return maybeMatch;
703710
}
704711

712+
if (model == null || model.interfaceModels == null) {
713+
return maybeMatch;
714+
}
715+
705716
for (CodegenModel oneOf : model.interfaceModels) {
706717
// Somehow the dataType can be in lower case?
707718
if (oneOf.dataType.equalsIgnoreCase(paramType)) {

generators/src/main/java/com/algolia/codegen/cts/tests/TestsClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
206206
}
207207
}
208208
stepOut.put("match", paramsType.enhanceParameter(step.expected.match));
209+
} else if (!step.type.equals("createClient")) {
210+
stepOut.put("useEchoRequester", false);
209211
}
210212
steps.add(stepOut);
211213
}

playground/javascript/node/search.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@ console.log('version', apiClientVersion, 'requests', requests);
1717

1818
async function testSearch() {
1919
try {
20-
const res = await client.search({
21-
requests:
22-
[
23-
{ indexName: 'foo', hitsPerPage: 2 }
24-
]
25-
});
26-
27-
// @ts-ignore
28-
console.log(`[OK]`, res.results[0].hits);
20+
const req = await client.setSettings({
21+
indexName: 'theIndexName',
22+
indexSettings: { distinct: true },
23+
})
24+
25+
26+
console.log(`[OK]`, req);
2927
} catch (e: any) {
3028
// Instance of
3129
if (e instanceof ApiError) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { Server } from 'http';
2+
3+
import type { Express, Request, Response } from 'express';
4+
5+
import { setupServer } from './index.ts';
6+
7+
// Checks that the client sends a different API key after the first request.
8+
function addRoutes(app: Express): void {
9+
app.post('/1/indexes/:indexName/batch', (req: Request, res: Response) => {
10+
res.json({
11+
taskID: 333,
12+
objectIDs: ['1', '2'],
13+
});
14+
});
15+
app.get('/1/indexes/:indexName/task/:taskID', (req, res) => {
16+
res.json({
17+
status: 'published',
18+
});
19+
});
20+
}
21+
22+
export function algoliaMockServer(): Promise<Server> {
23+
return setupServer('algoliaMock', 6686, addRoutes);
24+
}

scripts/cts/testServer/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { createSpinner } from '../../spinners.ts';
77
import type { CTSType } from '../runCts.ts';
88

99
import { expect } from 'chai';
10+
import { algoliaMockServer } from './algoliaMock.ts';
1011
import { apiKeyServer } from './apiKey.ts';
1112
import { benchmarkServer } from './benchmark.ts';
1213
import { chunkWrapperServer } from './chunkWrapper.ts';
@@ -31,6 +32,7 @@ export async function startTestServer(suites: Record<CTSType, boolean>): Promise
3132
chunkWrapperServer(),
3233
waitForApiKeyServer(),
3334
apiKeyServer(),
35+
algoliaMockServer(),
3436
);
3537
}
3638
if (suites.benchmark) {

0 commit comments

Comments
 (0)