-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphService.cs
57 lines (50 loc) · 1.94 KB
/
GraphService.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
using Microsoft.Extensions.Logging;
using Microsoft.Graph;
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace Extensions.GraphAPI
{
public class GraphService
{
private readonly ILogger<GraphService> _logger;
private IGraphAuthenticator _graphAuthenticator;
public GraphService(ILogger<GraphService> logger)
{
_logger = logger;
}
public void Authenticate(IGraphAuthenticator authenticator)
{
this._graphAuthenticator = authenticator;
}
public async Task<HttpResponseMessage> ProcessRequestAsync(string method, string url, object content, string token = "", string contentType = "application/json")
{
if (_graphAuthenticator == null) throw new ArgumentNullException(nameof(_graphAuthenticator));
try
{
var client = _graphAuthenticator.GetAuthenticatedClient(AuthenticatorProvider.ConfidentialUserClient, token);
var requestUrl = $"{client.BaseUrl.Replace("/v1.0", "")}/{url}";
_logger.LogDebug($"Request:{method} {requestUrl}");
var request = new BaseRequest(requestUrl, client, null)
{
Method = method
};
var response = await request.SendRequestAsync(content, CancellationToken.None, HttpCompletionOption.ResponseContentRead).ConfigureAwait(false);
return response;
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
var errorContent = new StringContent(ex.Message);
var errorResponse = new HttpResponseMessage
{
Content = errorContent,
StatusCode = System.Net.HttpStatusCode.InternalServerError
};
return errorResponse;
}
}
}
}