private async Task <HttpResponseMessage> MakeRequest(string url, int timeoutSeconds, KeyValuePair <string, object>[] parameters) { var request = new HttpClient { Timeout = new TimeSpan(0, 0, timeoutSeconds) }; Uri uri = new Uri(url); uri = new Uri(uri + _methodName.ToLower()); if (parameters.Any()) { uri = UriBuildingHelpers.AttachParameters(uri, parameters.Where(UriBuildingHelpers.IsSimpleType) .Where(x => x.Key != null && x.Value != null) .Select(x => new KeyValuePair <string, string>(x.Key, UriBuildingHelpers.SimpleTypeToString(x))).ToArray()); } var postItem = parameters.FirstOrDefault(x => !UriBuildingHelpers.IsSimpleType(x)); Debug.WriteLine(uri.ToString() + "\r\n " + parameters.Aggregate(string.Empty, (x, y) => x + (y.Key + " " + y.Value + "\r\n"))); var content = new StringContent(_requestSerializer.SerializeObject(postItem.Value), Encoding.UTF8, "application/json"); return(await request.PostAsync(uri, content)); }
private async Task <string> DoGet(string url, int timeoutSeconds, KeyValuePair <string, object>[] parameters) { var request = new HttpClient { Timeout = new TimeSpan(0, 0, timeoutSeconds) }; Uri uri = new Uri(url); uri = new Uri(uri + _methodName.ToLower()); if (parameters.Any()) { // Get all simple Type Parameters var simpleTypeParameters = parameters .Where(x => x.Key != null && x.Value != null && UriBuildingHelpers.IsSimpleType(x)) .Select(x => new KeyValuePair <string, string>(x.Key, UriBuildingHelpers.SimpleTypeToString(x))) .ToArray(); // Add simple type parameters to Uri uri = UriBuildingHelpers.AttachParameters(uri, simpleTypeParameters); // Get all non-simple type parameters var enumerableParameters = parameters.Where(x => x.Key != null && x.Value != null && !UriBuildingHelpers.IsSimpleType(x)) .Select(x => new KeyValuePair <string, string>(x.Key, UriBuildingHelpers.EnumerableTypeToString(x))) .ToArray(); // Add all non-simple paramters to uri uri = UriBuildingHelpers.AttachParameters(uri, enumerableParameters); } Debug.WriteLine(uri.ToString() + "\r\n " + parameters .Where(x => x.Key != null && x.Value != null).Aggregate(string.Empty, (x, y) => x + (y.Key + " " + y.Value + "\r\n"))); var result = await request.GetStringAsync(uri); Debug.WriteLine(uri.ToString() + "SUCCESS \r\n " + parameters .Where(x => x.Key != null && x.Value != null).Aggregate(string.Empty, (x, y) => x + (y.Key + " " + y.Value + "\r\n"))); return(result); }