Skip to content

Commit 747c39f

Browse files
committed
fix: everything else
1 parent 12904fa commit 747c39f

File tree

32 files changed

+152
-87
lines changed

32 files changed

+152
-87
lines changed

clients/algoliasearch-client-java/algoliasearch/src/main/java/com/algolia/config/Requester.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,30 @@
44
import java.io.Closeable;
55

66
/**
7-
* Represents a mechanism for executing HTTP requests and deserializing
8-
* responses. It provides
9-
* methods for making requests and returning the desired object representation.
10-
* Implementations of
7+
* Represents a mechanism for executing HTTP requests and deserializing responses. It provides
8+
* methods for making requests and returning the desired object representation. Implementations of
119
* this interface should ensure proper resource management.
1210
*/
1311
public interface Requester extends Closeable {
1412
/**
15-
* Executes an HTTP request and deserializes the response into a specified Java
16-
* type.
13+
* Executes an HTTP request and deserializes the response into a specified Java type.
1714
*
18-
* @param <T> The type of the returned object.
19-
* @param httpRequest The HTTP request to be executed.
15+
* @param <T> The type of the returned object.
16+
* @param httpRequest The HTTP request to be executed.
2017
* @param requestOptions Optional request options.
21-
* @param returnType The class of the response.
22-
* @param innerType The inner class type if the response is a container
23-
* type.
18+
* @param returnType The class of the response.
19+
* @param innerType The inner class type if the response is a container type.
2420
* @return The deserialized response.
2521
*/
2622
<T> T execute(HttpRequest httpRequest, RequestOptions requestOptions, Class<?> returnType, Class<?> innerType);
2723

2824
/**
29-
* Executes an HTTP request and deserializes the response into a specified type
30-
* reference.
25+
* Executes an HTTP request and deserializes the response into a specified type reference.
3126
*
32-
* @param <T> The type of the returned object.
33-
* @param httpRequest The HTTP request to be executed.
27+
* @param <T> The type of the returned object.
28+
* @param httpRequest The HTTP request to be executed.
3429
* @param requestOptions Optional request options.
35-
* @param returnType The type reference of the response.
30+
* @param returnType The type reference of the response.
3631
* @return The deserialized response.
3732
*/
3833
<T> T execute(HttpRequest httpRequest, RequestOptions requestOptions, TypeReference<?> returnType);

clients/algoliasearch-client-java/algoliasearch/src/main/java/com/algolia/internal/HttpRequester.java

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@
2020
import okio.BufferedSink;
2121

2222
/**
23-
* HttpRequester is responsible for making HTTP requests using the OkHttp
24-
* client. It provides a
25-
* mechanism for request serialization and deserialization using a given
26-
* {@link JsonSerializer}.
23+
* HttpRequester is responsible for making HTTP requests using the OkHttp client. It provides a
24+
* mechanism for request serialization and deserialization using a given {@link JsonSerializer}.
2725
*/
2826
public final class HttpRequester implements Requester {
2927

@@ -35,11 +33,11 @@ public final class HttpRequester implements Requester {
3533
/** Private constructor initialized using the builder pattern. */
3634
private HttpRequester(Builder builder, ClientConfig config) {
3735
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
38-
.connectTimeout(config.getConnectTimeout())
39-
.readTimeout(config.getReadTimeout())
40-
.writeTimeout(config.getWriteTimeout())
41-
.addInterceptor(new HeaderInterceptor(config.getDefaultHeaders()))
42-
.addNetworkInterceptor(new LogInterceptor(config.getLogger(), config.getLogLevel()));
36+
.connectTimeout(config.getConnectTimeout())
37+
.readTimeout(config.getReadTimeout())
38+
.writeTimeout(config.getWriteTimeout())
39+
.addInterceptor(new HeaderInterceptor(config.getDefaultHeaders()))
40+
.addNetworkInterceptor(new LogInterceptor(config.getLogger(), config.getLogLevel()));
4341
builder.interceptors.forEach(clientBuilder::addInterceptor);
4442
builder.networkInterceptors.forEach(clientBuilder::addNetworkInterceptor);
4543
if (config.getCompressionType() == CompressionType.GZIP) {
@@ -53,8 +51,7 @@ private HttpRequester(Builder builder, ClientConfig config) {
5351
}
5452

5553
@Override
56-
public <T> T execute(HttpRequest httpRequest, RequestOptions requestOptions, Class<?> returnType,
57-
Class<?> innerType) {
54+
public <T> T execute(HttpRequest httpRequest, RequestOptions requestOptions, Class<?> returnType, Class<?> innerType) {
5855
return execute(httpRequest, requestOptions, serializer.getJavaType(returnType, innerType));
5956
}
6057

@@ -80,8 +77,7 @@ private <T> T execute(@Nonnull HttpRequest httpRequest, RequestOptions requestOp
8077
RequestBody requestBody = createRequestBody(httpRequest);
8178

8279
// Build the HTTP request.
83-
Request request = new Request.Builder().url(url).headers(headers).method(httpRequest.getMethod(), requestBody)
84-
.build();
80+
Request request = new Request.Builder().url(url).headers(headers).method(httpRequest.getMethod(), requestBody).build();
8581

8682
// Get or adjust the HTTP client according to request options.
8783
OkHttpClient client = getOkHttpClient(requestOptions);
@@ -110,9 +106,9 @@ private <T> T execute(@Nonnull HttpRequest httpRequest, RequestOptions requestOp
110106
@Nonnull
111107
private static HttpUrl createHttpUrl(@Nonnull HttpRequest request, RequestOptions requestOptions) {
112108
HttpUrl.Builder urlBuilder = new HttpUrl.Builder()
113-
.scheme("https")
114-
.host("algolia.com") // will be overridden by the retry strategy
115-
.encodedPath(request.getPath());
109+
.scheme("https")
110+
.host("algolia.com") // will be overridden by the retry strategy
111+
.encodedPath(request.getPath());
116112
request.getQueryParameters().forEach(urlBuilder::addEncodedQueryParameter);
117113
if (requestOptions != null) {
118114
requestOptions.getQueryParameters().forEach(urlBuilder::addEncodedQueryParameter);
@@ -160,15 +156,11 @@ private Headers createHeaders(@Nonnull HttpRequest request, RequestOptions reque
160156
return builder.build();
161157
}
162158

163-
/**
164-
* Returns a suitable OkHttpClient instance based on the provided request
165-
* options.
166-
*/
159+
/** Returns a suitable OkHttpClient instance based on the provided request options. */
167160
@Nonnull
168161
private OkHttpClient getOkHttpClient(RequestOptions requestOptions) {
169162
// Return the default client if no request options are provided.
170-
if (requestOptions == null)
171-
return httpClient;
163+
if (requestOptions == null) return httpClient;
172164

173165
// Create a new client builder from the default client and adjust timeouts if
174166
// provided.
@@ -184,8 +176,7 @@ private OkHttpClient getOkHttpClient(RequestOptions requestOptions) {
184176

185177
@Override
186178
public void close() throws IOException {
187-
if (isClosed.get())
188-
return;
179+
if (isClosed.get()) return;
189180
httpClient.dispatcher().executorService().shutdown();
190181
httpClient.connectionPool().evictAll();
191182
if (httpClient.cache() != null) {
@@ -195,8 +186,7 @@ public void close() throws IOException {
195186
}
196187

197188
/**
198-
* The Builder class for HttpRequester. It provides a mechanism for constructing
199-
* an instance of
189+
* The Builder class for HttpRequester. It provides a mechanism for constructing an instance of
200190
* HttpRequester with customized configurations.
201191
*/
202192
public static class Builder {

clients/algoliasearch-client-scala/src/main/scala/algoliasearch/internal/HttpRequester.scala

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@ private[algoliasearch] class HttpRequester private (
4545
}
4646

4747
override def setAlgoliaApiKey(apiKey: String): Unit = {
48-
httpClient.newBuilder().addInterceptor(new Interceptor {
49-
override def intercept(chain: Interceptor.Chain): Response = {
50-
val request = chain.request().newBuilder().addHeader("X-Algolia-API-Key", apiKey).build()
51-
chain.proceed(request)
52-
}
53-
})
48+
httpClient
49+
.newBuilder()
50+
.addInterceptor(new Interceptor {
51+
override def intercept(chain: Interceptor.Chain): Response = {
52+
val request = chain.request().newBuilder().addHeader("X-Algolia-API-Key", apiKey).build()
53+
chain.proceed(request)
54+
}
55+
})
5456
}
5557

5658
private val jsonSerializer = JsonSerializer()(builder.formats)

clients/algoliasearch-client-swift/Sources/Core/Networking/Transporter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ open class Transporter {
4242

4343
self.requestBuilder = requestBuilder
4444
}
45-
45+
4646
public func setAlgoliaApiKey(apiKey: String) {
4747
self.configuration.defaultHeaders?.updateValue(apiKey, forKey: "X-Algolia-API-Key")
4848
}

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.Map;
1111
import org.openapitools.codegen.CodegenModel;
1212
import org.openapitools.codegen.CodegenOperation;
13+
import org.openapitools.codegen.CodegenResponse;
1314
import org.openapitools.codegen.SupportingFile;
1415

1516
public class TestsClient extends TestsGenerator {
@@ -114,9 +115,25 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
114115
stepOut.put("returnsBoolean", ope.returnType.equals("Boolean")); // ruby requires a ? for boolean functions.
115116
}
116117

118+
stepOut.put("isHelper", (boolean) ope.vendorExtensions.getOrDefault("x-helper", false));
119+
120+
// default to true because most api calls are asynchronous
121+
stepOut.put("isAsync", (boolean) ope.vendorExtensions.getOrDefault("x-asynchronous-helper", true));
122+
// Determines whether the endpoint is expected to return a response payload
123+
// deserialized and therefore a variable to store it into.
124+
stepOut.put("hasResponsePayload", true);
125+
126+
for (CodegenResponse response : ope.responses) {
127+
if (response.code.equals("204")) {
128+
stepOut.put("hasResponsePayload", false);
129+
}
130+
}
131+
117132
// set on testOut because we need to wrap everything for java.
118133
testOut.put("isHelper", (boolean) ope.vendorExtensions.getOrDefault("x-helper", false));
119-
testOut.put("isAsync", (boolean) ope.vendorExtensions.getOrDefault("x-asynchronous-helper", true)); // default to true because most api calls are asynchronous
134+
135+
// default to true because most api calls are asynchronous
136+
testOut.put("isAsync", (boolean) ope.vendorExtensions.getOrDefault("x-asynchronous-helper", true));
120137
}
121138

122139
stepOut.put("method", step.method);

scripts/cts/testServer/apiKey.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type { Server } from 'http';
2+
3+
import { expect } from 'chai';
4+
import type { Express, Request, Response } from 'express';
5+
6+
import { setupServer } from '.';
7+
8+
function addRoutes(app: Express): void {
9+
app.get('/check-api-key/1', (req: Request, res: Response) => {
10+
const headerName = 'x-algolia-api-key';
11+
12+
// eslint-disable-next-line no-unused-expressions
13+
expect(headerName in req.headers).to.be.true;
14+
15+
const headerAPIKeyValue = req.headers[headerName];
16+
expect(headerAPIKeyValue).to.equal('test-api-key');
17+
18+
res.status(200).json({ headerAPIKeyValue });
19+
});
20+
app.get('/check-api-key/2', (req: Request, res: Response) => {
21+
const headerName = 'x-algolia-api-key';
22+
23+
// eslint-disable-next-line no-unused-expressions
24+
expect(headerName in req.headers).to.be.true;
25+
26+
const headerAPIKeyValue = req.headers[headerName];
27+
expect(headerAPIKeyValue).to.equal('updated-api-key');
28+
29+
res.status(200).json({ headerAPIKeyValue });
30+
});
31+
}
32+
33+
export function apiKeyServer(): Promise<Server> {
34+
return setupServer('apiKey', 6683, addRoutes);
35+
}

scripts/cts/testServer/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { Express } from 'express';
66
import { createSpinner } from '../../spinners';
77
import type { CTSType } from '../runCts';
88

9+
import { apiKeyServer } from './apiKey';
910
import { benchmarkServer } from './benchmark';
1011
import { chunkWrapperServer } from './chunkWrapper';
1112
import { gzipServer } from './gzip';
@@ -24,6 +25,7 @@ export async function startTestServer(suites: Record<CTSType, boolean>): Promise
2425
replaceAllObjectsServer(),
2526
chunkWrapperServer(),
2627
waitForApiKeyServer(),
28+
apiKeyServer(),
2729
);
2830
}
2931
if (suites.benchmark) {
@@ -61,7 +63,7 @@ export async function setupServer(name: string, port: number, addRoutes: (app: E
6163
});
6264

6365
// catch all error handler
64-
app.use((err, req, res, _) => {
66+
app.use((err, _req, res, _) => {
6567
// eslint-disable-next-line no-console
6668
console.error(err.message);
6769
res.status(500).send({ message: err.message });

scripts/cts/testServer/waitFor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@ function addRoutes(app: Express): void {
114114
}
115115
});
116116

117-
app.get('/1/indexes/:indexName/task/:taskID', (req, res) => {
117+
app.get('/1/indexes/:indexName/task/:taskID', (_req, res) => {
118118
res.status(200).json({
119119
status: 'published',
120120
});
121121
});
122122

123-
app.get('/1/task/:taskID', (req, res) => {
123+
app.get('/1/task/:taskID', (_req, res) => {
124124
res.status(200).json({
125125
status: 'published',
126126
});

specs/common/helpers/setAlgoliaApiKey.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ method:
1616
schema:
1717
type: string
1818
responses:
19-
'200':
20-
description: OK
21-
content:
22-
application/json:
23-
schema:
24-
type: string
19+
'204':
20+
description: No content.
2521
'400':
2622
$ref: '../../common/responses/IndexNotFound.yml'
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
{{^useEchoRequester}}var res = {{/useEchoRequester}}{{> tests/method}};
1+
{{^useEchoRequester}}{{#hasResponsePayload}}var res = {{/hasResponsePayload}}{{/useEchoRequester}}{{> tests/method}};
22
{{#useEchoRequester}}EchoResponse result = _echo.LastResponse;{{/useEchoRequester}}

templates/csharp/tests/client/tests.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
var client = new {{client}}(new {{clientPrefix}}Config("appId", "apiKey"{{#hasRegionalHost}},"{{defaultRegion}}"{{/hasRegionalHost}}), _echo);
77
{{/autoCreateClient}}
88
{{#steps}}
9+
{
910
{{#times}}
1011
for (int i = 0; i < {{.}}; i++) {
1112
{{/times}}
@@ -39,6 +40,7 @@
3940
{{#times}}
4041
}
4142
{{/times}}
43+
}
4244
{{/steps}}
4345
}
4446
{{/tests}}

templates/dart/tests/client/client.mustache

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ void main() {
2020
options: ClientOptions(requester: requester),
2121
);
2222
{{/autoCreateClient}}
23-
{{#steps}}
23+
{{#steps}}
24+
{
2425
{{#isError}}
2526
await expectError(
2627
'{{{expectedError}}}',
@@ -45,6 +46,7 @@ void main() {
4546
{{/match}}
4647
{{#dynamicTemplate}}{{/dynamicTemplate}}
4748
{{/isError}}
49+
}
4850
{{/steps}}
4951
}
5052
);

templates/dart/tests/client/method.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
try {
2-
final res = await client.{{method}}(
2+
{{#hasResponsePayload}}final res = {{/hasResponsePayload}}await client.{{method}}(
33
{{#parametersWithDataType}}
44
{{> tests/request_param}}
55
{{/parametersWithDataType}}

templates/go/client.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (c *APIClient) GetConfiguration() *{{#lambda.titlecase}}{{#lambda.camelcase
141141
}
142142

143143
// Allow update of stored API key used to authenticate requests.
144-
func (c *APIClient) SetApiKey(apiKey string) {
144+
func (c *APIClient) SetAlgoliaApiKey(apiKey string) {
145145
c.cfg.ApiKey = apiKey
146146
}
147147

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
res, err = {{> tests/method}}
1+
{{#hasResponsePayload}}res, err = {{/hasResponsePayload}}{{> tests/method}}

templates/go/tests/client/tests.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func Test{{#lambda.titlecase}}{{clientPrefix}}{{testType}}{{/lambda.titlecase}}{
1515
{{/autoCreateClient}}
1616
_ = echo
1717
{{#steps}}
18+
{
1819
{{#times}}
1920
for i := 0; i < {{.}}; i++ {
2021
{{/times}}
@@ -54,6 +55,7 @@ func Test{{#lambda.titlecase}}{{clientPrefix}}{{testType}}{{/lambda.titlecase}}{
5455
{{#times}}
5556
}
5657
{{/times}}
58+
}
5759
{{/steps}}
5860
}
5961

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
{{^useEchoRequester}}{{{returnType}}} res = {{/useEchoRequester}}{{> tests/method}};
1+
{{^useEchoRequester}}{{#hasResponsePayload}}{{{returnType}}} res = {{/hasResponsePayload}}{{/useEchoRequester}}{{> tests/method}};
22
{{#useEchoRequester}}EchoResponse result = echo.getLastResponse();{{/useEchoRequester}}

0 commit comments

Comments
 (0)