/
JustGivingApiClient2.cs
287 lines (254 loc) · 11 KB
/
JustGivingApiClient2.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using System;
using JustGiving.Api.Sdk2.Clients.Account;
using JustGiving.Api.Sdk2.Clients.Charity;
using JustGiving.Api.Sdk2.Clients.Countries;
using JustGiving.Api.Sdk2.Configuration;
using JustGiving.Api.Sdk2.Configuration.Logging;
using JustGiving.Api.Sdk2.Http;
using JustGiving.Api.Sdk2.Logging;
using JustGiving.Api.Sdk2.Security;
using JustGiving.Api.Sdk2.Security.Basic;
using JustGiving.Api.Sdk2.Security.OAuth;
using RestSharp;
namespace JustGiving.Api.Sdk2
{
/// <summary>
/// Main entry point into the SDK. Instantiate this class using one of the constructors to begin using the JustGiving API.
/// </summary>
public class JustGivingApiClient2
{
private AccountClient _accounts;
private CharityClient _charities;
private CountryClient _countries;
private IRestClient _restClient;
private ApiRequestLogger _logger;
private readonly ClientOptions _options;
private ILogProvider _logProvider;
/// <summary>
/// Creates an Api client which uses an OAuth2 access token for authorization. This is the preferred method of authenticating, as it offers the best security and will work with all JustGiving user accounts.
/// </summary>
/// <param name="appId">The Application ID you received when you registered your app on the JustGiving developer portal.</param>
/// <param name="applicationKey">The Application Key you created for your app on the JustGiving developer portal. You must include it here for your app to work.</param>
/// <param name="oauthAccessToken">A valid OAuth2 access token issued by identity.justgiving.com</param>
public JustGivingApiClient2(string appId, string applicationKey, OAuthAccessToken oauthAccessToken)
{
if (string.IsNullOrWhiteSpace(appId))
{
throw new ArgumentException("You must provide an appId");
}
if (string.IsNullOrWhiteSpace(applicationKey))
{
throw new ArgumentException("You must provide a Application Key when using OAuth. To generate an Application Key for your app, log in to the JustGiving developer portal.");
}
_options = new ClientOptions(appId, applicationKey)
{
AuthorizationMode = AuthorizationMode.OAuth,
Endpoint = Endpoints.Production,
OAuthAccessToken = oauthAccessToken,
LoggingOptions = LoggingOptions.Default
};
}
/// <summary>
/// Creates an Api client which uses a username and password for Basic authorization. This method carries security risks and will not be available to JustGiving users with different user account types (such as Facebook).
/// </summary>
/// <param name="appId">The Application ID you received when you registered your app on the JustGiving developer portal.</param>
/// <param name="basicAuthCredential">The username and password of a registered JustGiving user.</param>
public JustGivingApiClient2(string appId, BasicCredential basicAuthCredential)
{
if (string.IsNullOrWhiteSpace(appId))
{
throw new ArgumentException("You must provide an appId");
}
if (basicAuthCredential == null)
{
throw new ArgumentException("You must provide a credential");
}
_options = new ClientOptions(appId)
{
AuthorizationMode = AuthorizationMode.Basic,
Endpoint = Endpoints.Production,
BasicAuthSettings = basicAuthCredential,
LoggingOptions = LoggingOptions.Default
};
}
/// <summary>
/// Creates an Api client which uses a username and password for Basic authorization. This method carries security risks and will not be available to JustGiving users with different user account types (such as Facebook).
/// </summary>
/// <param name="appId">The Application ID you received when you registered your app on the JustGiving developer portal.</param>
/// <param name="applicationKey">If you created a Application Key for this app in the Developer Portal, you must include it here for your app to work.</param>
/// <param name="basicAuthCredential">The username and password of a registered JustGiving user.</param>
public JustGivingApiClient2(string appId, string applicationKey, BasicCredential basicAuthCredential)
{
if (string.IsNullOrWhiteSpace(appId))
{
throw new ArgumentException("You must provide an appId");
}
if (string.IsNullOrWhiteSpace(applicationKey))
{
throw new ArgumentException("Application Key cannot be empty. If you have not created an Application Key for your application, use the overloaded version of this constructor.");
}
if (basicAuthCredential == null)
{
throw new ArgumentException("You must provide a credential");
}
_options = new ClientOptions(appId, applicationKey)
{
AuthorizationMode = AuthorizationMode.Basic,
Endpoint = Endpoints.Production,
BasicAuthSettings = basicAuthCredential,
LoggingOptions = LoggingOptions.Default
};
}
/// <summary>
/// Creates an anonymous Api client using common default settings. This client will not be able to access protected Api resources which require authentication.
/// </summary>
/// <param name="appId">The Application ID you received when you registered your app on the JustGiving developer portal.</param>
public JustGivingApiClient2(string appId)
{
if (string.IsNullOrWhiteSpace(appId))
{
throw new ArgumentException("You must provide an appId");
}
_options = new ClientOptions(appId)
{
AuthorizationMode = AuthorizationMode.Anonymous,
Endpoint = Endpoints.Production,
LoggingOptions = LoggingOptions.Default
};
}
/// <summary>
/// Creates an anonymous Api client using common default settings. This client will not be able to access protected Api resources which require authentication.
/// </summary>
/// <param name="appId">The Appliaction ID you received when you registered your app on the JustGiving developer portal.</param>
/// <param name="applicationKey">If you created a Application Key for this app in the Developer Portal, you must include it here for your app to work.</param>
public JustGivingApiClient2(string appId, string applicationKey)
{
if (string.IsNullOrWhiteSpace(appId))
{
throw new ArgumentException("You must provide an appId");
}
if (string.IsNullOrWhiteSpace(applicationKey))
{
throw new ArgumentException("Application Key cannot be empty. If you have not created an Application Key for your application, use the overloaded version of this constructor.");
}
_options = new ClientOptions(appId, applicationKey)
{
AuthorizationMode = AuthorizationMode.Anonymous,
Endpoint = Endpoints.Production,
LoggingOptions = LoggingOptions.Default
};
}
/// <summary>
/// Creates an API client using custom configuration settings. For advanced use.
/// </summary>
/// <param name="options"></param>
public JustGivingApiClient2(ClientOptions options)
{
_options = options;
}
/// <summary>
/// Tells the API client to use the JustGiving sandbox environment, and turns on verbose logging of all HTTP requests and responses.
/// </summary>
public void UseSandbox()
{
_options.Endpoint = Endpoints.Sandbox;
}
/// <summary>
/// Tells the API client to write extremely detailed log information about every API request and response. For development & troubleshooting scenarios.
/// </summary>
public void LogEverything()
{
_options.LoggingOptions.LogAllMessageContent = true;
_options.LoggingOptions.LogFailedRequests = true;
_options.LoggingOptions.LogSuccessfulRequests = true;
_options.RequestDebuggingInfo = true;
}
/// <summary>
/// Tells the API client the use the JustGiving production (live) environment, with some minimal error logging.
/// </summary>
public void UseProduction()
{
_options.Endpoint = Endpoints.Production;
}
/// <summary>
/// API resources for working with user accounts and personalised information.
/// </summary>
public AccountClient Accounts
{
get
{
if (_accounts == null)
{
_accounts = new AccountClient(RestClient, Logger);
}
return _accounts;
}
}
/// <summary>
/// API resources for working with Charity data.
/// </summary>
public CharityClient Charities
{
get
{
if (_charities == null)
{
_charities = new CharityClient(RestClient, Logger);
}
return _charities;
}
}
/// <summary>
/// API resources for working with Country data.
/// </summary>
public CountryClient Countries
{
get
{
if (_countries == null)
{
_countries = new CountryClient(RestClient, Logger);
}
return _countries;
}
}
/// <summary>
/// Gets or sets the underlying log infrastructure. Defaults to the Log4NetLogProvider.
/// </summary>
public ILogProvider LogProvider
{
get
{
if (_logProvider == null)
{
_logProvider = new Log4NetLogProvider();
}
return _logProvider;
}
set { _logProvider = value; }
}
private ApiRequestLogger Logger
{
get
{
if (_logger == null)
{
_logger = new ApiRequestLogger(_options, LogProvider);
}
return _logger;
}
}
private IRestClient RestClient
{
get
{
if (_restClient == null)
{
var factory = new RestClientFactory();
_restClient = factory.CreateClient(_options);
}
return _restClient;
}
}
}
}