/// <summary> /// Sends a POST request to the server asynchronously. /// </summary> /// <param name="uri">The Uri where the request will be sent.</param> /// <returns>The server's response.</returns> public async Task <String> SendPostAsync(Uri uri) { if (uri == null) { throw new ArgumentException("Parameter uri must not be null. Please commit a valid Uri object."); } using (HttpResponseMessage response = await StravaHttpClient.PostAsync(uri, null)) { if (response != null) { AsyncResponseReceived?.Invoke(null, new AsyncResponseReceivedEventArgs(response)); CheckStravaApiUsage(response); _lastResponseCode = response.StatusCode; if (response.StatusCode == HttpStatusCode.OK) { return(await response.Content.ReadAsStringAsync()); } } } return(string.Empty); }
public async Task <string> SendPostAsync(Uri uri, string fileName) { if (uri == null) { throw new ArgumentException("Parameter uri must not be null. Please commit a valid Uri object."); } if (string.IsNullOrEmpty(fileName)) { throw new ArgumentException("Please provide a valid file name"); } FileInfo info = new FileInfo(fileName); MultipartFormDataContent content = new MultipartFormDataContent(); content.Add(new ByteArrayContent(File.ReadAllBytes(info.FullName)), "file", info.Name); using (HttpResponseMessage response = await StravaHttpClient.PostAsync(uri, content)) { if (response != null) { AsyncResponseReceived?.Invoke(null, new AsyncResponseReceivedEventArgs(response)); CheckStravaApiUsage(response); _lastResponseCode = response.StatusCode; if (response.StatusCode == HttpStatusCode.OK) { return(await response.Content.ReadAsStringAsync()); } } } return(string.Empty); }
private async Task <string> HandleStravaApiCallAsync(Task <HttpResponseMessage> asyncHttpCall) { using (HttpResponseMessage response = await asyncHttpCall) { if (response != null) { AsyncResponseReceived?.Invoke(null, new AsyncResponseReceivedEventArgs(response)); CheckStravaApiUsage(response); _lastResponseCode = response.StatusCode; if (response.StatusCode == HttpStatusCode.OK) { return(await response.Content.ReadAsStringAsync()); } } } return(string.Empty); }