/// <summary> /// Initializes a new instance of the <see cref="ApiMethod"/> class. /// </summary> /// <param name="methodUrl"> /// The url of the method to call. /// </param> /// <param name="oAuthToken"> /// The OAuth <see cref="Token"/> to use for the call. Can be <b>null</b> if the /// method does not require OAuth. /// </param> /// <param name="httpMethod"> /// The required <see cref="HttpMethod"/> for the Tumblr API call. Only GET and /// POST are supported. /// </param> /// <param name="parameters"> /// The parameters for the Tumblr API call. Can be <b>null</b> if the method does not require parameters. /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="methodUrl"/> is <b>null</b>. /// </exception> /// <exception cref="ArgumentException"> /// <list type="bullet"> /// <item> /// <description> /// <paramref name="methodUrl"/> is empty. /// </description> /// </item> /// <item> /// <description> /// <paramref name="httpMethod"/> is not Get or Post. /// </description> /// </item> /// </list> /// </exception> public ApiMethod( string methodUrl, Token oAuthToken, HttpMethod httpMethod, MethodParameterSet parameters = null) { if (methodUrl == null) { throw new ArgumentNullException("methodUrl"); } if (methodUrl.Length == 0) { throw new ArgumentException("Method URL cannot be empty.", "methodUrl"); } if (httpMethod != HttpMethod.Get && httpMethod != HttpMethod.Post) { throw new ArgumentException("The http method must be either GET or POST.", "httpMethod"); } this.methodUrl = methodUrl; this.httpMethod = httpMethod; this.oAuthToken = oAuthToken; this.parameters = parameters ?? new MethodParameterSet(); }
/// <summary> /// Converts the current instance to a <see cref="MethodParameterSet"/>/ /// </summary> /// <returns> /// A <see cref="MethodParameterSet"/>. /// </returns> public MethodParameterSet ToMethodParameterSet() { var result = new MethodParameterSet(parameters); if (Tags != null) { result.Add("tags", String.Join(",", Tags.ToArray())); } if (!String.IsNullOrEmpty(Tweet)) { result.Add("tweet", Tweet); } if (Date != null) { result.Add("date", Date.Value.ToUniversalTime().ToString("R")); } if (Format != PostFormat.Html) { result.Add("format", Format.ToString().ToLowerInvariant()); } if (!String.IsNullOrEmpty(Slug)) { result.Add("slug", Slug); } result.Add("state", State.ToString().ToLowerInvariant()); return(result); }
/// <summary> /// Initializes a new instance of the <see cref="BlogMethod"/> class. /// </summary> /// <param name="blogName"> /// The name of the blog target of the method call. Can be passed with or without the trailing ".tumblr.com". /// </param> /// <param name="methodName"> /// The name of the method to call. The method url will be automatically built using /// this information together with the <paramref name="blogName"/>. /// </param> /// <param name="oAuthToken"> /// The OAuth <see cref="Token"/> to use for the call. Can be <b>null</b> if the /// method does not require OAuth. /// </param> /// <param name="httpMethod"> /// The required <see cref="HttpMethod"/> for the Tumblr API call. Only <see cref="HttpMethod.Get"/> and /// <see cref="HttpMethod.Post"/> are supported. /// </param> /// <param name="parameters"> /// The parameters for the Tumblr API call. Can be <b>null</b> if the method does not require parameters. /// </param> /// <exception cref="ArgumentNullException"> /// <list type="bullet"> /// <item> /// <description> /// <paramref name="blogName"/> is <b>null</b>. /// </description> /// </item> /// <item> /// <description> /// <paramref name="methodName"/> is <b>null</b>. /// </description> /// </item> /// </list> /// </exception> /// <exception cref="ArgumentException"> /// <list type="bullet"> /// <item> /// <description> /// <paramref name="blogName"/> is empty. /// </description> /// </item> /// <item> /// <description> /// <paramref name="methodName"/> is empty. /// </description> /// </item> /// </list> /// </exception> public BlogMethod( string blogName, string methodName, Token oAuthToken, HttpMethod httpMethod, MethodParameterSet parameters = null) : base(String.Format("https://api.tumblr.com/v2/blog/{0}/{1}", Validate(blogName), methodName), oAuthToken, httpMethod, parameters) { if (blogName == null) { throw new ArgumentNullException("blogName"); } if (blogName.Length == 0) { throw new ArgumentException("Blog name cannot be empty.", "blogName"); } if (methodName == null) { throw new ArgumentNullException("methodName"); } if (methodName.Length == 0) { throw new ArgumentException("Method name cannot be empty.", "methodName"); } }
internal PostData(PostCreationState state, IEnumerable <string> tags) { parameters = new MethodParameterSet(); State = state; Tags = (tags != null && tags.FirstOrDefault() != null) ? new List <string>(tags) : new List <string>(); Format = PostFormat.Html; }
/// <summary> /// Initializes a new instance of the <see cref="UserMethod"/> class. /// </summary> /// <param name="methodName"> /// The name of the method to call. The method url will be automatically built. /// </param> /// <param name="oAuthToken"> /// The OAuth <see cref="Token"/> to use for the call. Can be <b>null</b> if the /// method does not require OAuth. /// </param> /// <param name="httpMethod"> /// The required <see cref="HttpMethod"/> for the Tumblr API call. Only <see cref="HttpMethod.Get"/> and /// <see cref="HttpMethod.Post"/> are supported. /// </param> /// <param name="parameters"> /// The parameters for the Tumblr API call. Can be <b>null</b> if the method does not require parameters. /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="methodName"/> is <b>null</b>. /// </exception> /// <exception cref="ArgumentException"> /// <paramref name="methodName"/> is empty. /// </exception> public UserMethod( string methodName, Token oAuthToken, HttpMethod httpMethod, MethodParameterSet parameters = null) : base(String.Format("https://api.tumblr.com/v2/user/{0}", methodName), oAuthToken, httpMethod, parameters) { if (methodName == null) { throw new ArgumentNullException("methodName"); } if (methodName.Length == 0) { throw new ArgumentException("Method name cannot be empty.", "methodName"); } }
/// <summary> /// Converts the current instance to a <see cref="MethodParameterSet"/>/ /// </summary> /// <returns> /// A <see cref="MethodParameterSet"/>. /// </returns> public MethodParameterSet ToMethodParameterSet() { var result = new MethodParameterSet(parameters); if (Tags != null) { result.Add("tags", String.Join(",", Tags.ToArray())); } if (!String.IsNullOrEmpty(Tweet)) { result.Add("tweet", Tweet); } if (Date != null) { result.Add("date", Date.Value.ToUniversalTime().ToString("R")); } if (Format != PostFormat.Html) { result.Add("format", Format.ToString().ToLowerInvariant()); } if (!String.IsNullOrEmpty(Slug)) { result.Add("slug", Slug); } result.Add("state", State.ToString().ToLowerInvariant()); if (State == PostCreationState.Queue) { if (Publish_On != null) { result.Add("publish_on", DateTimeHelper.ToTimestamp(Publish_On.Value)); } } return(result); }
/// <summary> /// set the authoriaztionheader for TumblrClient /// </summary> /// <param name="request"></param> /// <param name="hashProvider"></param> /// <param name="consumerKey"></param> /// <param name="consumerSecret"></param> /// <param name="oAuthToken"></param> /// <returns></returns> public static async Task PreparationForTumblrClient(this HttpRequestMessage request, IHmacSha1HashProvider hashProvider, string consumerKey, string consumerSecret, Token oAuthToken) { MethodParameterSet requestParameters = null; if (request.Content is FormUrlEncodedContent) { var formUrlEncoded = request.Content as FormUrlEncodedContent; string content = await formUrlEncoded.ReadAsStringAsync().ConfigureAwait(false); requestParameters = new MethodParameterSet(content); } else if (request.Content is MultipartFormDataContent multiPartContent) { requestParameters = new MethodParameterSet(); foreach (var c in multiPartContent) { if (c is StringContent stringContent) { requestParameters.Add(c.Headers.ContentDisposition.Name, await c.ReadAsStringAsync().ConfigureAwait(false)); } } } //if we have an api_key parameter we can skip the oauth if (requestParameters.FirstOrDefault(c => c.Name == "api_key") == null) { var authorizationHeaderParameters = new MethodParameterSet(requestParameters); if (oAuthToken != null) { authorizationHeaderParameters.Add("oauth_token", oAuthToken.Key); } authorizationHeaderParameters.Add("oauth_consumer_key", consumerKey); authorizationHeaderParameters.Add("oauth_nonce", Guid.NewGuid().ToString()); authorizationHeaderParameters.Add("oauth_timestamp", DateTimeHelper.ToTimestamp(DateTime.UtcNow).ToString()); authorizationHeaderParameters.Add("oauth_signature_method", "HMAC-SHA1"); authorizationHeaderParameters.Add("oauth_version", "1.0"); string urlParameters = authorizationHeaderParameters.ToFormUrlEncoded(); var requestUriNoQueryString = request.RequestUri.OriginalString; if (!String.IsNullOrEmpty(request.RequestUri.Query)) { requestUriNoQueryString = request.RequestUri.OriginalString.Replace(request.RequestUri.Query, String.Empty); } string signatureBaseString = String.Format("{0}&{1}&{2}", request.Method.ToString(), UrlEncoder.Encode(requestUriNoQueryString), UrlEncoder.Encode(urlParameters)); string signatureHash = hashProvider.ComputeHash(consumerSecret, oAuthToken?.Secret, signatureBaseString); authorizationHeaderParameters.Add("oauth_signature", signatureHash); foreach (IMethodParameter p in requestParameters) { //remove non-oauth parameters from the authorization header if (!p.Name.StartsWith("oauth")) { authorizationHeaderParameters.Remove(p); } } request.Headers.Authorization = new AuthenticationHeaderValue("OAuth", authorizationHeaderParameters.ToAuthorizationHeader()); } if (request.Method == HttpMethod.Get) { request.Content = null; //we don't have to send a body with get requests } }