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)); }
public void given_some_parameters_of_which_some_are_null_correct_url_built() { // Arrange var uri = new Uri("http://www.chronoresto.fr/"); // Act var result = UriBuildingHelpers.AttachParameters(uri, new KeyValuePair <string, string>("food", ""), new KeyValuePair <string, string>("code", "this")); // Assert Assert.That(result.ToString(), Is.EqualTo("http://www.chronoresto.fr/?code=this")); }
public async Task BuildRequest(string url, int timeoutSeconds, params 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(x => x.Key != null && x.Value != null).Select(x => new KeyValuePair <string, string>(x.Key, x.Value.ToString())).ToArray()); } await request.DeleteAsync(uri); }
public async Task <TReturnType> BuildRequest <TReturnType>(string url, int timeoutSeconds, params 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(x => x.Key != null && x.Value != null).Select(x => new KeyValuePair <string, string>(x.Key, x.Value.ToString())).ToArray()); } var result = await request.DeleteAsync(uri); var stringResult = await result.Content.ReadAsStringAsync(); Debug.WriteLine(stringResult); return(_requestSerializer.DeserializeObject <TReturnType>(stringResult)); }
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); }