in src/CsrValidation/csharp/ScepValidation/IntuneClient.cs [108:181]
public async Task<JObject> PostAsync(string serviceName, string urlSuffix, string apiVersion, JObject json, Guid activityId, Dictionary<string, string> additionalHeaders = null)
{
if (string.IsNullOrWhiteSpace(serviceName))
{
throw new ArgumentNullException(nameof(serviceName));
}
if (string.IsNullOrWhiteSpace(urlSuffix))
{
throw new ArgumentNullException(nameof(urlSuffix));
}
if (string.IsNullOrWhiteSpace(apiVersion))
{
throw new ArgumentNullException(nameof(apiVersion));
}
if (json == null)
{
throw new ArgumentNullException(nameof(json));
}
string intuneServiceEndpoint = await this.locationProvider.GetServiceEndpointAsync(serviceName);
if (string.IsNullOrWhiteSpace(intuneServiceEndpoint))
{
IntuneServiceNotFoundException exception = new IntuneServiceNotFoundException(serviceName);
trace.TraceEvent(TraceEventType.Error, 0, exception.Message);
throw exception;
}
string token = await authClient.AcquireTokenAsync(new[] { intuneResourceUrl + "/.default" });
string intuneRequestUrl = intuneServiceEndpoint + "/" + urlSuffix;
IHttpClient client = this.httpClient;
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
client.DefaultRequestHeaders.Add("client-request-id", activityId.ToString());
client.DefaultRequestHeaders.Add("api-version", apiVersion);
var httpContent = new StringContent(json.ToString(), Encoding.UTF8, "application/json");
if (additionalHeaders != null)
{
foreach (KeyValuePair<string, string> entry in additionalHeaders)
{
client.DefaultRequestHeaders.Add(entry.Key, entry.Value);
}
}
HttpResponseMessage response = null;
string result = null;
try
{
response = await client.PostAsync(intuneRequestUrl, httpContent);
result = await response.Content.ReadAsStringAsync();
response.EnsureSuccessStatusCode();
}
catch (HttpRequestException e)
{
trace.TraceEvent(TraceEventType.Error, 0, $"Failed to contact intune service with URL: {intuneRequestUrl};\r\n{e.Message}");
trace.TraceEvent(TraceEventType.Error, 0, result);
this.locationProvider.Clear(); // clear contents in case the service location has changed and we cached the value
throw;
}
try
{
return JObject.Parse(result);
}
catch (JsonReaderException e)
{
throw new IntuneClientException($"Failed to parse JSON response from Intune. Response {result}", e);
}
}