Skip to content

Commit

Permalink
Merge branch 'release/1.0.8-individual-items-from-dataverse' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Kozlov committed Dec 5, 2024
2 parents 0d700c7 + 139d29d commit bd62a97
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 12 deletions.
40 changes: 35 additions & 5 deletions FunctionApp/Dynamics365/CRM/Accounts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,45 @@ public Accounts(HttpClientProvider httpClientProvider, ILogger<Accounts> logger)
}

[Function("D365-CRM-Accounts")]
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest req)
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = "crm/accounts/{id?}")] HttpRequest req, Guid? id)
{
_logger.LogInformation("Dynamics365-CRM-Accounts is requested.");

var client = _httpClientProvider.Create();
var accountsJson = await client.GetStringAsync("accounts");
var accounts = JsonValue.Parse(accountsJson);
try
{
var client = _httpClientProvider.Create();

return new OkObjectResult(accounts?["value"]);
if (!id.HasValue)
{
var accountsJson = await client.GetStringAsync("accounts?$select=accountid,name");
var accounts = JsonValue.Parse(accountsJson);
return new OkObjectResult(accounts?["value"]);
}

var accountResponse = await client.GetAsync($"accounts({id})");
if (!accountResponse.IsSuccessStatusCode)
{
if (accountResponse.StatusCode == System.Net.HttpStatusCode.NotFound)
{
return new NotFoundResult();
}

// throws Exception
accountResponse.EnsureSuccessStatusCode();
}

var accountJson = await accountResponse.Content.ReadAsStringAsync();
return new ContentResult()
{
Content = accountJson,
ContentType = "application/json"
};
}
catch (HttpRequestException ex)
{
_logger.LogError(ex, "An error has occured while processing Dynamics365-CRM-Accounts request.");
return new StatusCodeResult(ex.StatusCode.HasValue ? (int)ex.StatusCode.Value : StatusCodes.Status500InternalServerError);
}
}
}
}
2 changes: 1 addition & 1 deletion FunctionApp/Dynamics365/CRM/Authorize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public Authorize(IOptions<AppSettings> settings, ILogger<Authorize> logger)
}

[Function("D365-CRM-Authorize")]
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "crm/authorize")] HttpRequest req)
{
var scopes = new string[] { $"https://admin.services.crm.dynamics.com/user_impersonation", "offline_access" };

Expand Down
41 changes: 36 additions & 5 deletions FunctionApp/Dynamics365/CRM/Contacts.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Grpc.Core;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
Expand All @@ -18,15 +19,45 @@ public Contacts(HttpClientProvider httpClientProvider, ILogger<Contacts> logger)
}

[Function("D365-CRM-Contacts")]
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest req)
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = "crm/contacts/{id?}")] HttpRequest req, Guid? id)
{
_logger.LogInformation("Dynamics365-CRM-Contacts is requested.");

var client = _httpClientProvider.Create();
var contactsJson = await client.GetStringAsync("contacts");
var contacts = JsonValue.Parse(contactsJson);
try
{
var client = _httpClientProvider.Create();

return new OkObjectResult(contacts?["value"]);
if (!id.HasValue)
{
var contactsJson = await client.GetStringAsync("contacts?$select=contactid,fullname");
var contacts = JsonValue.Parse(contactsJson);
return new OkObjectResult(contacts?["value"]);
}

var contactResponse = await client.GetAsync($"contacts({id})");
if (!contactResponse.IsSuccessStatusCode)
{
if (contactResponse.StatusCode == System.Net.HttpStatusCode.NotFound)
{
return new NotFoundResult();
}

// throws Exception
contactResponse.EnsureSuccessStatusCode();
}

var contactJson = await contactResponse.Content.ReadAsStringAsync();
return new ContentResult()
{
Content = contactJson,
ContentType = "application/json"
};
}
catch (HttpRequestException ex)
{
_logger.LogError(ex, "An error has occured while processing Dynamics365-CRM-Contacts request.");
return new StatusCodeResult(ex.StatusCode.HasValue ? (int)ex.StatusCode.Value : StatusCodes.Status500InternalServerError);
}
}
}
}
2 changes: 1 addition & 1 deletion FunctionApp/Dynamics365/CRM/HttpClientProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public HttpClientProvider(IOptions<AppSettings> settings)
public HttpClient Create()
{
var client = new HttpClient(new OAuthMessageHandler(_azureAppSettings, new HttpClientHandler()));
client.BaseAddress = new Uri($"{_azureAppSettings.DynamicsUrl}/api/data/v9.1/");
client.BaseAddress = new Uri($"{_azureAppSettings.DynamicsUrl}/api/data/v9.2/");
client.Timeout = new TimeSpan(0, 2, 0);
client.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
client.DefaultRequestHeaders.Add("OData-Version", "4.0");
Expand Down

0 comments on commit bd62a97

Please sign in to comment.