-
Notifications
You must be signed in to change notification settings - Fork 0
/
CanopyServicesProvider.cs
65 lines (57 loc) · 2.82 KB
/
CanopyServicesProvider.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
using Canopy.Provider.Models;
using Canopy.Provider.Services;
using System;
using System.Net;
using System.Net.Http;
namespace Canopy.Provider
{
public class CanopyServicesProvider
{
/// <summary>
/// Gets the search Canopy service
/// </summary>
/// <typeparam name="TInterface">A Canopy service interface (Canopy.Provider.Interfaces)</typeparam>
/// <typeparam name="TImplementation">A Canopy service implementation (Canopy.Provider.Services)</typeparam>
/// <param name="apiBaseUrl">Canopy base URL</param>
/// <param name="account">Account object with UUID and SHA 256 encoding key</param>
/// <param name="proxy">(optional) Configure a proxy if required</param>
public static TInterface GetSearchService<TInterface, TImplementation>(Uri apiBaseUrl, Account account, WebProxy proxy = null)
where TInterface : class where TImplementation : class, TInterface
{
var httpClient = GetHttpClient(apiBaseUrl, proxy);
return (TInterface)typeof(TImplementation)
.GetConstructor(new Type[] { typeof(HttpClient), typeof(Account) })
.Invoke(new object[] { httpClient, account });
}
/// <summary>
/// Gets the retrieve Canopy service
/// </summary>
/// <typeparam name="TInterface">A Canopy service interface (Canopy.Provider.Interfaces)</typeparam>
/// <typeparam name="TImplementation">A Canopy service implementation (Canopy.Provider.Services)</typeparam>
/// <param name="apiBaseUrl">Canopy base URL</param>
/// <param name="account">Account object with UUID and SHA 256 encoding key</param>
/// <param name="proxy">(optional) Configure a proxy if required</param>
public static TInterface GetRetrieveService<TInterface, TImplementation>(Uri apiBaseUrl, Account account, WebProxy proxy = null)
where TInterface : class where TImplementation : class, TInterface
{
var httpClient = GetHttpClient(apiBaseUrl, proxy);
CanopySearchService<Product> productSearch = new(httpClient, account);
return (TInterface)typeof(TImplementation)
.GetConstructor(new Type[] { typeof(HttpClient), typeof(Account), typeof(CanopySearchService<Product>) })
.Invoke(new object[] { httpClient, account, productSearch });
}
private static HttpClient GetHttpClient(Uri apiBaseUrl, WebProxy proxy = null)
{
var httpClientHandler = new HttpClientHandler()
{
Proxy = proxy,
UseProxy = true,
AllowAutoRedirect = true,
};
return new HttpClient(httpClientHandler)
{
BaseAddress = apiBaseUrl
};
}
}
}