internal static async Task <string> ResetPasswordAsync(ResetPasswordRequest data) { var rb = new RequestBuilder() { Method = HttpConsts.Post, BaseUri = new Uri(ResetPasswordUrl), }; rb.AddParameter(RequestParameterType.Query, "key", IntegrationTestUtils.GetApiKey()); var payload = JsonParser.Serialize(data); var request = rb.CreateRequest(); request.Content = new StringContent(payload, Encoding.UTF8, "application/json"); var response = await SendAndDeserialize <Dictionary <string, object> >(request); return((string)response["email"]); }
public string GetTweet(string id, string expansions, string tweet_fields, string media_fields, string poll_fields, string place_fields, string user_fields) { RequestBuilder rb = new RequestBuilder(_oAuthInfo, "GET", _tweetEndpoint + id); rb.AddParameter("expansions", expansions); rb.AddParameter("tweet.fields", tweet_fields); rb.AddParameter("media.fields", media_fields); rb.AddParameter("place.fields", place_fields); rb.AddParameter("poll.fields", poll_fields); rb.AddParameter("user.fields", user_fields); return(rb.Execute()); }
/// <summary> /// Reflectively enumerate the properties of this object looking for all properties containing the /// RequestParameterAttribute and copy their values into the request builder. /// </summary> private void SetAllPropertyValues(RequestBuilder requestBuilder) { Type myType = this.GetType(); var properties = myType.GetProperties(); foreach (var property in properties) { var attribute = property.GetCustomAttribute<Google.Apis.Util.RequestParameterAttribute>(); if (attribute != null) { string name = attribute.Name ?? property.Name.ToLower(); object value = property.GetValue(this, new object[] { }); if (value != null) { requestBuilder.AddParameter(attribute.Type, name, value.ToString()); } } } }
/// <summary> /// The core download logic. It downloads the media in parts, where each part's size is defined by /// <see cref="ChunkSize"/> (in bytes). /// </summary> /// <param name="url">The URL of the resource to download.</param> /// <param name="stream">The download will download the resource into this stream.</param> /// <param name="cancellationToken">A cancellation token to cancel this download in the middle.</param> /// <returns>A task with the download progress object. If an exception occurred during the download, its /// <see cref="IDownloadProgress.Exception "/> property will contain the exception.</returns> private async Task <IDownloadProgress> DownloadCoreAsync(string url, Stream stream, CancellationToken cancellationToken) { url.ThrowIfNull("url"); stream.ThrowIfNull("stream"); if (!stream.CanWrite) { throw new ArgumentException("stream doesn't support write operations"); } RequestBuilder builder = null; var uri = new Uri(url); if (string.IsNullOrEmpty(uri.Query)) { builder = new RequestBuilder() { BaseUri = new Uri(url) }; } else { builder = new RequestBuilder() { BaseUri = new Uri(url.Substring(0, url.IndexOf("?"))) }; // Remove '?' at the beginning. var query = uri.Query.Substring(1); var pairs = from parameter in query.Split('&') select parameter.Split('='); // Add each query parameter. each pair contains the key [0] and then its value [1]. foreach (var p in pairs) { builder.AddParameter(RequestParameterType.Query, p[0], p[1]); } } builder.AddParameter(RequestParameterType.Query, "alt", "media"); long currentRequestFirstBytePos = 0; try { // This "infinite" loop stops when the "to" byte position in the "Content-Range" header is the last // byte of the media ("length"-1 in the "Content-Range" header). // e.g. "Content-Range: 200-299/300" - "to"(299) = "length"(300) - 1. while (true) { var currentRequestLastBytePos = currentRequestFirstBytePos + ChunkSize - 1; // Create the request and set the Range header. var request = builder.CreateRequest(); request.Headers.Range = new RangeHeaderValue(currentRequestFirstBytePos, currentRequestLastBytePos); using (var response = await service.HttpClient.SendAsync(request, cancellationToken). ConfigureAwait(false)) { // Read the content and copy to the parameter's stream. var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); responseStream.CopyTo(stream); // Read the headers and check if all the media content was already downloaded. var contentRange = response.Content.Headers.ContentRange; long mediaContentLength; if (contentRange == null) { // Content range is null when the server doesn't adhere the media download protocol, in // that case we got all the media in one chunk. currentRequestFirstBytePos = mediaContentLength = response.Content.Headers.ContentLength.Value; } else { currentRequestFirstBytePos = contentRange.To.Value + 1; mediaContentLength = contentRange.Length.Value; } if (currentRequestFirstBytePos == mediaContentLength) { var progress = new DownloadProgress(DownloadStatus.Completed, mediaContentLength); UpdateProgress(progress); return(progress); } } UpdateProgress(new DownloadProgress(DownloadStatus.Downloading, currentRequestFirstBytePos)); } } catch (TaskCanceledException ex) { Logger.Error(ex, "Download media was canceled"); UpdateProgress(new DownloadProgress(ex, currentRequestFirstBytePos)); throw ex; } catch (Exception ex) { Logger.Error(ex, "Exception occurred while downloading media"); var progress = new DownloadProgress(ex, currentRequestFirstBytePos); UpdateProgress(progress); return(progress); } }
/// <summary> /// Sets query parameters in the given builder with all all properties with the /// <seealso cref="Google.Apis.Util.RequestParameterAttribute"/> attribute. /// </summary> /// <param name="builder">The request builder</param> /// <param name="request"> /// A request object which contains properties with /// <seealso cref="Google.Apis.Util.RequestParameterAttribute"/> attribute. Those properties will be set in the /// given request builder object /// </param> public static void InitParameters(RequestBuilder builder, object request) { IterateParameters(request, (type, name, value) => { builder.AddParameter(type, name, value.ToString()); }); }
/// <summary> /// The core download logic. It downloads the media in parts, where each part's size is defined by /// <see cref="ChunkSize"/> (in bytes). /// </summary> /// <param name="url">The URL of the resource to download.</param> /// <param name="stream">The download will download the resource into this stream.</param> /// <param name="cancellationToken">A cancellation token to cancel this download in the middle.</param> /// <returns>A task with the download progress object. If an exception occurred during the download, its /// <seealso cref="IDownloadProgress.Exception "/> property will contain the exception.</returns> private async Task <IDownloadProgress> DownloadCore(string url, Stream stream, CancellationToken cancellationToken) { // validate the parameters url.ThrowIfNull("url"); stream.ThrowIfNull("stream"); if (!stream.CanWrite) { throw new ArgumentException("stream doesn't support write operations"); } var builder = new RequestBuilder() { BaseUri = new Uri(url) }; builder.AddParameter(RequestParameterType.Query, "alt", "media"); long currentRequestFirstBytePos = 0; try { // this "infinite" loop stops when the "to" byte position in the "Content-Range" header is the last // byte of the media ("length"-1 in the "Content-Range" header). // e.g. "Content-Range: 200-299/300" - "to"(299) = "length"(300) - 1. while (true) { cancellationToken.ThrowIfCancellationRequested(); var currentRequestLastBytePos = currentRequestFirstBytePos + ChunkSize - 1; // create the request and set the Range header var request = builder.CreateRequest(); request.Headers.Range = new RangeHeaderValue(currentRequestFirstBytePos, currentRequestLastBytePos); using (var response = await service.HttpClient.SendAsync(request, cancellationToken). ConfigureAwait(false)) { // read the content and copy to the parameter's stream var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); responseStream.CopyTo(stream); // read the headers and check if all the media content was already downloaded var contentRange = response.Content.Headers.ContentRange; currentRequestFirstBytePos = contentRange.To.Value + 1; long mediaContentLength = contentRange.Length.Value; if (currentRequestFirstBytePos == mediaContentLength) { var progress = new DownloadProgress(DownloadStatus.Completed, mediaContentLength); UpdateProgress(progress); return(progress); } } UpdateProgress(new DownloadProgress(DownloadStatus.Downloading, currentRequestFirstBytePos)); } } catch (Exception ex) { var progress = new DownloadProgress(ex, currentRequestFirstBytePos); UpdateProgress(progress); return(progress); } }
public string GetMentionedTimeline(string id, DateTime?endtime, string expansions, int maxResults, string mediafields, string pagination_token, string placefields, string pollfields, string since_id, DateTime?start_time, string tweet_fields, string until_id, string user_fields) { _userMentionTimeLine = _userMentionTimeLine.Replace(":id", id); RequestBuilder rb = new RequestBuilder(_oAuthInfo, "GET", _userMentionTimeLine); if (endtime.HasValue) { rb.AddParameter("end_time", endtime.Value.ToString()); } rb.AddParameter("expansions", expansions); if (maxResults > 0) { rb.AddParameter("max_results", maxResults.ToString()); } if (!string.IsNullOrEmpty(mediafields)) { rb.AddParameter("media.fields", mediafields); } if (!string.IsNullOrEmpty(pagination_token)) { rb.AddParameter("pagination_token", pagination_token); } if (!string.IsNullOrEmpty(placefields)) { rb.AddParameter("place.fields", placefields); } if (!string.IsNullOrEmpty(pollfields)) { rb.AddParameter("poll.fields", pollfields); } if (!string.IsNullOrEmpty(since_id)) { rb.AddParameter("since_id", since_id); } if (start_time.HasValue) { rb.AddParameter("start_time", start_time.Value.ToString()); } if (!string.IsNullOrEmpty(tweet_fields)) { rb.AddParameter("tweet.fields", tweet_fields); } if (!string.IsNullOrEmpty(until_id)) { rb.AddParameter("until_id", until_id); } if (!string.IsNullOrEmpty(user_fields)) { rb.AddParameter("user.fields", user_fields); } string result = rb.Execute(); return(result); }