Skip to content

Commit 67f547a

Browse files
author
Dmitry Kozlov
committed
changed routes for BC functions and added an ability to request separate entities
1 parent 5ce108a commit 67f547a

File tree

4 files changed

+123
-37
lines changed

4 files changed

+123
-37
lines changed

FunctionApp/Dynamics365/BusinessCentral/Authorize.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public Authorize(IOptions<AppSettings> settings, ILogger<Authorize> logger)
2626
}
2727

2828
[Function("D365-BC-Authorize")]
29-
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
29+
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "bc/authorize")] HttpRequest req)
3030
{
3131
var scopes = new string[] { "https://api.businesscentral.dynamics.com/.default", "offline_access" };
3232

FunctionApp/Dynamics365/BusinessCentral/Customers.cs

+41-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Grpc.Core;
12
using Microsoft.AspNetCore.Http;
23
using Microsoft.AspNetCore.Mvc;
34
using Microsoft.Azure.Functions.Worker;
@@ -29,20 +30,50 @@ public Customers(IOptions<AppSettings> settings, HttpClientProvider httpClientPr
2930
}
3031

3132
[Function("D365-BC-Customers")]
32-
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest req)
33+
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = "bc/customers/{id?}")] HttpRequest req, Guid? id)
3334
{
34-
_logger.LogInformation("Dynamics365-BusinessCentral-Customers is requested.");
35+
_logger.LogInformation("D365-BC-Customers is requested.");
3536

36-
var client = _httpClientProvider.Create();
37-
var companyId = await client.GetCompanyIdAsync(_settings.Company);
38-
if (companyId == null)
37+
try
3938
{
40-
return new NotFoundResult();
41-
}
39+
var client = _httpClientProvider.Create();
40+
var companyId = await client.GetCompanyIdAsync(_settings.Company);
41+
if (companyId == null)
42+
{
43+
return new NotFoundResult();
44+
}
45+
46+
if (!id.HasValue)
47+
{
48+
var customersJson = await client.GetStringAsync($"companies({companyId})/customers");
49+
var customers = JsonValue.Parse(customersJson);
50+
return new OkObjectResult(customers?["value"]);
51+
}
52+
53+
var customerResponse = await client.GetAsync($"companies({companyId})/customers({id})");
54+
if (!customerResponse.IsSuccessStatusCode)
55+
{
56+
if (customerResponse.StatusCode == System.Net.HttpStatusCode.NotFound)
57+
{
58+
return new NotFoundResult();
59+
}
60+
61+
// throws Exception
62+
customerResponse.EnsureSuccessStatusCode();
63+
}
4264

43-
var customersJson = await client.GetStringAsync($"companies({companyId})/customers");
44-
var customers = JsonValue.Parse(customersJson);
45-
return new OkObjectResult(customers?["value"]);
65+
var customerJson = await customerResponse.Content.ReadAsStringAsync();
66+
return new ContentResult()
67+
{
68+
Content = customerJson,
69+
ContentType = "application/json"
70+
};
71+
}
72+
catch (HttpRequestException ex)
73+
{
74+
_logger.LogError(ex, "An error has occured while processing D365-BC-Customers request.");
75+
return new StatusCodeResult(ex.StatusCode.HasValue ? (int)ex.StatusCode.Value : StatusCodes.Status500InternalServerError);
76+
}
4677
}
4778
}
4879
}

FunctionApp/Dynamics365/BusinessCentral/Items.cs

