-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSession.cs
113 lines (95 loc) · 3.28 KB
/
Session.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
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using EncompassREST.Exceptions;
namespace EncompassREST
{
public class Session
{
private readonly string _clientId;
private readonly string _clientSecret;
private readonly string _instanceId;
private readonly string _userId;
private readonly string _password;
//private string _SessionKey;
private AccessToken _accessToken;
private const string _apiUrl = "https://api.elliemae.com";
private readonly HttpClient _client;
private DateTime _tokenExpires;
private DateTime _tokenLastCall;
#region Properties
public AccessToken AccessToken
{
get
{
return _accessToken;
}
private set
{
_accessToken = value;
_tokenLastCall = DateTime.Now;
IEnumerable<string> vals;
if (_client.DefaultRequestHeaders.TryGetValues("Authorization", out vals))
_client.DefaultRequestHeaders.Remove("Authorization");
_client.DefaultRequestHeaders.Add("Authorization", _accessToken.AuthenticationString);
}
}
public Loans Loans { get; }
public Schemas Schemas { get; }
public Webhooks Webhooks { get; }
public Reports Reports { get; }
public Pipeline Pipeline { get; }
public HttpClient RESTClient
{
get
{
if (_accessToken == null)
throw new SessionNotOpenException();
var now = DateTime.Now;
if ((now - _tokenLastCall).TotalMinutes > 12 ||
(_tokenExpires - now).TotalMinutes < 10)
{
ClearAccessToken();
StartSession();
}
_tokenLastCall = now;
return _client;
}
}
#endregion
public Session(string clientId, string clientSecret, string instanceId, string userId, string password)
{
_clientId = clientId;
_clientSecret = clientSecret;
_instanceId = instanceId;
_userId = userId;
_password = password;
_client = new HttpClient()
{
BaseAddress = new Uri(_apiUrl)
};
ServicePointManager.DefaultConnectionLimit = 100;
_accessToken = null;
Loans = new Loans(this);
Schemas = new Schemas(this);
Webhooks = new Webhooks(this);
Reports = new Reports(this);
Pipeline = new Pipeline(this);
}
public void StartSession()
{
var t = new AccessToken(_clientId, _clientSecret, _instanceId, this);
t.GetTokenAsync(_userId, _password).Wait();
AccessToken = t;
var validate = t.GetTokenValidationAsync().Result;
_tokenExpires = validate.GetExpirationDate();
}
internal void ClearAccessToken()
{
_accessToken = null;
_client.DefaultRequestHeaders.Remove("Authorization");
Console.WriteLine("Session Cleared");
}
}
}