-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adjust clients for updated API of Dynamics and Business Central
- Loading branch information
Dmitry Kozlov
committed
Nov 20, 2024
1 parent
ce30f56
commit 018fc48
Showing
15 changed files
with
200 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Plumsail.DataSource | ||
{ | ||
class DebugRequestHandler : DelegatingHandler | ||
{ | ||
public DebugRequestHandler(HttpMessageHandler? innerHandler = null) | ||
{ | ||
InnerHandler = innerHandler ?? new HttpClientHandler(); | ||
} | ||
|
||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
Console.WriteLine(""); | ||
Console.WriteLine(string.Format("Request: {0} {1}", request.Method, request.RequestUri)); | ||
Console.WriteLine("Request headers:"); | ||
foreach (var header in request.Headers) | ||
{ | ||
Console.WriteLine(string.Format("{0}: {1}", header.Key, string.Join(',', header.Value))); | ||
} | ||
if (request.Content is not null) | ||
{ | ||
Console.WriteLine(""); | ||
Console.WriteLine("Request body:"); | ||
var body = await request.Content.ReadAsStringAsync(); | ||
Console.WriteLine(body); | ||
} | ||
|
||
return await base.SendAsync(request, cancellationToken); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Plumsail.DataSource | ||
{ | ||
class DebugResponseHandler : DelegatingHandler | ||
{ | ||
public DebugResponseHandler(HttpMessageHandler? innerHandler = null) | ||
{ | ||
InnerHandler = innerHandler ?? new HttpClientHandler(); | ||
} | ||
|
||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
var response = await base.SendAsync(request, cancellationToken); | ||
|
||
Console.WriteLine(""); | ||
Console.WriteLine("Response headers:"); | ||
foreach (var header in response.Headers) | ||
{ | ||
Console.WriteLine(string.Format("{0}: {1}", header.Key, string.Join(',', header.Value))); | ||
} | ||
if (response.Content is not null) | ||
{ | ||
Console.WriteLine(""); | ||
Console.WriteLine("Response body:"); | ||
var body = await response.Content.ReadAsStringAsync(); | ||
Console.WriteLine(body); | ||
} | ||
|
||
return response; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 0 additions & 20 deletions
20
FunctionApp/Dynamics365/BusinessCentral/GraphServiceClientExtensions.cs
This file was deleted.
Oops, something went wrong.
49 changes: 0 additions & 49 deletions
49
FunctionApp/Dynamics365/BusinessCentral/GraphServiceClientProvider.cs
This file was deleted.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
FunctionApp/Dynamics365/BusinessCentral/HttpClientExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System.Text.Json.Nodes; | ||
|
||
namespace Plumsail.DataSource.Dynamics365.BusinessCentral | ||
{ | ||
internal static class HttpClientExtensions | ||
{ | ||
internal static async System.Threading.Tasks.Task<string> GetCompanyIdAsync(this HttpClient client, string companyName) | ||
{ | ||
var companiesJson = await client.GetStringAsync("companies?$select=id,name"); | ||
var contacts = JsonValue.Parse(companiesJson)["value"].AsArray(); | ||
|
||
return contacts.FirstOrDefault(c => c["name"]?.GetValue<string>() == companyName)?["id"]?.GetValue<string>(); | ||
} | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
FunctionApp/Dynamics365/BusinessCentral/HttpClientProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using Azure.Core; | ||
using Azure.Identity; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.Graph.Beta; | ||
using Microsoft.Identity.Client; | ||
using Microsoft.Kiota.Abstractions.Authentication; | ||
using Plumsail.DataSource.Dynamics365.BusinessCentral.Settings; | ||
using Plumsail.DataSource.Dynamics365.CRM; | ||
using System; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Plumsail.DataSource.Dynamics365.BusinessCentral | ||
{ | ||
public class HttpClientProvider | ||
{ | ||
private readonly AzureApp _azureAppSettings; | ||
|
||
public HttpClientProvider(IOptions<AppSettings> settings) | ||
{ | ||
_azureAppSettings = settings.Value.AzureApp; | ||
} | ||
|
||
public HttpClient Create() | ||
{ | ||
// for debugging requests | ||
//var debugHandler = new DebugRequestHandler(new DebugResponseHandler()); | ||
//var client = new HttpClient(new OAuthMessageHandler(_azureAppSettings, debugHandler)); | ||
|
||
var client = new HttpClient(new OAuthMessageHandler(_azureAppSettings)); | ||
client.BaseAddress = new Uri($"https://api.businesscentral.dynamics.com/v2.0/{_azureAppSettings.InstanceId}/Production/api/v2.0/"); | ||
client.Timeout = new TimeSpan(0, 2, 0); | ||
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | ||
|
||
return client; | ||
} | ||
} | ||
|
||
class OAuthMessageHandler : DelegatingHandler | ||
{ | ||
private readonly AzureApp _azureAppSettings; | ||
|
||
public OAuthMessageHandler(AzureApp azureAppSettings, HttpMessageHandler? innerHandler = null) : base(innerHandler ?? new HttpClientHandler()) | ||
{ | ||
_azureAppSettings = azureAppSettings; | ||
} | ||
|
||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
var app = ConfidentialClientApplicationBuilder.Create(_azureAppSettings.ClientId) | ||
.WithClientSecret(_azureAppSettings.ClientSecret) | ||
.WithTenantId(_azureAppSettings.Tenant) | ||
.Build(); | ||
|
||
var cache = new TokenCacheHelper(AzureApp.CacheFileDir); | ||
cache.EnableSerialization(app.UserTokenCache); | ||
|
||
var account = await app.GetAccountAsync(cache.GetAccountIdentifier()); | ||
var result = await app.AcquireTokenSilent(["https://api.businesscentral.dynamics.com/.default"], account).ExecuteAsync(); | ||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken); | ||
return await base.SendAsync(request, cancellationToken); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.