/// <summary> /// Uploads the addresses. /// </summary> /// <param name="addresses">The addresses.</param> /// <param name="id">Name of the file.</param> public void UploadAddresses(Dictionary <int, PersonAddressItem> addresses, string id) { try { PersonAddressItem[] addressArray = addresses.Values.ToArray(); StringBuilder data = new StringBuilder(); for (int i = 1; i <= addressArray.Length; i++) { PersonAddressItem personAddressItem = addressArray[i - 1]; if (personAddressItem.Country.ToUpperInvariant() != "US" || personAddressItem.State.Length == 1 || personAddressItem.State.Length > 2) { continue; // Only support US addresses } data.AppendFormat("{0}={1}&", "individual_id", $"{personAddressItem.PersonId}_{personAddressItem.PersonAliasId}_{personAddressItem.FamilyId}_{personAddressItem.LocationId}"); data.AppendFormat("{0}={1}&", "individual_first_name", personAddressItem.FirstName); data.AppendFormat("{0}={1}&", "individual_last_name", personAddressItem.LastName); data.AppendFormat("{0}={1}&", "address_line_1", personAddressItem.Street1); data.AppendFormat("{0}={1}&", "address_line_2", personAddressItem.Street2); data.AppendFormat("{0}={1}&", "address_city_name", personAddressItem.City); data.AppendFormat("{0}={1}&", "address_state_code", personAddressItem.State); data.AppendFormat("{0}={1}&", "address_postal_code", personAddressItem.PostalCode); // data.AppendFormat( "{0}={1}&", "address_country_code", personAddressItem.Country ); if (i % _batchsize == 0 || i == addressArray.Length) { var request = new RestRequest($"api/files/{id}/records", Method.POST); request.AddParameter("application/x-www-form-urlencoded", data.ToString().TrimEnd('&'), ParameterType.RequestBody); IRestResponse response = _client.Execute(request); if (response.StatusCode != HttpStatusCode.OK) { throw new HttpResponseException(new HttpResponseMessage(response.StatusCode) { Content = new StringContent(response.Content) }); } data = new StringBuilder(); } } } catch (Exception ex) { throw new AggregateException("Communication with NCOA server failed: Could not upload addresses to the NCOA server. Possible cause is one or more addresses are not in a valid format or invalid state code.", ex); } try { var request = new RestRequest($"api/files/{id}/index", Method.GET); IRestResponse response = _client.Execute(request); if (response.StatusCode != HttpStatusCode.OK) { throw new HttpResponseException(new HttpResponseMessage(response.StatusCode) { Content = new StringContent(response.Content) }); } NcoaResponse file; try { file = JsonConvert.DeserializeObject <NcoaResponse>(response.Content); } catch { throw new Exception($"Failed to deserialize NCOA response: {response.Content}"); } if (file.Status != "Mapped") { throw new Exception($"NCOA is not in the correct state: {file.Status}"); } } catch (Exception ex) { throw new AggregateException("Communication with NCOA server failed: Could not check upload status. Possible causes are one or more addresses are not in a valid format or invalid state code; or server is in invalid state.", ex); } }
/// <summary> /// Uploads the addresses. /// </summary> /// <param name="addresses">The addresses.</param> /// <param name="id">Name of the file.</param> public void UploadAddresses(Dictionary <int, PersonAddressItem> addresses, string id) { try { string directory = AppDomain.CurrentDomain.BaseDirectory; directory = Path.Combine(directory, "App_Data", "Logs"); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } string filePath = Path.Combine(directory, "NcoaException.log"); File.WriteAllText(filePath, string.Empty); PersonAddressItem[] addressArray = addresses.Values.ToArray(); StringBuilder data = new StringBuilder(); for (int i = 1; i <= addressArray.Length; i++) { PersonAddressItem personAddressItem = addressArray[i - 1]; data.AppendFormat("{0}={1}&", "individual_id", $"{personAddressItem.PersonId}_{personAddressItem.PersonAliasId}_{personAddressItem.FamilyId}_{personAddressItem.LocationId}"); data.AppendFormat("{0}={1}&", "individual_first_name", personAddressItem.FirstName); data.AppendFormat("{0}={1}&", "individual_last_name", personAddressItem.LastName); data.AppendFormat("{0}={1}&", "address_line_1", personAddressItem.Street1); data.AppendFormat("{0}={1}&", "address_line_2", personAddressItem.Street2); data.AppendFormat("{0}={1}&", "address_city_name", personAddressItem.City); data.AppendFormat("{0}={1}&", "address_state_code", personAddressItem.State); data.AppendFormat("{0}={1}&", "address_postal_code", personAddressItem.PostalCode); // data.AppendFormat( "{0}={1}&", "address_country_code", personAddressItem.Country ); if (i % _batchsize == 0 || i == addressArray.Length) { var request = new RestRequest($"files/{id}/records", Method.POST); request.AddParameter("application/x-www-form-urlencoded", data.ToString().TrimEnd('&'), ParameterType.RequestBody); IRestResponse response = _client.Execute(request); try { File.AppendAllText(filePath, $"{data.ToString().TrimEnd( '&' )}{Environment.NewLine}Status Code: {response.StatusCode}{Environment.NewLine}Response: {response.Content.ToStringSafe()}{Environment.NewLine}"); } catch { } if (response.StatusCode != HttpStatusCode.OK) { throw new Exception($"Failed to upload addresses to NCOA. Status Code: {response.StatusCode}, Response: {response.Content.ToStringSafe()}"); } data = new StringBuilder(); } } } catch (Exception ex) { throw new AggregateException("Communication with NCOA server failed: Could not upload addresses to the NCOA server. Possible cause is one or more addresses are not in a valid format or invalid state code.", ex); } try { var request = new RestRequest($"files/{id}/index", Method.GET); IRestResponse response = _client.Execute(request); if (response.StatusCode != HttpStatusCode.OK) { throw new Exception($"Failed to upload addresses to NCOA. Status Code: {response.StatusCode}, Response: {response.Content.ToStringSafe()}"); } NcoaResponse file; try { file = JsonConvert.DeserializeObject <NcoaResponse>(response.Content); } catch { throw new Exception($"Failed to deserialize NCOA response: {response.Content}"); } if (file.Status != "Mapped") { throw new Exception($"NCOA is not in the correct state: {file.Status}"); } } catch (Exception ex) { throw new AggregateException("Communication with NCOA server failed: Could not check upload status. Possible causes are one or more addresses are not in a valid format or invalid state code; or server is in invalid state.", ex); } }