Skip to content

Commit

Permalink
Merge branch 'release/1.0.4-upgrade-to-net-8-isolated' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Kozlov committed Nov 20, 2024
2 parents 9549425 + 018fc48 commit b8484b0
Show file tree
Hide file tree
Showing 26 changed files with 429 additions and 378 deletions.
52 changes: 52 additions & 0 deletions .github/workflows/develop_data-source-functions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Docs for the Azure Web Apps Deploy action: https://github.com/azure/functions-action
# More GitHub Actions for Azure: https://github.com/Azure/actions

name: Build and deploy dotnet core app to Azure Function App - data-source-functions

on:
push:
branches:
- develop
workflow_dispatch:

env:
AZURE_FUNCTIONAPP_PACKAGE_PATH: '.' # set this to the path to your web app project, defaults to the repository root
DOTNET_VERSION: '8.0.x' # set this to the dotnet version to use

jobs:
build-and-deploy:
runs-on: windows-latest
permissions:
id-token: write #This is required for requesting the JWT

steps:
- name: 'Checkout GitHub Action'
uses: actions/checkout@v4

- name: Setup DotNet ${{ env.DOTNET_VERSION }} Environment
uses: actions/setup-dotnet@v1
with:
dotnet-version: ${{ env.DOTNET_VERSION }}

- name: 'Resolve Project Dependencies Using Dotnet'
shell: pwsh
run: |
pushd './${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}'
dotnet build --configuration Release --output ./output
popd
- name: Login to Azure
uses: azure/login@v2
with:
client-id: ${{ secrets.AZUREAPPSERVICE_CLIENTID_F1F0122F19484BB2858796558B0D19C9 }}
tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_2C4D4BECD44F46F5B6E6CA8ABD485841 }}
subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_29B57699ACA7440BB8E138E8DA7FB705 }}

- name: 'Run Azure Functions Action'
uses: Azure/functions-action@v1
id: fa
with:
app-name: 'data-source-functions'
slot-name: 'Production'
package: '${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}/output'

36 changes: 36 additions & 0 deletions FunctionApp/DebugRequestHandler.cs
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);
}
}
}
37 changes: 37 additions & 0 deletions FunctionApp/DebugResponseHandler.cs
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;
}
}
}
33 changes: 13 additions & 20 deletions FunctionApp/Dynamics365/BusinessCentral/Authorize.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,34 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.Graph;
using Microsoft.Identity.Client;
using System.Net.Http.Headers;
using Microsoft.Graph.Auth;
using System.Linq;
using System.Collections.Generic;
using Microsoft.Extensions.Options;
using Microsoft.Identity.Client;
using Plumsail.DataSource.Dynamics365.BusinessCentral.Settings;
using System.Text;
using Microsoft.AspNetCore.Http.Extensions;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Plumsail.DataSource.Dynamics365.BusinessCentral
{
public class Authorize
{
private readonly AzureApp _settings;
private readonly ILogger<Authorize> _logger;

public Authorize(IOptions<AppSettings> settings)
public Authorize(IOptions<AppSettings> settings, ILogger<Authorize> logger)
{
_settings = settings.Value.AzureApp;
_logger = logger;
}

[FunctionName("D365-BC-Authorize")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
[Function("D365-BC-Authorize")]
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
{
var scopes = new string[] { "https://graph.microsoft.com/.default", "offline_access" };
var scopes = new string[] { "https://api.businesscentral.dynamics.com/.default", "offline_access" };

if (req.Method == "POST" && req.Form.ContainsKey("code"))
{
Expand Down
58 changes: 23 additions & 35 deletions FunctionApp/Dynamics365/BusinessCentral/Customers.cs
Original file line number Diff line number Diff line change
@@ -1,60 +1,48 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.Graph;
using Microsoft.Identity.Client;
using System.Net.Http.Headers;
using Microsoft.Graph.Auth;
using System.Linq;
using System.Collections.Generic;
using Microsoft.Extensions.Options;
using Microsoft.Graph;
using Microsoft.Graph.Beta;
using Microsoft.Graph.Beta.Models;
using Plumsail.DataSource.Dynamics365.BusinessCentral.Settings;
using System.Text;
using Microsoft.AspNetCore.Http.Extensions;
using System.Net;
using Plumsail.DataSource.Dynamics365.CRM;
using System.Collections.Generic;
using System.Text.Json.Nodes;
using System.Threading.Tasks;

namespace Plumsail.DataSource.Dynamics365.BusinessCentral
{
public class Customers
{
private readonly Settings.Customers _settings;
private readonly GraphServiceClientProvider _graphProvider;
private readonly HttpClientProvider _httpClientProvider;
private readonly ILogger<Customers> _logger;

public Customers(IOptions<AppSettings> settings, GraphServiceClientProvider graphProvider)
public Customers(IOptions<AppSettings> settings, HttpClientProvider httpClientProvider, ILogger<Customers> logger)
{
_settings = settings.Value.Customers;
_graphProvider = graphProvider;
_httpClientProvider = httpClientProvider;
_logger = logger;
}

[FunctionName("D365-BC-Customers")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
ILogger log)
[Function("D365-BC-Customers")]
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest req)
{
log.LogInformation("Dynamics365-BusinessCentral-Customers is requested.");
_logger.LogInformation("Dynamics365-BusinessCentral-Customers is requested.");

var graph = await _graphProvider.Create();
var company = await graph.GetCompanyAsync(_settings.Company);
if (company == null)
var client = _httpClientProvider.Create();
var companyId = await client.GetCompanyIdAsync(_settings.Company);
if (companyId == null)
{
return new NotFoundResult();
}

var customersPage = await graph.Financials.Companies[company.Id].Customers.Request().GetAsync();
var customers = new List<Customer>(customersPage);
while (customersPage.NextPageRequest != null)
{
customersPage = await customersPage.NextPageRequest.GetAsync();
customers.AddRange(customersPage);
}

return new OkObjectResult(customers);
var customersJson = await client.GetStringAsync($"companies({companyId})/customers");
var customers = JsonValue.Parse(customersJson);
return new OkObjectResult(customers?["value"]);
}
}
}

This file was deleted.

This file was deleted.

15 changes: 15 additions & 0 deletions FunctionApp/Dynamics365/BusinessCentral/HttpClientExtensions.cs
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>();
}
}
}
Loading

0 comments on commit b8484b0

Please sign in to comment.