public void SerializeDateAndTimeValues_MatchesExpected(Session session, Dictionary <string, string> expectedResult) { // Note that the usage of the Session class here is because it has date and time fields var result = serializer.Serialize(session); var serialized = JsonConvert.SerializeObject( expectedResult, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, } ); // Strip the { } from start and end of serialized substring Assert.Contains(serialized.TrimStart('{').TrimEnd('}'), result); }
/// <summary> /// Calls a method of the iVvy api. Encapsulates the header and /// signing requirements. /// </summary> public async Task <ResultOrError <T> > CallAsync <T>( string apiNamespace, string action, object requestData) where T : new() { if (ApiVersion != "1.0") { throw new ArgumentException("ApiVersion is not a valid version number"); } var postData = ""; if (requestData != null) { postData = serializer.Serialize(requestData); } var method = "POST"; var contentType = "application/json; charset=utf-8"; var contentMD5 = Utils.GetMD5Hash(postData); var date = ""; var requestUri = "/api/" + ApiVersion + "/" + apiNamespace + "?action=" + action; var ivvyDate = DateTime.Now.ToUniversalTime().ToString(Utils.DateTimeFormat); var headersToSign = "IVVYDate=" + ivvyDate; var initialStringToSign = method + contentMD5 + contentType + date + requestUri + ApiVersion + headersToSign; var stringToSign = initialStringToSign.ToLower(); var signature = Utils.SignString(stringToSign, ApiSecret); var message = new HttpRequestMessage(HttpMethod.Post, BaseUrl + requestUri); message.Headers.Clear(); message.Content = new StringContent(postData, Encoding.UTF8, "application/json"); message.Content.Headers.Add("Content-MD5", contentMD5); message.Content.Headers.Add("IVVY-Date", ivvyDate); message.Content.Headers.Add("X-Api-Authorization", "IWS " + ApiKey + ":" + signature); HttpResponseMessage httpResponse = null; ResultOrError <T> result = null; try { var httpClient = httpClientWrapper.GetHttpClient(); httpResponse = await httpClient.SendAsync(message); var data = await httpResponse.Content.ReadAsStringAsync(); await Events.AfterApiCalledAsync(new ApiCallDetails( apiNamespace, action, message.Headers, message.Content?.Headers, postData, httpResponse.StatusCode, httpResponse.Headers, data )); result = deserializer.Deserialize <T>(data); if (result == null) { result = new ResultOrError <T>() { ErrorCode = libErrorCode, ErrorCodeSpecific = "CallAsync", ErrorMessage = "Received invalid response.", }; } } catch (Exception ex) { result = new ResultOrError <T>() { ErrorCode = libErrorCode, ErrorCodeSpecific = "CallAsync", ErrorMessage = ex.Message, }; } finally { if (httpResponse != null) { httpResponse.Dispose(); } } return(result); }