Skip to content

Commit 54c401c

Browse files
verdie-gbkoelman
andauthored
openapi: include headers (#1459)
OpenAPI: include headers Co-authored-by: Bart Koelman <[email protected]>
1 parent 69d91d6 commit 54c401c

File tree

35 files changed

+13022
-248
lines changed

35 files changed

+13022
-248
lines changed

docs/usage/openapi-client.md

+33-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ The next steps describe how to generate a JSON:API client library and use our pa
3939
5. Add the next line inside the **OpenApiReference** section in your project file:
4040
4141
```xml
42-
<Options>/GenerateExceptionClasses:false /AdditionalNamespaceUsages:JsonApiDotNetCore.OpenApi.Client.Exceptions</Options>
42+
<Options>/GenerateExceptionClasses:false /AdditionalNamespaceUsages:JsonApiDotNetCore.OpenApi.Client</Options>
4343
```
4444
4545
6. Add the following glue code to connect our package with your generated code.
@@ -77,7 +77,7 @@ The next steps describe how to generate a JSON:API client library and use our pa
7777
["filter"] = "has(assignedTodoItems)",
7878
["sort"] = "-lastName",
7979
["page[size]"] = "5"
80-
});
80+
}, null);
8181
8282
foreach (var person in getResponse.Data)
8383
{
@@ -166,3 +166,34 @@ For example, the next section puts the generated code in a namespace and generat
166166
<Options>/GenerateClientInterfaces:true</Options>
167167
</OpenApiReference>
168168
```
169+
170+
## Headers and caching
171+
172+
To use [ETags for caching](~/usage/caching.md), NSwag needs extra settings to make response headers accessible.
173+
Specify the following in the `<Options>` element of your project file:
174+
175+
```
176+
/GenerateExceptionClasses:false /WrapResponses:true /GenerateResponseClasses:false /ResponseClass:ApiResponse /AdditionalNamespaceUsages:JsonApiDotNetCore.OpenApi.Client
177+
```
178+
179+
This enables the following code, which is explained below:
180+
181+
```c#
182+
var getResponse = await ApiResponse.TranslateAsync(() => apiClient.GetPersonCollectionAsync(null, null));
183+
string eTag = getResponse.Headers[HeaderNames.ETag].Single();
184+
Console.WriteLine($"Retrieved {getResponse.Result.Data.Count} people.");
185+
186+
// wait some time...
187+
188+
getResponse = await ApiResponse.TranslateAsync(() => apiClient.GetPersonCollectionAsync(null, eTag));
189+
190+
if (getResponse is { StatusCode: (int)HttpStatusCode.NotModified, Result: null })
191+
{
192+
Console.WriteLine("The HTTP response hasn't changed, so no response body was returned.");
193+
}
194+
```
195+
196+
The response of the first API call contains both data and an ETag header, which is a fingerprint of the response.
197+
That ETag gets passed to the second API call. This enables the server to detect if something changed, which optimizes
198+
network usage: no data is sent back, unless is has changed.
199+
If you only want to ask whether data has changed without fetching it, use a HEAD request instead.

0 commit comments

Comments
 (0)