Skip to content

Commit faaf924

Browse files
committed
fix: ConfigureAwait all he things for older consumers
1 parent c285d8e commit faaf924

File tree

7 files changed

+572
-390
lines changed

7 files changed

+572
-390
lines changed
+51-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,54 @@
11
// This work is licensed under the terms of the MIT license.
22
// For a copy, see <https://opensource.org/licenses/MIT>.
33

4-
Console.WriteLine("Hello, World!");
4+
using System.Net.Http.Headers;
5+
6+
using IssuuSDK;
7+
using IssuuSDK.Api;
8+
9+
using Microsoft.Extensions.Configuration;
10+
11+
var settings = GetSettings();
12+
var http = CreateHttpClient();
13+
var api = new IssuuApiClient(http, settings);
14+
15+
var drafts = await api.Drafts.GetDraftsAsync().ConfigureAwait(false);
16+
17+
var doc = await api.Publications.GetPublicationAsync("xryumedetev").ConfigureAwait(false);
18+
if (doc.IsSuccess)
19+
{
20+
//var assets = await api.Publications.GetPublicationAssetsAsync("xryumedetev", AssetType.Cover, documentPageNumber: 3).ConfigureAwait(false);
21+
//if (assets.IsSuccess)
22+
//{
23+
24+
//}
25+
26+
var fullScreen = await api.Publications.GetPublicationFullScreenShareAsync(doc.Data!.Slug).ConfigureAwait(false);
27+
var reader = await api.Publications.GetPublicationReaderShareAsync(doc.Data.Slug).ConfigureAwait(false);
28+
var qrCode = await api.Publications.GetPublicationQrCodeAsync(doc.Data.Slug).ConfigureAwait(false);
29+
var embed = await api.Publications.GetPublicationEmbedAsync(doc.Data.Slug).ConfigureAwait(false);
30+
}
31+
32+
IssuuSettings GetSettings()
33+
{
34+
var configuration = new ConfigurationBuilder()
35+
.AddJsonFile("./appsettings.json", optional: false)
36+
.AddJsonFile("./appsettings.env.json", optional: true)
37+
.Build();
38+
39+
IssuuSettings settings = new();
40+
configuration.GetSection(IssuuSettings.ConfigurationSection).Bind(settings);
41+
42+
settings.Validate();
43+
44+
return settings;
45+
}
46+
47+
HttpClient CreateHttpClient()
48+
{
49+
var http = new HttpClient();
50+
51+
http.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
52+
53+
return http;
54+
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"label": "Publications",
3+
"position": 7,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "Working with Issuu published documents."
7+
}
8+
}
9+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
sidebar_position: 1
3+
tags: [Publication, Document]
4+
---
5+
6+
# Getting a list of publications
7+
8+
This page discusses how to read a list of publications on Issuu
9+
10+
### Getting a list of publications
11+
12+
:::tip[Issuu API]
13+
14+
This method corresponds to calling `HTTP GET https://api.issuu.com/v2/publications`.<br />
15+
API documentation: https://api.issuu.com/v2/reference/#get-/publications
16+
17+
:::
18+
19+
To read a list of published documents, we can use the `Publications.GetPublicationsAsync` operation. This operation will return a page of a set of documents.
20+
21+
```csharp
22+
public async Task ReadPublishedDocuments(int page = 1, int size = 10, CancellationToken ct)
23+
{
24+
var response = await _client.Publications.GetPublicationsAsync(page, size ct);
25+
if (response.IsSuccess && response.Data is { Length: >0 })
26+
{
27+
var documents = response.Data;
28+
// Do something with the documents.
29+
}
30+
}
31+
```
32+
33+
If the response is successful, the `Meta` property should be populated with additional paging information. You can use this to determine if you can read any other pages of information.
34+
35+
:::tip[Paging Defaults]
36+
37+
The following defaults are used for `page` and `size` if you do not provide them:
38+
39+
| Parameter | Type | Default |
40+
|---|---|---|
41+
| `page` | `int` | 1 |
42+
| `size` | `int` | 10 |
43+
44+
:::

docs/docs/requests-and-responses.md

+113
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,119 @@ You'd still need to use `IssuuRequest` for sending payload data where that paylo
1919

