Skip to content

Commit

Permalink
Merge pull request #1 from data-altinn-no/feat/aareg
Browse files Browse the repository at this point in the history
feat: employment history
  • Loading branch information
erlendoksvoll authored Oct 24, 2024
2 parents 7137a67 + d68bc06 commit 1ac5ef5
Show file tree
Hide file tree
Showing 26 changed files with 426 additions and 264 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build-deploy-workflow.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Deploy dan-plugin-DATASOURCENAME
name: Deploy dan-plugin-Nav

on:
push:
Expand All @@ -12,8 +12,8 @@ jobs:
run:
uses: data-altinn-no/deploy-actions/.github/workflows/dan-deploy-flow.yml@main
with:
artifact_name: 'dan-plugin-DATASOURCENAME' # Can be omitted, defaults to 'artifact'
function_project_path: 'src/Dan.Plugin.DATASOURCENAME'
artifact_name: 'dan-plugin-Nav' # Can be omitted, defaults to 'artifact'
function_project_path: 'src/Dan.Plugin.Nav'
secrets:
function_app_name: ${{ secrets.FUNCTIONAPP_NAME }}
publish_profile: ${{ secrets.AZURE_FUNCTION_PUBLISH_CREDS }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

# Azure Functions localsettings file
local.settings.json
/.idea

# User-specific files
*.rsuser
Expand Down
4 changes: 2 additions & 2 deletions Dan.Plugin.DATASOURCENAME.sln → Dan.Plugin.Nav.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31717.71
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dan.Plugin.DATASOURCENAME.Test", "test\Dan.Plugin.DATASOURCENAME.Test\Dan.Plugin.DATASOURCENAME.Test.csproj", "{B3D46531-F347-4F9E-AD08-6A93E02A7E48}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dan.Plugin.Nav.Test", "test\Dan.Plugin.Nav.Test\Dan.Plugin.Nav.Test.csproj", "{B3D46531-F347-4F9E-AD08-6A93E02A7E48}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dan.Plugin.DATASOURCENAME", "src\Dan.Plugin.DATASOURCENAME\Dan.Plugin.DATASOURCENAME.csproj", "{EC1D3A58-C881-442C-8C4A-B1D2A2535F6B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dan.Plugin.Nav", "src\Dan.Plugin.Nav\Dan.Plugin.Nav.csproj", "{EC1D3A58-C881-442C-8C4A-B1D2A2535F6B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
File renamed without changes.
91 changes: 0 additions & 91 deletions src/Dan.Plugin.DATASOURCENAME/Metadata.cs

This file was deleted.

12 changes: 0 additions & 12 deletions src/Dan.Plugin.DATASOURCENAME/Models/ExampleModel.cs

This file was deleted.

146 changes: 0 additions & 146 deletions src/Dan.Plugin.DATASOURCENAME/Plugin.cs

This file was deleted.

97 changes: 97 additions & 0 deletions src/Dan.Plugin.Nav/Clients/NavClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Dan.Common;
using Dan.Common.Exceptions;
using Dan.Common.Models;
using Dan.Plugin.Nav.Config;
using Dan.Plugin.Nav.Models;
using Dan.Plugin.Nav.Services;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;

namespace Dan.Plugin.Nav.Clients;

public interface INavClient
{
Task<NavEmployeeQueryResponse> GetEmploymentHistory(EvidenceHarvesterRequest req);
}

public class NavClient(
IHttpClientFactory clientFactory,
IOptions<Settings> settings,
ILoggerFactory loggerFactory,
IGraphQlRequestService graphQlRequestService) : INavClient
{
private readonly HttpClient _client = clientFactory.CreateClient(Constants.SafeHttpClient);
private readonly Settings _settings = settings.Value;
private readonly ILogger<NavClient> _logger = loggerFactory.CreateLogger<NavClient>();

public async Task<NavEmployeeQueryResponse> GetEmploymentHistory(EvidenceHarvesterRequest req)
{
var baseUrl = _settings.NavUrl;
const string path = "/aareg/v1/arbeidsforhold/graphql";
var url = $"{baseUrl}{path}";

var requestBody =
graphQlRequestService.CreateEmploymentHistoryRequest(req.SubjectParty!.NorwegianSocialSecurityNumber);
var request = GetRequest(url, HttpMethod.Post, req.MPToken, requestBody);

var response = await MakeRequest<NavEmployeeQueryResponse>(request);

return response;
}

private static HttpRequestMessage GetRequest<T>(string url, HttpMethod method, string mpToken, T body = null) where T : class
{
var uriBuilder = new UriBuilder(url);
var uri = uriBuilder.ToString();
var request = new HttpRequestMessage(method, uri);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", mpToken);

if (body == null) return request;

var jsonbody = JsonConvert.SerializeObject(body);
var content = new StringContent(jsonbody, Encoding.UTF8, "application/json");
request.Content = content;

return request;
}

private async Task<T> MakeRequest<T>(HttpRequestMessage request)
{
HttpResponseMessage result;
try
{
result = await _client.SendAsync(request);
}
catch (HttpRequestException ex)
{
throw new EvidenceSourceTransientException(PluginConstants.ErrorUpstreamUnavailble, "Error communicating with upstream source", ex);
}

if (!result.IsSuccessStatusCode)
{
throw result.StatusCode switch
{
HttpStatusCode.NotFound => new EvidenceSourcePermanentClientException(PluginConstants.ErrorNotFound, "Upstream source could not find the requested entity (404)"),
HttpStatusCode.BadRequest => new EvidenceSourcePermanentClientException(PluginConstants.ErrorInvalidInput, "Upstream source indicated an invalid request (400)"),
_ => new EvidenceSourceTransientException(PluginConstants.ErrorUpstreamUnavailble, $"Upstream source retuned an HTTP error code ({(int)result.StatusCode})")
};
}

try
{
return JsonConvert.DeserializeObject<T>(await result.Content.ReadAsStringAsync());
}
catch (Exception ex)
{
_logger.LogError("Unable to parse data returned from upstream source: {exceptionType}: {exceptionMessage}", ex.GetType().Name, ex.Message);
throw new EvidenceSourcePermanentServerException(PluginConstants.ErrorUnableToParseResponse, "Could not parse the data model returned from upstream source", ex);
}
}
}
Loading

0 comments on commit 1ac5ef5

Please sign in to comment.