Last active
February 8, 2016 09:15
-
-
Save jorabra/a75cbe4402f128408a93 to your computer and use it in GitHub Desktop.
Combined search for main and under organization in the Brønnøysundregister.
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
using System; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Threading.Tasks; | |
using System.Web.Http; | |
using Newtonsoft.Json.Linq; | |
namespace Controllers | |
{ | |
public class OrgController : ApiController | |
{ | |
[Route("api/org/{orgnum}")] | |
[HttpGet] | |
public async Task<IHttpActionResult> LookupOrg(string orgnum) | |
{ | |
if (string.IsNullOrWhiteSpace(orgnum)) | |
{ | |
return Ok(); | |
} | |
var brregBaseAddress = "http://data.brreg.no/"; | |
HttpResponseMessage response = await LookupMainOrg(orgnum, brregBaseAddress); | |
if (!response.IsSuccessStatusCode) | |
{ | |
response = await LookupSubOrg(orgnum, brregBaseAddress); | |
} | |
if (response.IsSuccessStatusCode) | |
{ | |
var stringResponse = await response.Content.ReadAsStringAsync(); | |
JObject json = JObject.Parse(stringResponse); | |
return Ok(json); | |
} | |
return NotFound(); | |
} | |
private async Task<HttpResponseMessage> LookupMainOrg(string orgnum, string baseAddress) | |
{ | |
using (var client = new HttpClient()) | |
{ | |
client.BaseAddress = new Uri(baseAddress); | |
client.DefaultRequestHeaders.Accept.Clear(); | |
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | |
HttpResponseMessage response = await client.GetAsync("enhetsregisteret/enhet/" + orgnum + ".json"); | |
return response; | |
} | |
} | |
private async Task<HttpResponseMessage> LookupSubOrg(string orgnum, string baseAddress) | |
{ | |
using (var client = new HttpClient()) | |
{ | |
client.BaseAddress = new Uri(baseAddress); | |
client.DefaultRequestHeaders.Accept.Clear(); | |
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | |
HttpResponseMessage response = await client.GetAsync("enhetsregisteret/underenhet/" + orgnum + ".json"); | |
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
(function () { | |
'use strict'; | |
angular.module('app') | |
.controller('someApplicationController', controller); | |
function controller($scope, $http) { | |
function lookupOrgNum(orgnum) { | |
$http.get('api/org/' + orgnum). | |
success(function (data) { | |
$scope.form.organization.orgname = data.navn; | |
if (angular.isDefined(data.beliggenhetsadresse)) { // Underenhet | |
$scope.form.organization.visitAddress = data.beliggenhetsadresse.adresse; | |
$scope.form.organization.visitZip = data.beliggenhetsadresse.postnummer; | |
$scope.form.organization.visitCity = data.beliggenhetsadresse.poststed; | |
} | |
else if (angular.isDefined(data.forretningsadresse)) { // Hovedenhet | |
$scope.form.organization.visitAddress = data.forretningsadresse.adresse; | |
$scope.form.organization.visitZip = data.forretningsadresse.postnummer; | |
$scope.form.organization.visitCity = data.forretningsadresse.poststed; | |
} | |
$scope.form.organization.postalAddress = data.postadresse.adresse; | |
$scope.form.organization.postalZip = data.postadresse.postnummer; | |
$scope.form.organization.postalCity = data.postadresse.poststed; | |
}). | |
error(function (data, status) { | |
if (status === 404) { | |
$scope.form.organization.orgname = ""; | |
toaster.pop('error', '', 'Organisasjonsnummeret finnes ikke'); | |
} | |
}); | |
} | |
$scope.getOrgName = function () { | |
$scope.form.organization.orgname = ""; | |
if (typeof $scope.form.organization.orgnum === "undefined" || $scope.form.organization.orgnum < 800000000 || $scope.form.organization.orgnum > 990000000) { | |
return; | |
} | |
lookupOrgNum($scope.form.organization.orgnum); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment