Copy/Paste at your own risk!
/// <summary>
/// Requests a Partner by creating a new HttpClient instance and setting up the
/// header for Basic Authentication using a valid Aegis CRM username and password.
///
/// Example: var partner = GetPartner("123456789", "id");
///
/// </summary>
///
/// <param name="value">The value to look up</param>
/// <param name="type">The type of value to look up</param>
///
/// <returns>The partner as a JSON object</returns>
protected object GetPartner(string value, string type)
{
string baseAddress = "https://connect.aegispremier.com/api";
string context = "Please contact Aegis Premier Technologies if you have not yet been assigned a Context variable.";
string username = "AegisCRMUserName@example.com";
string password = "AegisCRMPassword";
string requestUri = String.Format("{0}/{1}/", baseAddress, context);
var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(requestUri);
// Set the data type to JSON and append the Aegis CRM login information to the Request header.
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue
(
"Basic",
Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Format("{0}:{1}", username, password)))
);
// Format the request
var apiRequest = String.Format("{0}/{1}/{2}", "partner", type, value);
// Send the request and retrieve the response.
var apiResponse = httpClient.GetAsync(apiRequest).Result;
if (!apiResponse.IsSuccessStatusCode)
{
var error = apiResponse.Content.ReadAsStringAsync().Result;
throw new Exception(error);
}
var json = Newtonsoft.Json.JsonConvert.DeserializeObject(apiResponse.Content.ReadAsStringAsync().Result);
return json;
}
/// <summary>
/// Requests a Partner by creating a new HttpClient instance and setting up the
/// header for Basic Authentication using a valid Aegis CRM username and password.
///
/// It then creates a new JSON object representing a Log In form, and POSTs it to
/// the API to retrieve the Partner object.
///
/// Example: var partner = Login("webUserName", "webPassword");
///
/// </summary>
///
/// <param name="partnerUserName">The partner's web username.</param>
/// <param name="partnerPassword">The partner's web password.</param>
///
/// <returns>The partner as a JSON object</returns>
protected object Login(string partnerUserName, string partnerPassword)
{
string baseAddress = "https://connect.aegispremier.com/api";
string context = "Please contact Aegis Premier Technologies if you have not yet been assigned a Context variable.";
string username = "AegisCRMUserName@example.com";
string password = "AegisCRMPassword";
string requestUri = String.Format("{0}/{1}/", baseAddress, context);
var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(requestUri);
// Set the data type to JSON and append the Aegis Premier Technologies login information to the Request header.
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue
(
"Basic",
Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Format("{0}:{1}", username, password)))
);
// Create a new Log In object to pass the partner's credentials
var webLogInCredentials = new { UserName = partnerUserName, Password = partnerPassword };
// Format the request
var apiRequest = String.Format("{0}/{1}", "partner", "login");
// Send the request and retrieve the response.
var apiResponse = httpClient.PostAsJsonAsync(apiRequest, webLogInCredentials).Result;
if (!apiResponse.IsSuccessStatusCode)
{
var error = apiResponse.Content.ReadAsAsync<HttpError>().Result;
throw new Exception(error.Message);
}
var json = Newtonsoft.Json.JsonConvert.DeserializeObject(apiResponse.Content.ReadAsStringAsync().Result);
return json;
}