-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathOntraportBaseWrite.cs
95 lines (85 loc) · 4.43 KB
/
OntraportBaseWrite.cs
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System.Text.Json;
using Res = HanumanInstitute.OntraportApi.Properties.Resources;
namespace HanumanInstitute.OntraportApi;
/// <summary>
/// Provides common API endpoints for all objects with update methods.
/// </summary>
/// <typeparam name="T">The data object type deriving from ApiObject.</typeparam>
public abstract class OntraportBaseWrite<T> : OntraportBaseWrite<T, T>
where T : ApiObject
{
public OntraportBaseWrite(OntraportHttpClient apiRequest, string endpointSingular, string endpointPlural, string? primarySearchKey) :
base(apiRequest, endpointSingular, endpointPlural, primarySearchKey)
{ }
}
/// <summary>
/// Provides common API endpoints for all objects with update methods.
/// TOverride can be used to configure Live and Sandbox accounts with a common interface and different Field IDs.
/// </summary>
/// <typeparam name="T">The data object type deriving from ApiObject.</typeparam>
/// <typeparam name="TOverride">A sub-type that overrides T members.</typeparam>
public abstract class OntraportBaseWrite<T, TOverride> : OntraportBaseRead<T, TOverride>, IOntraportBaseWrite<T>
where T : ApiObject
where TOverride : T
{
public string? PrimarySearchKey { get; }
public OntraportBaseWrite(OntraportHttpClient apiRequest, string endpointSingular, string endpointPlural, string? primarySearchKey) :
base(apiRequest, endpointSingular, endpointPlural)
{
PrimarySearchKey = primarySearchKey;
}
/// <summary>
/// Retrieves all the information for an existing object.
/// </summary>
/// <param name="keyValue">The key value of the specific object, usually the name or email.</param>
/// <returns>The selected object.</returns>
public async Task<T?> SelectAsync(string keyValue, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(PrimarySearchKey)) { throw new InvalidOperationException(Res.InvalidMethodForObjectType); }
var result = await SelectAsync(new ApiSearchOptions().AddCondition(PrimarySearchKey!, "=", keyValue), cancellationToken: cancellationToken).ConfigureAwait(false);
return result.FirstOrDefault();
}
/// <summary>
/// This endpoint will add a new object to your database. It can be used for any object type as long as the correct parameters are supplied. This endpoint allows duplication; if you want to avoid duplicates you should merge instead.
/// </summary>
/// <param name="values">Fields to set on the object.</param>
/// <returns>The created object.</returns>
public async Task<T> CreateAsync(object? values = null, CancellationToken cancellationToken = default)
{
var query = new Dictionary<string, object?>().AddObject(values).WriteOverrideFields<T, TOverride>();
var json = await ApiRequest.PostJsonAsync(
EndpointPlural,
query, true, cancellationToken).ConfigureAwait(false);
return await json.RunAndCatchAsync(x => OnParseCreate(x)).ConfigureAwait(false);
}
/// <summary>
/// When overriden in a derived class, allows custom parsing of CreateAsync response.
/// </summary>
/// <param name="json">The JSON data to parse.</param>
/// <returns>A T or object derived from it.</returns>
protected virtual T OnParseCreate(JsonElement json) =>
CreateApiObject(json.JsonData());
/// <summary>
/// Updates an existing object with given data.
/// </summary>
/// <param name="objectId">The ID of the object to update.</param>
/// <param name="values">Fields to set on the object.</param>
/// <returns>A dictionary of updated fields.</returns>
public async Task<T> UpdateAsync(int objectId, object? values = null, CancellationToken cancellationToken = default)
{
var query = new Dictionary<string, object?>
{
{ "id", objectId }
}.AddObject(values).WriteOverrideFields<T, TOverride>();
var json = await ApiRequest.PutJsonAsync(
EndpointPlural, query, cancellationToken).ConfigureAwait(false);
return await json.RunAndCatchAsync(x => OnParseUpdate(x)).ConfigureAwait(false);
}
/// <summary>
/// When overriden in a derived class, allows custom parsing of UpdateAsync response.
/// </summary>
/// <param name="json">The JSON data to parse.</param>
/// <returns>A T or object derived from it.</returns>
protected virtual T OnParseUpdate(JsonElement json) =>
CreateApiObject(json.JsonData().JsonChild("attrs"));
}