2020
:::
2121

22+
### Mapping HTTP responses
23+
24+
A mapping delegate, `MapResponse<TResponse>` is provided for passing mapping functions to the `FetchAsync` code API. This is defined as:
25+
26+
```csharp
27+
public delegate Task<MappedResponse<TResponse>> MapResponse<TResponse>(
28+
HttpResponseMessage response, CancellationToken cancellationToken);
29+
```
30+
31+
The mapping delegate must accept a HTTP response message, and return a `MappedResponse<TResponse>`, which will contain the result data, and any additional metadata and links.
32+
33+
Example:
34+
35+
```csharp
36+
async Task<MappedResult<Document[]>> MapAsync(HttpResponse response, CancellationToken ct)
37+
{
38+
DataContainer<TResponse>? data = default;
39+
Meta? meta = default;
40+
Dictionary<string, string>? links = default;
41+
if (response.Content is not null)
42+
{
43+
data = await response.Content.ReadFromJsonAsync<DataContainer<TResponse>>(
44+
_deserializerOptions, cancellationToken)
45+
.ConfigureAwait(false);
46+
47+
if (data is not null)
48+
{
49+
if (data.Count.HasValue && data.PageSize.HasValue)
50+
{
51+
meta = new()
52+
{
53+
PageSize = data.PageSize.Value,
54+
TotalItems = data.Count.Value,
55+
TotalPages = (int)Math.Ceiling(data.Count.Value / (double)data.PageSize.Value)
56+
};
57+
}
58+
59+
if (data.Links is { Count: > 0 })
60+
{
61+
links = new();
62+
foreach (var link in data.Links)
63+
{
64+
links.Add(link.Key, link.Value.Href);
65+
}
66+
}
67+
}
68+
}
69+
}
70+
71+
var request = new IssuuRequest(...);
72+
await client.FetchAsync<Document>(
73+
request,
74+
$"/drafts",
75+
MapAsync,
76+
cancellationToken);
77+
)
78+
```
79+
80+
:::tip[Only Success Results]
81+
82+
You do not need to worry about mapping an error response from the Issuu API, as the IssuuSDK will handle this automatically. When your mapping delegate is called, this is for a success HTTP status code
83+
84+
:::
85+
86+
Internally, the IssuuSDK makes use of this approach combined with specific container types which can be deserialized from the JSON responses.
87+
88+
#### `DataContainer<TResult>` container type
89+
90+
This container type is used when returning set operations, where `TResult` is the array of the intended type:
91+
92+
| Property | Type | Notes |
93+
|---|---|---|
94+
| `Count` | `int?` | The total number of items |
95+
| `PageSize` | `int?` | The size of the page |
96+
| `Results` | `TData?` | The data |
97+
| `Links` | `Dictionary<string, LinkContainer>?` | The set of links for the resource |
98+
99+
For example, when using the `Drafts.GetDraftsAsync(...)` operation, internally a `DataContainer<Document[]>` is used which returns a set of documents.
100+
101+
:::info[Single Results]
102+
103+
The `DataContainer<T>` type is only used for results that return sets of data. For individual result instances, such as a single document, the JSON result is directly deserialized, not in a wrapping type.
104+
105+
:::
106+
107+
#### `LinkContainer` container type
108+
109+
This container type is used to deserialize a link:
110+
111+
This container type is used to deserialzie a specific error message:
112+
113+
| Property | Type | Notes |
114+
|---|---|---|
115+
| `Href` | `string` | The error message |
116+
117+
#### `ErrorContainer` container type
118+
119+
This container type is used to deserialize an error response:
120+
121+
| Property | Type | Notes |
122+
|---|---|---|
123+
| `Fields` | `Dictionary<string, ErrorMessageContainer>?` | The set of field errors |
124+
| `Details` | `Dictionary<string, ErrorMessageContainer>?` | The set of specific error details |
125+
| `Message` | `string` | The error message |
126+
127+
#### `ErrorMessageContainer` container type
128+
129+
This container type is used to deserialzie a specific error message:
130+
131+
| Property | Type | Notes |
132+
|---|---|---|
133+
| `Message` | `string` | The error message |
134+
22135
### HTTP GET example - returning a single resource
23136

