public void Fill(CompaniesHouseOfficerOrderItem model) { model.Name = name; model.Nationality = nationality; model.AppointedOn = appointed_on; model.CountryOfResidence = country_of_residence; model.Link = links != null && links.officer != null ? links.officer.appointments : null; model.Occupation = occupation; model.OfficerRole = officer_role; model.ResignedOn = resigned_on; if (address != null) { model.AddressLine1 = address.address_line_1; model.AddressLine2 = address.address_line_2; model.CareOf = address.care_of; model.Country = address.country; model.Locality = address.locality; model.PoBox = address.po_box; model.Postcode = address.postal_code; model.Premises = address.premises; model.Region = address.region; } //if if (date_of_birth != null) { model.DobDay = date_of_birth.day; model.DobMonth = date_of_birth.month; model.DobYear = date_of_birth.year; } //if model.AppointmentOrder = new CompaniesHouseOfficerAppointmentOrder(); } //Fill
} //CheckCache private void RetrieveDataFromCompaniesApi(string companyRef) { var model = new CompaniesHouseOfficerOrder { CompanyRefNum = companyRef, Timestamp = DateTime.UtcNow, Officers = new List <CompaniesHouseOfficerOrderItem>() }; try { HttpClient httpClient = new HttpClient(); httpClient.BaseAddress = new Uri("https://api.companieshouse.gov.uk/"); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(ConfigManager.CurrentValues.Instance.CompaniesHouseApiKey))); const int itemsPerPage = 35; //default int startIndexOfficers = 0; bool shouldRetrieveOfficers = true; do { //Get officers of company by ref num var result = httpClient.GetAsync(string.Format("company/{0}/officers/?items_per_page={1}&start_index={2}", companyRef, itemsPerPage, startIndexOfficers)) .Result.Content.ReadAsStringAsync() .Result; var officersList = JsonConvert.DeserializeObject <OfficersListResult>(result); if (officersList != null) { officersList.Fill(model); foreach (var officer in officersList.items) { var officerModel = new CompaniesHouseOfficerOrderItem(); officer.Fill(officerModel); if (officer.links != null && officer.links.officer != null && !string.IsNullOrEmpty(officer.links.officer.appointments)) { int startIndexAppointments = 0; bool shouldRetrieveAppointments = true; do { //Get appointments by officer ref num var appointmentsResult = httpClient.GetAsync(string.Format("{0}/?items_per_page={1}&start_index={2}", officer.links.officer.appointments, itemsPerPage, startIndexAppointments)) .Result.Content.ReadAsStringAsync() .Result; var appointment = JsonConvert.DeserializeObject <AppointmentListResult>(appointmentsResult); if (appointment != null) { appointment.Fill(officerModel.AppointmentOrder); } if (appointment != null && appointment.total_results > (startIndexAppointments + itemsPerPage)) { startIndexAppointments += itemsPerPage; } else { shouldRetrieveAppointments = false; } } while (shouldRetrieveAppointments); } //if model.Officers.Add(officerModel); } //foreach } //if if (officersList != null && officersList.total_results > (startIndexOfficers + itemsPerPage)) { startIndexOfficers += itemsPerPage; } else { shouldRetrieveOfficers = false; } } while (shouldRetrieveOfficers); } catch (Exception ex) { Log.Error(ex, "Failed to retrieve data from companies house for company {0}", companyRef); } SaveToDB(model); Result = model; } //RetrieveDataFromCompaniesData