-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathContractingWorksExt.cs
More file actions
53 lines (48 loc) · 2.1 KB
/
ContractingWorksExt.cs
File metadata and controls
53 lines (48 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System.Text.Json.Serialization;
using System.Text.Json;
using System.Text.Unicode;
using System.Text;
namespace ContractingWorks.WriteApi
{
// We need to extend the NSWag client so that we can set the Authorization header
partial class ContractingWorksClient
{
public required string CwToken { get; init; }
public required ILogger Logger { get; init; }
// Inject the Authorization header when the request is prepared
partial void PrepareRequest(HttpClient client, HttpRequestMessage request, string url)
{
request.Headers.Authorization = new ("Bearer", CwToken);
#if DEBUG
// Bit messy code to dump the JSON doc to the log
// in DEBUG.
// This is just to let you see the JSON doc
// we send to the REST Api.
var content = request.Content;
if (content is not null)
{
var bs = content.ReadAsByteArrayAsync().Result;
var json = UTF8Encoding.UTF8.GetString(bs);
Logger.LogInformation("Request:{json}", json);
}
#endif
}
partial void UpdateJsonSerializerSettings(System.Text.Json.JsonSerializerOptions settings)
{
// It's important to not include null values in JSON doc to the
// REST Api. A null value means that the REST API will try to
// set the value in the table to null.
// A null value typically means "leave the value as is".
// `JsonIgnoreCondition.WhenWritingNull` means we don't include
// null values in the JSON which will leave the value as is
settings.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull ;
// The REST API assumes camelCasing which is the idiom for JavaScript
settings.PropertyNamingPolicy = JsonNamingPolicy.CamelCase ;
#if DEBUG
settings.WriteIndented = true ;
#else
settings.WriteIndented = false ;
#endif
}
}
}