-
Notifications
You must be signed in to change notification settings - Fork 137
/
ServiceContext.cs
448 lines (366 loc) · 15.5 KB
/
ServiceContext.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
////*********************************************************
// <copyright file="ServiceContext.cs" company="Intuit">
/*******************************************************************************
* Copyright 2016 Intuit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
// <summary>This file contains SdkException.</summary>
// <summary>This file contains Service Context.</summary>
////*********************************************************
namespace Intuit.Ipp.Core
{
using System;
using System.Globalization;
using System.Net;
using System.Xml;
using Configuration;
using Rest;
using Intuit.Ipp.Diagnostics;
using Intuit.Ipp.Exception;
using Intuit.Ipp.Security;
using System.Collections.Generic;
using System.Configuration;
#if NETCORE
using Microsoft.Extensions.Configuration;
#endif
/// <summary>
/// This Enumeration specifies which Intuit service to connect to.
/// </summary>
public enum IntuitServicesType
{
/// <summary>
/// QuickBooks Online Data through IDS.
/// </summary>
QBO,
/// <summary>
/// Intuit Platform services.
/// </summary>
IPS,
/// <summary>
/// None service type.
/// </summary>
None
}
/// <summary>
/// Intuit Partner Platform Service Context.
/// </summary>
public class ServiceContext
{
#region Private Members
/// <summary>
/// The Realm Id.
/// </summary>
private string realmId;
/// <summary>
/// Intuit Service Type(QBO).
/// </summary>
private IntuitServicesType serviceType;
/// <summary>
/// Base Uri for IDS Service Call.
/// </summary>
private string baseserviceURL;
/// <summary>
/// Application Token.
/// </summary>
private string appToken;
/// <summary>
/// this flag indicates if static create methods of this class has been invoked.
/// </summary>
private bool isCreateMethod;
/// <summary>
/// Temporary storage for serialization and compression values for request and reponse.
/// </summary>
private Message messageValues;
/// <summary>
/// include param to be passed to services. Responsible for getting additional data or special handling of request.
/// </summary>
private List<String> include = new List<String>();
/// <summary>
/// requestId param to be passed to services. Responsible for identifying each request by a unique identifier.
/// </summary>
private string requestId;
#endregion
#region Constructors
#region Intuit Anywhere and Federated
/// <summary>
/// Initializes a new instance of the <see cref="ServiceContext"/> class.
/// </summary>
/// <param name="realmId">The realm id.</param>
/// <param name="serviceType">Service Type - QBO/QB.</param>
/// <param name="requestValidator">The request validate.</param>
/// <param name="configReader">The config reader, if <see langword="null"/>, <see cref="JsonFileConfigurationProvider" /> will be used</param>
/// <returns>Returns ServiceContext object.</returns>
/// <exception cref="Intuit.Ipp.Exception.IdsException">If arguments are null or empty.</exception>
/// <exception cref="Intuit.Ipp.Exception.InvalidRealmException">If realm id is invalid.</exception>
/// <exception cref="Intuit.Ipp.Exception.InvalidTokenException">If the token is invalid.</exception>
public ServiceContext(string realmId, IntuitServicesType serviceType, IRequestValidator requestValidator = null, IConfigurationProvider configReader = null)
{
this.IppConfiguration = (configReader ?? new JsonFileConfigurationProvider()).GetConfiguration();
// Validate Parameters
if (string.IsNullOrWhiteSpace(realmId))
{
IdsExceptionManager.HandleException(new InvalidRealmException(Properties.Resources.ParameterNotNullEmptyMessage, new ArgumentException(Properties.Resources.ParameterNotNullMessage, Properties.Resources.RealmIdParameterName)));
}
this.realmId = realmId;
this.serviceType = serviceType;
if (requestValidator != null)
{
this.IppConfiguration.Security = requestValidator;
}
if (this.IppConfiguration.Security == null)
{
throw new InvalidTokenException("Atleast one security mechanism has to be specified for the SDK to work. Either use config file or use constructor to specify the authentication type.");
}
this.baseserviceURL = this.GetBaseURL();
this.isCreateMethod = false;
}
/// <summary>
/// Initializes a new instance of the <see cref="ServiceContext"/> class.
/// </summary>
/// <param name="appToken">Application Token.</param>
/// <param name="realmId">The realm id.</param>
/// <param name="serviceType">Service Type - QBO.</param>
/// <param name="requestValidator">The request validate.</param>
/// <returns>Returns ServiceContext object.</returns>
/// <exception cref="Intuit.Ipp.Exception.IdsException">If arguments are null or empty.</exception>
/// <exception cref="Intuit.Ipp.Exception.InvalidRealmException">If realm id is invalid.</exception>
/// <exception cref="Intuit.Ipp.Exception.InvalidTokenException">If the token is invalid.</exception>
[Obsolete("This constructor is deprecated as appToken is not supported in OAuth2. Please use the other constuctor")]
public ServiceContext(string appToken, string realmId, IntuitServicesType serviceType, IRequestValidator requestValidator = null)
: this()
{
// Validate Parameters
if (string.IsNullOrWhiteSpace(appToken))
{
IdsExceptionManager.HandleException(new IdsException(Properties.Resources.ParameterNotNullMessage, new ArgumentNullException(Properties.Resources.AppTokenParameterName, Properties.Resources.ParameterNotNullMessage)));
}
if (string.IsNullOrWhiteSpace(realmId))
{
IdsExceptionManager.HandleException(new InvalidRealmException(Properties.Resources.ParameterNotNullEmptyMessage, new ArgumentException(Properties.Resources.ParameterNotNullMessage, Properties.Resources.RealmIdParameterName)));
}
this.realmId = realmId;
this.serviceType = serviceType;
this.appToken = appToken;
if (requestValidator != null)
{
this.IppConfiguration.Security = requestValidator;
}
if (this.IppConfiguration.Security == null)
{
throw new InvalidTokenException("Atleast one security mechanism has to be specified for the SDK to work. Either use config file or use constructor to specify the authenticatio type.");
}
this.baseserviceURL = this.GetBaseURL();
this.isCreateMethod = false;
}
/// <summary>
/// Prevents a default instance of the <see cref="ServiceContext"/> class from being created.
/// </summary>
private ServiceContext() { }
#endregion
#endregion
#region Public Properties
/// <summary>
/// Gets or sets the Ipp configuration.
/// </summary>
public IppConfiguration IppConfiguration { get; set; }
/// <summary>
/// Gets Base Uri for IDS Service Calls.
/// </summary>
public string BaseUrl
{
get { return this.GetBaseURL(); }
}
/// <summary>
/// Gets Realm/Company Id.
/// </summary>
public string RealmId
{
get { return this.realmId; }
}
/// <summary>
/// Gets or Sets the Application Token.
/// </summary>
[Obsolete("This property is deprecated as appToken is not supported in OAuth2")]
public string AppToken
{
get { return this.appToken; }
}
/// <summary>
/// Gets Intuit Service Type.
/// </summary>
public IntuitServicesType ServiceType
{
get
{
return this.serviceType;
}
}
/// <summary>
/// Gets Intuit Include Type.
/// </summary>
public List<String> Include
{
get
{
return this.include;
}
set
{
this.include = value;
}
}
/// <summary>
/// Gets Intuit MinorVersion Type.
/// </summary>
public string MinorVersion
{
get
{
return this.GetMinorVersion();
}
}
/// <summary>
/// Gets Unique requestId for the API call.
/// </summary>
public string RequestId {
get
{
if (this.requestId ==null)
{
this.requestId = Guid.NewGuid().ToString("N");
}
return this.requestId;
}
set
{
this.requestId = value;
}
}
/// <summary>
/// timeout param to be passed to services. To setup the ReadWriteTimeout property in HttpWebRequest.
/// It is only for sync web requests. If not set, the default timeout will be used.
/// </summary>
public Nullable<int> Timeout { get; set; }
#endregion
#region Public Methods
/// <summary>
/// Populates the values of the service context like realmId, service type to the Data Services being targetted.
/// </summary>
public void UseDataServices()
{
if (this.isCreateMethod)
{
this.serviceType = IntuitServicesType.QBO;
// Get the base Uri for the new service type
this.baseserviceURL = this.GetBaseURL();
this.RevertConfiguration();
}
}
/// <summary>
/// Populates the values of the service context like service type and base url to Platform Services.
/// </summary>
public void UsePlatformServices()
{
this.serviceType = IntuitServicesType.IPS;
this.baseserviceURL = this.GetBaseURL();
}
#endregion
#region Private Methods
/// <summary>
/// Sets the default configuration values for IPS operations.
/// </summary>
private void SetDefaultConfigurationForIPS()
{
this.messageValues = new Message
{
Request = new Request
{
CompressionFormat = this.IppConfiguration.Message.Request.CompressionFormat,
SerializationFormat = this.IppConfiguration.Message.Request.SerializationFormat
},
Response = new Response
{
CompressionFormat = this.IppConfiguration.Message.Response.CompressionFormat,
SerializationFormat = this.IppConfiguration.Message.Response.SerializationFormat
}
};
// Set the serviceContext IPP configuration to what IPS is accepting.
this.IppConfiguration.Message.Request.SerializationFormat = Configuration.SerializationFormat.Xml;
this.IppConfiguration.Message.Request.CompressionFormat = Configuration.CompressionFormat.None;
this.IppConfiguration.Message.Response.SerializationFormat = Configuration.SerializationFormat.Xml;
this.IppConfiguration.Message.Response.CompressionFormat = Configuration.CompressionFormat.None;
}
/// <summary>
/// Reverts the ipp configuration to the original values.
/// </summary>
private void RevertConfiguration()
{
if (this.messageValues != null && this.messageValues.Request != null && this.messageValues.Response != null)
{
this.IppConfiguration.Message.Request.SerializationFormat = this.messageValues.Request.SerializationFormat;
this.IppConfiguration.Message.Request.CompressionFormat = this.messageValues.Request.CompressionFormat;
this.IppConfiguration.Message.Response.SerializationFormat = this.messageValues.Response.SerializationFormat;
this.IppConfiguration.Message.Response.CompressionFormat = this.messageValues.Response.CompressionFormat;
}
}
/// <summary>
/// Gets the base Uri for a QBO user.
/// </summary>
/// <returns>Returns the base Uri endpoint for a user.</returns>
private string GetBaseURL()
{
this.IppConfiguration.Logger.CustomLogger.Log(TraceLevel.Info, "Called GetBaseURL method.");
string baseurl = string.Empty;
if (this.serviceType == IntuitServicesType.QBO)
{
baseurl = this.IppConfiguration.BaseUrl.Qbo;
if (string.IsNullOrEmpty(baseurl))
{
baseurl = Utility.CoreConstants.QBO_BASEURL;
}
else
{
if (!baseurl.EndsWith("/"))
{
baseurl = baseurl + "/";
}
}
this.IppConfiguration.Logger.CustomLogger.Log(TraceLevel.Info, string.Format(CultureInfo.InvariantCulture, "BaseUrl set for QBO Service Type: {0}.", baseurl));
}
else if (this.serviceType == IntuitServicesType.IPS)
{
baseurl = this.IppConfiguration.BaseUrl.Ips;
if (string.IsNullOrEmpty(baseurl))
{
baseurl = Utility.CoreConstants.IPS_BASEURL;
}
this.IppConfiguration.Logger.CustomLogger.Log(TraceLevel.Info, string.Format(CultureInfo.InvariantCulture, "BaseUrl set for Intuit Platform Service Type: {0}.", baseurl));
}
return baseurl;
}
/// <summary>
/// Gets the minorVersion for a QBO call
/// </summary>
/// <returns>Returns the minorVersion</returns>
private string GetMinorVersion()
{
this.IppConfiguration.Logger.CustomLogger.Log(TraceLevel.Info, "Called GetMinorVersion method.");
string minorversion = null;
minorversion = this.IppConfiguration.MinorVersion.Qbo;
this.IppConfiguration.Logger.CustomLogger.Log(TraceLevel.Info, string.Format(CultureInfo.InvariantCulture, "MinorVersion set for QBO Service Type: {0}.", minorversion));
return minorversion;
}
#endregion
}
}