24137
```csharp

libs/IssuuSDK/Api/DraftOperations.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public async Task<IssuuResponse<Document>> CreateDraftAsync(
137137
{
138138
var request = new IssuuRequest<Draft>(HttpMethod.Post, path, draft);
139139

140-
return await client.FetchSingleAsync<Draft, Document>(request, cancellationToken);
140+
return await client.FetchSingleAsync<Draft, Document>(request, cancellationToken).ConfigureAwait(false);
141141
}
142142

143143
public async Task<IssuuResponse> DeleteDraftAsync(
@@ -148,7 +148,7 @@ public async Task<IssuuResponse> DeleteDraftAsync(
148148

149149
var request = new IssuuRequest(HttpMethod.Delete, path + $"/{slug}");
150150

151-
return await client.SendAsync(request, cancellationToken);
151+
return await client.SendAsync(request, cancellationToken).ConfigureAwait(false);
152152
}
153153

154154
public async Task<IssuuResponse<Document>> GetDraftAsync(
@@ -159,7 +159,7 @@ public async Task<IssuuResponse<Document>> GetDraftAsync(
159159

160160
var request = new IssuuRequest(HttpMethod.Get, path + $"/{slug}");
161161

162-
return await client.FetchSingleAsync<Document>(request, cancellationToken);
162+
return await client.FetchSingleAsync<Document>(request, cancellationToken).ConfigureAwait(false);
163163
}
164164

165165
public async Task<IssuuResponse<Document[]>> GetDraftsAsync(
@@ -169,7 +169,7 @@ public async Task<IssuuResponse<Document[]>> GetDraftsAsync(
169169
{
170170
var request = new IssuuRequest(HttpMethod.Get, path, page: page, size: size);
171171

172-
return await client.FetchManyAsync<Document[]>(request, cancellationToken);
172+
return await client.FetchManyAsync<Document[]>(request, cancellationToken).ConfigureAwait(false);
173173
}
174174

175175
public async Task<IssuuResponse<PublishResult>> PublishDraftAsync(
@@ -192,7 +192,7 @@ public async Task<IssuuResponse<PublishResult>> PublishDraftAsync(
192192
DesiredName = desiredName
193193
});
194194

195-
return await client.FetchSingleAsync<PublishRequest, PublishResult>(request, cancellationToken);
195+
return await client.FetchSingleAsync<PublishRequest, PublishResult>(request, cancellationToken).ConfigureAwait(false);
196196
}
197197

198198
public async Task<IssuuResponse<Document>> UpdateDraftAsync(
@@ -204,7 +204,7 @@ public async Task<IssuuResponse<Document>> UpdateDraftAsync(
204204

205205
var request = new IssuuRequest<Draft>(new HttpMethod("PATCH"), path + $"/{slug}", draft);
206206

207-
return await client.FetchSingleAsync<Draft, Document>(request, cancellationToken);
207+
return await client.FetchSingleAsync<Draft, Document>(request, cancellationToken).ConfigureAwait(false);
208208
}
209209

210210
public async Task<IssuuResponse<Document>> UploadDocumentContentAsync(
@@ -227,7 +227,7 @@ public async Task<IssuuResponse<Document>> UploadDocumentContentAsync(
227227
fileName: fileName,
228228
filePath: filePath);
229229

230-
return await client.FetchSingleAsync<Document>(request, cancellationToken);
230+
return await client.FetchSingleAsync<Document>(request, cancellationToken).ConfigureAwait(false);
231231
}
232232

233233
public async Task<IssuuResponse<Document>> UploadDocumentContentAsync(
@@ -251,6 +251,6 @@ public async Task<IssuuResponse<Document>> UploadDocumentContentAsync(
251251
fileName: fileName,
252252
fileStream: fileStream);
253253

254-
return await client.FetchSingleAsync<Document>(request, cancellationToken);
254+
return await client.FetchSingleAsync<Document>(request, cancellationToken).ConfigureAwait(false);
255255
}
256256
}

libs/IssuuSDK/Api/PublicationOperations.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public async Task<IssuuResponse> DeletePublicationAsync(
137137

138138
var request = new IssuuRequest(HttpMethod.Delete, path + $"/{slug}");
139139

140-
return await client.SendAsync(request, cancellationToken);
140+
return await client.SendAsync(request, cancellationToken).ConfigureAwait(false);
141141
}
142142

143143
public async Task<IssuuResponse<Document>> GetPublicationAsync(
@@ -148,7 +148,7 @@ public async Task<IssuuResponse<Document>> GetPublicationAsync(
148148

149149
var request = new IssuuRequest(HttpMethod.Get, path + $"/{slug}");
150150

151-
return await client.FetchSingleAsync<Document>(request, cancellationToken);
151+
return await client.FetchSingleAsync<Document>(request, cancellationToken).ConfigureAwait(false);
152152
}
153153

154154
public async Task<IssuuResponse<AssetResult[]>> GetPublicationAssetsAsync(
@@ -183,10 +183,10 @@ public async Task<IssuuResponse<AssetResult[]>> GetPublicationAssetsAsync(
183183

184184
if (assetType != AssetType.Cover)
185185
{
186-
return await client.FetchManyAsync<AssetResult[]>(request, cancellationToken);
186+
return await client.FetchManyAsync<AssetResult[]>(request, cancellationToken).ConfigureAwait(false);
187187
}
188188

189-
var result = await client.FetchManyAsync<AssetResult[][]>(request, cancellationToken);
189+
var result = await client.FetchManyAsync<AssetResult[][]>(request, cancellationToken).ConfigureAwait(false);
190190

191191
var data = result.Data is { Length: > 0 } ? result.Data[0] : null;
192192

@@ -230,7 +230,7 @@ public async Task<IssuuResponse<EmbedResult>> GetPublicationEmbedAsync(
230230
path + $"/{slug}/embed",
231231
query: query);
232232

233-
return await client.FetchSingleAsync<EmbedResult>(request, cancellationToken);
233+
return await client.FetchSingleAsync<EmbedResult>(request, cancellationToken).ConfigureAwait(false);
234234
}
235235

236236
public async Task<IssuuResponse<ShareResult>> GetPublicationFullScreenShareAsync(
@@ -245,7 +245,7 @@ public async Task<IssuuResponse<ShareResult>> GetPublicationFullScreenShareAsync
245245
path + $"/{slug}/fullscreen",
246246
settings ?? new());
247247

248-
return await client.FetchSingleAsync<DisplaySettings, ShareResult>(request, cancellationToken);
248+
return await client.FetchSingleAsync<DisplaySettings, ShareResult>(request, cancellationToken).ConfigureAwait(false);
249249
}
250250

251251
public async Task<IssuuResponse<ShareResult>> GetPublicationReaderShareAsync(
@@ -258,7 +258,7 @@ public async Task<IssuuResponse<ShareResult>> GetPublicationReaderShareAsync(
258258
HttpMethod.Get,
259259
path + $"/{slug}/reader");
260260

261-
return await client.FetchSingleAsync<ShareResult>(request, cancellationToken);
261+
return await client.FetchSingleAsync<ShareResult>(request, cancellationToken).ConfigureAwait(false);
262262
}
263263

264264
public async Task<IssuuResponse<QrShareResult>> GetPublicationQrCodeAsync(
@@ -273,7 +273,7 @@ public async Task<IssuuResponse<QrShareResult>> GetPublicationQrCodeAsync(
273273
path + $"/{slug}/qrcode",
274274
settings ?? new());
275275

276-
return await client.FetchSingleAsync<QrDisplaySettings, QrShareResult>(request, cancellationToken);
276+
return await client.FetchSingleAsync<QrDisplaySettings, QrShareResult>(request, cancellationToken).ConfigureAwait(false);
277277
}
278278

279279
public async Task<IssuuResponse<Document[]>> GetPublicationsAsync(
@@ -283,7 +283,7 @@ public async Task<IssuuResponse<Document[]>> GetPublicationsAsync(
283283
{
284284
var request = new IssuuRequest(HttpMethod.Get, path, page: page, size: size);
285285

286-
return await client.FetchManyAsync<Document[]>(request, cancellationToken);
286+
return await client.FetchManyAsync<Document[]>(request, cancellationToken).ConfigureAwait(false);
287287
}
288288
}
289289

0 commit comments

Comments
 (0)