+41-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Grpc.Core;
12
using Microsoft.AspNetCore.Http;
23
using Microsoft.AspNetCore.Mvc;
34
using Microsoft.Azure.Functions.Worker;
@@ -28,20 +29,50 @@ public Items(IOptions<AppSettings> settings, HttpClientProvider httpClientProvid
2829

2930
[Function("D365-BC-Items")]
3031
public async Task<IActionResult> Run(
31-
[HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req)
32+
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "bc/items/{id?}")] HttpRequest req, Guid? id)
3233
{
33-
_logger.LogInformation("Dynamics365-BusinessCentral-Items is requested.");
34+
_logger.LogInformation("D365-BC-Items is requested.");
3435

35-
var client = _httpClientProvider.Create();
36-
var companyId = await client.GetCompanyIdAsync(_settings.Company);
37-
if (companyId == null)
36+
try
3837
{
39-
return new NotFoundResult();
40-
}
38+
var client = _httpClientProvider.Create();
39+
var companyId = await client.GetCompanyIdAsync(_settings.Company);
40+
if (companyId == null)
41+
{
42+
return new NotFoundResult();
43+
}
44+
45+
if (!id.HasValue)
46+
{
47+
var itemsJson = await client.GetStringAsync($"companies({companyId})/items");
48+
var items = JsonValue.Parse(itemsJson);
49+
return new OkObjectResult(items?["value"]);
50+
}
51+
52+
var itemResponse = await client.GetAsync($"companies({companyId})/items({id})");
53+
if (!itemResponse.IsSuccessStatusCode)
54+
{
55+
if (itemResponse.StatusCode == System.Net.HttpStatusCode.NotFound)
56+
{
57+
return new NotFoundResult();
58+
}
59+
60+
// throws Exception
61+
itemResponse.EnsureSuccessStatusCode();
62+
}
4163

42-
var itemsJson = await client.GetStringAsync($"companies({companyId})/items");
43-
var items = JsonValue.Parse(itemsJson);
44-
return new OkObjectResult(items?["value"]);
64+
var itemJson = await itemResponse.Content.ReadAsStringAsync();
65+
return new ContentResult()
66+
{
67+
Content = itemJson,
68+
ContentType = "application/json"
69+
};
70+
}
71+
catch (HttpRequestException ex)
72+
{
73+
_logger.LogError(ex, "An error has occured while processing D365-BC-Items request.");
74+
return new StatusCodeResult(ex.StatusCode.HasValue ? (int)ex.StatusCode.Value : StatusCodes.Status500InternalServerError);
75+
}
4576
}
4677
}
4778
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
using Microsoft.AspNetCore.Http;
22
using Microsoft.AspNetCore.Mvc;
33
using Microsoft.Azure.Functions.Worker;
4-
using Microsoft.Azure.WebJobs;
54
using Microsoft.Extensions.Logging;
65
using Microsoft.Extensions.Options;
7-
using Microsoft.Graph;
8-
using Microsoft.Graph.Beta.Models;
96
using Plumsail.DataSource.Dynamics365.BusinessCentral.Settings;
10-
using Plumsail.DataSource.Dynamics365.CRM;
11-
using System.Collections.Generic;
127
using System.Text.Json.Nodes;
13-
using System.Threading.Tasks;
148

159
namespace Plumsail.DataSource.Dynamics365.BusinessCentral
1610
{
@@ -28,20 +22,50 @@ public Vendors(IOptions<AppSettings> settings, HttpClientProvider httpClientProv
2822
}
2923

3024
[Function("D365-BC-Vendors")]
31-
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest req)
25+
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = "bc/vendors/{id?}")] HttpRequest req, Guid? id)
3226
{
33-
_logger.LogInformation("Dynamics365-BusinessCentral-Vendors is requested.");
27+
_logger.LogInformation("D365-BC-Vendors is requested.");
3428

35-
var client = _httpClientProvider.Create();
36-
var companyId = await client.GetCompanyIdAsync(_settings.Company);
37-
if (companyId == null)
29+
try
3830
{
39-
return new NotFoundResult();
40-
}
31+
var client = _httpClientProvider.Create();
32+
var companyId = await client.GetCompanyIdAsync(_settings.Company);
33+
if (companyId == null)
34+
{
35+
return new NotFoundResult();
36+
}
37+
38+
if (!id.HasValue)
39+
{
40+
var vendorsJson = await client.GetStringAsync($"companies({companyId})/vendors");
41+
var vendors = JsonValue.Parse(vendorsJson);
42+
return new OkObjectResult(vendors?["value"]);
43+
}
44+
45+
var vendorResponse = await client.GetAsync($"companies({companyId})/vendors({id})");
46+
if (!vendorResponse.IsSuccessStatusCode)
47+
{
48+
if (vendorResponse.StatusCode == System.Net.HttpStatusCode.NotFound)
49+
{
50+
return new NotFoundResult();
51+
}
52+
53+
// throws Exception
54+
vendorResponse.EnsureSuccessStatusCode();
55+
}
4156

42-
var vendorsJson = await client.GetStringAsync($"companies({companyId})/vendors");
43-
var vendors = JsonValue.Parse(vendorsJson);
44-
return new OkObjectResult(vendors?["value"]);
57+
var vendorJson = await vendorResponse.Content.ReadAsStringAsync();
58+
return new ContentResult()
59+
{
60+
Content = vendorJson,
61+
ContentType = "application/json"
62+
};
63+
}
64+
catch (HttpRequestException ex)
65+
{
66+
_logger.LogError(ex, "An error has occured while processing D365-BC-Vendors request.");
67+
return new StatusCodeResult(ex.StatusCode.HasValue ? (int)ex.StatusCode.Value : StatusCodes.Status500InternalServerError);
68+
}
4569
}
4670
}
4771
}

0 commit comments

Comments
 (0)