@@ -39,7 +39,7 @@ The next steps describe how to generate a JSON:API client library and use our pa
39
39
5. Add the next line inside the **OpenApiReference** section in your project file:
40
40
41
41
```xml
42
- <Options>/GenerateExceptionClasses:false /AdditionalNamespaceUsages:JsonApiDotNetCore.OpenApi.Client.Exceptions </Options>
42
+ <Options>/GenerateExceptionClasses:false /AdditionalNamespaceUsages:JsonApiDotNetCore.OpenApi.Client</Options>
43
43
```
44
44
45
45
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
77
77
["filter"] = "has(assignedTodoItems)",
78
78
["sort"] = "-lastName",
79
79
["page[size]"] = "5"
80
- });
80
+ }, null );
81
81
82
82
foreach (var person in getResponse.Data)
83
83
{
@@ -166,3 +166,34 @@ For example, the next section puts the generated code in a namespace and generat
166
166
<Options >/GenerateClientInterfaces:true</Options >
167
167
</OpenApiReference >
168
168
```
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