Exemplo n.º 1
0
        internal virtual FluentHttpRequest PrepareRequest(GithubApiVersion version, string method, string path, object parameters, Stream responseStream)
        {
            var request = new FluentHttpRequest()
                .ResourcePath(path)
                .Method(method)
                .Proxy(Proxy)
                .OnResponseHeadersReceived((o, e) => e.SaveResponseIn(responseStream));

            request.BaseUrl(version == GithubApiVersion.V3
                                ? "https://api.github.com"
                                : "https://github.com/api/v2/json");

            string bearerToken = null;

            IDictionary<string, object> dictionaryParameters = null;
            if (parameters is IDictionary<string, object>)
                dictionaryParameters = (IDictionary<string, object>)parameters;

            // give priority to bearer_token then access_token specified in parameters.
            if (dictionaryParameters != null)
            {
                if (dictionaryParameters.ContainsKey("bearer_token"))
                {
                    bearerToken = dictionaryParameters["bearer_token"].ToString();
                    dictionaryParameters.Remove(bearerToken);
                }
                else if (dictionaryParameters.ContainsKey("access_token"))
                {
                    bearerToken = dictionaryParameters["access_token"].ToString();
                    dictionaryParameters.Remove(bearerToken);
                }
            }

            if (Authentication != null)
            {
                if (string.IsNullOrEmpty(bearerToken) && Authentication is GithubOAuthAuthenticator)
                {
                    var oauth2 = (GithubOAuthAuthenticator)Authentication;
                    bearerToken = oauth2.AccessToken;
                }

                if (string.IsNullOrEmpty(bearerToken))
                {
                    if (Authentication is GithubBasicAuthenticator)
                    {
                        var basicAuth = (GithubBasicAuthenticator)Authentication;
                        request.AuthenticateUsing(new HttpBasicAuthenticator(basicAuth.Username, basicAuth.Password));
                    }
                    else
                    {
                        throw new NotSupportedException("Authentication not supported.");
                    }
                }
                else
                {
                    request.AuthenticateUsing(new OAuth2AuthorizationRequestHeaderBearerAuthenticator(bearerToken));
                }
            }

            if (method.Equals("GET", StringComparison.OrdinalIgnoreCase))
            {
                // for GET, all parameters goes as querystring
                request.QueryStrings(qs => qs.Add(dictionaryParameters));
            }
            else
            {
                return request
                    .Headers(h => h.Add("Content-Type", "application/json"))
                    .Body(b =>
                              {
                                  if (parameters != null)
                                      b.Append(JsonSerializer.Current.SerializeObject(parameters));
                              });
            }

            return request;
        }