|
| 1 | +using DocuSign.eSign.Client; |
| 2 | +using eg_03_csharp_auth_code_grant_core.Models; |
| 3 | +using Microsoft.AspNetCore.Http; |
| 4 | +using Microsoft.Extensions.Caching.Memory; |
| 5 | +using System.Security.Claims; |
| 6 | + |
| 7 | +namespace eg_03_csharp_auth_code_grant_core.Common |
| 8 | +{ |
| 9 | + public class RequestItemsService : IRequestItemsService |
| 10 | + { |
| 11 | + private readonly IHttpContextAccessor _httpContextAccessor; |
| 12 | + private readonly IMemoryCache _cache; |
| 13 | + private readonly string _id; |
| 14 | + private string API_CLIENT_KEY = "{0}_ApiClient"; |
| 15 | + private string CONFIG_KEY = "{0}_DocusignConfig"; |
| 16 | + private string BASE_URI = "https://demo.docusign.net/restapi"; |
| 17 | + private string _accessToken; |
| 18 | + |
| 19 | + public RequestItemsService(IHttpContextAccessor httpContextAccessor, IMemoryCache cache) |
| 20 | + { |
| 21 | + _httpContextAccessor = httpContextAccessor; |
| 22 | + _cache = cache; |
| 23 | + Status = "sent"; |
| 24 | + var identity = httpContextAccessor.HttpContext.User.Identity as ClaimsIdentity; |
| 25 | + |
| 26 | + if (identity != null && identity.IsAuthenticated) |
| 27 | + { |
| 28 | + _accessToken = identity.FindFirst(x => x.Type.Equals("access_token")).Value; |
| 29 | + _id = httpContextAccessor.HttpContext.User.Identity.Name; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public ApiClient DefaultApiClient |
| 34 | + { |
| 35 | + get |
| 36 | + { |
| 37 | + var key = string.Format(API_CLIENT_KEY, _id); |
| 38 | + ApiClient apiClient = _cache.Get<ApiClient>("apiClient"); |
| 39 | + if (apiClient == null) |
| 40 | + { |
| 41 | + apiClient = new ApiClient(BASE_URI); |
| 42 | + _cache.Set(key, apiClient); |
| 43 | + } |
| 44 | + |
| 45 | + return apiClient; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public Configuration DefaultConfiguration |
| 50 | + { |
| 51 | + get |
| 52 | + { |
| 53 | + var key = string.Format(CONFIG_KEY, _id); |
| 54 | + var docuSignConfig = _cache.Get<Configuration>(key); |
| 55 | + |
| 56 | + if (docuSignConfig == null) |
| 57 | + { |
| 58 | + docuSignConfig = new Configuration(new ApiClient(BASE_URI)); |
| 59 | + _cache.Set(key, docuSignConfig); |
| 60 | + } |
| 61 | + docuSignConfig.AddDefaultHeader("Authorization", "Bearer " + _accessToken); |
| 62 | + return docuSignConfig; |
| 63 | + } |
| 64 | + } |
| 65 | + private string GetKey(string key) |
| 66 | + { |
| 67 | + return string.Format("{0}_{1}", _id, key); |
| 68 | + } |
| 69 | + public string EgName { |
| 70 | + get => _cache.Get<string>(GetKey("EgName")); |
| 71 | + set => _cache.Set(GetKey("EgName"), value); |
| 72 | + } |
| 73 | + |
| 74 | + public Session Session { |
| 75 | + get => _cache.Get<Session>(GetKey("Session")); |
| 76 | + set => _cache.Set(GetKey("Session"), value); |
| 77 | + } |
| 78 | + |
| 79 | + public User User { |
| 80 | + get => _cache.Get<User>(GetKey("User")); |
| 81 | + set => _cache.Set(GetKey("User"), value); |
| 82 | + } |
| 83 | + |
| 84 | + public string EnvelopeId { |
| 85 | + get => _cache.Get<string>(GetKey("EnvelopeId")); |
| 86 | + set => _cache.Set(GetKey("EnvelopeId"), value); |
| 87 | + } |
| 88 | + |
| 89 | + public string DocumentId { |
| 90 | + get => _cache.Get<string>(GetKey("DocumentId")); |
| 91 | + set => _cache.Set(GetKey("DocumentId"), value); |
| 92 | + } |
| 93 | + |
| 94 | + public EnvelopeDocuments EnvelopeDocuments { |
| 95 | + get => _cache.Get<EnvelopeDocuments>(GetKey("EnvelopeDocuments")); |
| 96 | + set => _cache.Set(GetKey("EnvelopeDocuments"), value); |
| 97 | + } |
| 98 | + |
| 99 | + public string TemplateId { |
| 100 | + get => _cache.Get<string>(GetKey("TemplateId")); |
| 101 | + set => _cache.Set(GetKey("TemplateId"), value); |
| 102 | + } |
| 103 | + |
| 104 | + public string Status { get; set; } |
| 105 | + } |
| 106 | +} |
0 commit comments