-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathServiceNowClient.cs
More file actions
133 lines (111 loc) · 4.37 KB
/
ServiceNowClient.cs
File metadata and controls
133 lines (111 loc) · 4.37 KB
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
// Copyright (c) One Identity LLC. All rights reserved.
namespace ServiceNowTicketValidator
{
using System;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security;
using System.Text;
using System.Text.Json;
using global::ServiceNowTicketValidator.DTOs;
using OneIdentity.SafeguardDotNet;
using Serilog;
internal class ServiceNowClient : IDisposable
{
private const string IncidentByTicketNumberFormat = "api/now/table/incident?sysparm_query=number%3D{0}&sysparm_limit=1";
private readonly Uri _applicationUrl;
private readonly string _userName;
private readonly SecureString _password;
private HttpClient _restClient;
public ServiceNowClient(
string applicationUrl,
SecureString clientSecret,
string userName,
SecureString password)
{
_applicationUrl = new Uri(applicationUrl, UriKind.Absolute);
_userName = userName;
_password = password.Copy();
if (clientSecret != null && clientSecret.Length != 0)
{
throw new NotImplementedException("Only basic authentication with a username and password is supported in this example. Do not specify a client secret. OAuth authentication is not implemented.");
}
CreateHttpClient();
}
public ServiceNowIncident GetIncident(string ticketNumber)
{
var url = string.Format(IncidentByTicketNumberFormat, Uri.EscapeDataString(ticketNumber));
var req = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri(_applicationUrl, url),
};
try
{
var res = _restClient.SendAsync(req).GetAwaiter().GetResult();
var msg = res.Content?.ReadAsStringAsync().GetAwaiter().GetResult();
return JsonSerializer.Deserialize<ServiceNowResult<ServiceNowIncident>>(msg)?.result?.FirstOrDefault();
}
catch (Exception ex)
{
Log.Error(ex, "Unable to find incident for ticket number {TicketNumber}", ticketNumber);
return null;
}
}
private T FollowLink<T>(string linkUrl)
where T : class
{
var relativeUrl = new Uri(linkUrl).PathAndQuery;
var req = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri(_applicationUrl, relativeUrl),
};
try
{
var res = _restClient.SendAsync(req).GetAwaiter().GetResult();
var msg = res.Content?.ReadAsStringAsync().GetAwaiter().GetResult();
return JsonSerializer.Deserialize<ServiceNowResult<T>>(msg)?.result?.FirstOrDefault();
}
catch (Exception ex)
{
Log.Error(
ex,
"Unable to follow link {ServiceNowLink} for type {ObjectType}",
linkUrl,
typeof(T).ToString());
return null;
}
}
public ServiceNowCmdbCi GetConfigurationItem(string linkUrl)
{
return FollowLink<ServiceNowCmdbCi>(linkUrl);
}
public ServiceNowSysUser GetSystemUser(string linkUrl)
{
return FollowLink<ServiceNowSysUser>(linkUrl);
}
private void CreateHttpClient()
{
var handler = new HttpClientHandler();
handler.SslProtocols = System.Security.Authentication.SslProtocols.Tls12;
_restClient = new HttpClient(handler);
_restClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
_restClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{_userName}:{_password.ToInsecureString()}")));
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_password?.Dispose();
_restClient?.Dispose();
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}