예제 #1
0
        /// <summary>
        /// request url that specified by yourself with get method.
        /// according the package Flurl.Http.GeneratedExtensions to rewrite it in order to debug, develop.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <param name="cancellationToken"></param>
        /// <param name="completionOption"></param>
        /// <returns></returns>
        public static Task <T> GetJsonAsync <T>(this Flurl.Url url, CancellationToken cancellationToken = default(CancellationToken), HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead)
        {
            var request = new FlurlRequest(url);
            var result  = request.GetJsonAsync <T>(cancellationToken, completionOption);

            return(result);
        }
예제 #2
0
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var flurlRequest  = new FlurlRequest(request.RequestUri.ToString());
            var stringContent = (request.Content as StringContent);

            if (stringContent != null)
            {
                request.Content =
                    new CapturedStringContent(
                        await stringContent.ReadAsStringAsync(),
                        GetEncodingFromCharSet(stringContent.Headers?.ContentType?.CharSet),
                        stringContent.Headers?.ContentType?.MediaType
                        );
            }

            if (request?.Properties != null)
            {
                request.Properties["FlurlHttpCall"] = new HttpCall()
                {
                    FlurlRequest = flurlRequest,
                    Request      = request
                }
            }
            ;

            return(await base.SendAsync(request, cancellationToken));
        }
        private static async Task <byte[]> GetFileAsync(Uri url)
        {
            var request  = new FlurlRequest(url);
            var response = await request.SendAsync(HttpMethod.Get);

            return(await response.Content.ReadAsByteArrayAsync());
        }
예제 #4
0
        private TResult Request <TResult>(string url, Func <IFlurlRequest, Task <TResult> > func)
        {
            var result = default(TResult);

            try
            {
                var req = new FlurlRequest(_config.BaseUrl + url)
                          .ConfigureRequest(x =>
                {
                    var jsonSettings = new JsonSerializerSettings
                    {
                        NullValueHandling     = NullValueHandling.Ignore,
                        DefaultValueHandling  = DefaultValueHandling.Include,
                        MissingMemberHandling = MissingMemberHandling.Ignore
                    };
                    x.JsonSerializer = new NewtonsoftJsonSerializer(jsonSettings);
                }).
                          SetQueryParam("merchant_token", _config.Token);

                result = func.Invoke(req).Result;
            }
            catch (FlurlHttpException ex)
            {
                throw;
            }

            return(result);
        }
예제 #5
0
        internal IFlurlRequest GetClient(Url pathSegment = null, bool anonymous = false, bool useBaseUrl = true)
        {
            var url = useBaseUrl ? BaseUrl
                      .AppendPathSegment(API_SEGMENT)
                      .AppendPathSegment(pathSegment.Path)
                      .SetQueryParams(pathSegment.QueryParams) : pathSegment;
            IFlurlRequest client = new FlurlRequest(url);

            foreach (var hea in Headers)
            {
                client = client.WithHeader(hea.Key, hea.Value);
            }
            //var client = url
            //    //.WithHeader("Content-Type", "application/json")
            //    .WithHeader("Accept", "application/json")
            //    //.WithHeader("User-Agent", Platform + "|" + AppName + "|" + GetType().GetTypeInfo().Assembly.GetName().Version)
            //    .WithHeader("Accept-Language", System.Globalization.CultureInfo.CurrentCulture.ToString());

            client = client.WithHeader("ClientVersion", "1.0.0");
            foreach (var inter in callInterceptors)
            {
                client.ConfigureRequest(httpClient =>
                {
                    inter(pathSegment, anonymous, useBaseUrl, httpClient);
                });
            }
            //if (!anonymous)
            //{
            //    var authHeader = GetAuthorizationHeader();
            //    client = client.WithHeader(authHeader.Key, authHeader.Value);
            //}
            return(client);
        }
예제 #6
0
        public override void OnEpochEnd(int epoch, Dictionary <string, float> logs = null)
        {
            logs = logs != null ? logs : new Dictionary <string, float>();
            var send = new Dictionary <string, float>();

            send["epoch"] = epoch;
            foreach (var log in logs)
            {
                var k = log.Key;
                var v = log.Value;
                send[k] = v;
            }

            try
            {
                var req = new FlurlRequest(new Flurl.Url(this.root + this.path));
                foreach (var item in headers)
                {
                    req.Headers.Add(item.Key, item.Value);
                }

                if (this.send_as_json)
                {
                    req.PostJsonAsync(send).Wait();
                }
                else
                {
                    throw new NotSupportedException("Only json format supported");
                }
            }
            catch
            {
                Logger.Warning("Warning: could not reach RemoteMonitor root server at " + this.root.ToString());
            }
        }
        private string MakePostRequest(string url, IDictionary <string, string> inputFields)
        {
            try
            {
                string requestContents = string.Empty;
                if (inputFields != null)
                {
                    requestContents = string.Join("&", (
                                                      from i in inputFields
                                                      select string.Format("{0}={1}", i.Key, HttpUtility.UrlEncode(i.Value))).ToArray <string>());
                }
                var request = new FlurlRequest(url)
                              .SetQueryParams(inputFields, Flurl.NullValueHandling.Remove);
                var response = request
                               .PostAsync(null)
                               .ReceiveString()
                               .Result;

                return(response);
            }
            catch (FlurlHttpException ex)
            {
                return(string.Empty);
            }
        }
예제 #8
0
        public async IAsyncEnumerable <R4UCard> Process(IAsyncEnumerable <R4UCard> originalCards)
        {
            await foreach (var card in originalCards)
            {
                yield return(card);

                Log.Information("Getting possible foils for [{serial}]", card.Serial);

                var urlRequest = new FlurlRequest(foilSearchURL).SetQueryParam("keyword", card.Serial);
                Log.Debug("URL: {url}", urlRequest.Url);
                var doc = await urlRequest.GetHTMLAsync();

                var cardList = doc.QuerySelectorAll(".cardlist-item").Select(i => i as IHtmlAnchorElement).Skip(1);
                foreach (var cardLink in cardList)
                {
                    Log.Information("Found URL: {url}", cardLink.Href);
                    var cardLinkDoc = await cardLink.Href.WithReferrer(urlRequest.Url.Path).GetHTMLAsync();

                    var newCard = card.Clone();
                    newCard.NonFoil = card;
                    newCard.Serial  = cardLinkDoc.QuerySelector(".cardlist-number").TextContent;
                    newCard.Rarity  = cardLinkDoc.QuerySelectorAll(".cardlist-text") //
                                      .Where(i => i.Children.ElementAt(2)?.TextContent == "レアリティ")
                                      .Select(i => i.Children.ElementAt(3).TextContent.Trim())
                                      .First();
                    yield return(newCard);
                }
            }
        }
예제 #9
0
        private async Task <TResult> Request <TResult>(string baseUrl, string path, Func <IFlurlRequest, Task <TResult> > func)
        {
            var result = default(TResult);


            try
            {
                var request = new FlurlRequest(baseUrl + path)
                              .ConfigureRequest(x =>
                {
                    var jsonSettings = new JsonSerializerSettings
                    {
                        NullValueHandling     = NullValueHandling.Ignore,
                        DefaultValueHandling  = DefaultValueHandling.Include,
                        MissingMemberHandling = MissingMemberHandling.Ignore
                    };
                    x.JsonSerializer = new NewtonsoftJsonSerializer(jsonSettings);
                });

                return(await func.Invoke(request));
            }
            catch (FlurlHttpException ex)
            {
                throw;
            }

            return(await Task.FromResult(result));
        }
예제 #10
0
        [Test, NonParallelizable]         // #239
        public void request_default_settings_change_when_client_changes()
        {
            FlurlHttp.ConfigureClient("http://test.com", cli => cli.Settings.CookiesEnabled = true);
            var req  = new FlurlRequest("http://test.com");
            var cli1 = req.Client;

            Assert.IsTrue(req.Settings.CookiesEnabled, "pre-configured client should provide defaults to new request");

            req.Url = "http://test.com/foo";
            Assert.AreSame(cli1, req.Client, "new URL with same host should hold onto same client");
            Assert.IsTrue(req.Settings.CookiesEnabled);

            req.Url = "http://test2.com";
            Assert.AreNotSame(cli1, req.Client, "new host should trigger new client");
            Assert.IsFalse(req.Settings.CookiesEnabled);

            FlurlHttp.ConfigureClient("http://test2.com", cli => cli.Settings.CookiesEnabled = true);
            Assert.IsTrue(req.Settings.CookiesEnabled, "changing client settings should be reflected in request");

            req.Settings = new FlurlHttpSettings();
            Assert.IsTrue(req.Settings.CookiesEnabled, "entirely new settings object should still inherit current client settings");

            req.Client = new FlurlClient();
            Assert.IsFalse(req.Settings.CookiesEnabled, "entirely new client should provide new defaults");

            req.Url = "http://test.com";
            Assert.AreNotSame(cli1, req.Client, "client was explicitly set on request, so it shouldn't change even if the URL changes");
            Assert.IsFalse(req.Settings.CookiesEnabled);
        }
예제 #11
0
        private async Task <List <RippleTransaction> > GetRippleTransactions(string address)
        {
            var    txs          = new List <RippleTransaction>();
            object pagingMarker = null;

            do
            {
                var request = new FlurlRequest(_settings.RpcUrl);

                request.Settings.JsonSerializer = new NewtonsoftJsonSerializer(new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore
                });

                if (!string.IsNullOrEmpty(_settings.RpcUsername))
                {
                    request = request.WithBasicAuth(_settings.RpcUsername, _settings.RpcPassword);
                }

                var response = await request
                               .PostJsonAsync(new RippleAccountTransactionsRequest(address, marker : pagingMarker))
                               .ReceiveJson <RippleAccountTransactionsResponse>();

                if (!string.IsNullOrEmpty(response.Result.Error))
                {
                    throw new InvalidOperationException($"XRP request error: {response.Result.ErrorMessage ?? response.Result.Error}");
                }

                txs.AddRange(response.Result.Transactions);

                pagingMarker = response.Result.Marker;
            } while (pagingMarker != null);

            return(txs);
        }
예제 #12
0
        //private PayPalWebhookEvent ParseAndValidatePayPalWebhookEvent(PayPalWebhookRequestConfig requestConfig, HttpRequestBase request)
        //{
        //    var payPalWebhookEvent = default(PayPalWebhookEvent);

        //    if (request.InputStream.CanSeek)
        //        request.InputStream.Seek(0, SeekOrigin.Begin);

        //    using (var reader = new StreamReader(request.InputStream))
        //    {
        //        var json = reader.ReadToEnd();

        //        var tmpPayPalWebhookEvent = JsonConvert.DeserializeObject<PayPalWebhookEvent>(json);

        //        var result = MakePayPalRequest("/v1/notifications/verify-webhook-signature", (req) => req
        //            .PostJsonAsync(new PayPalVerifyWebhookSignatureRequest
        //            {
        //                AuthAlgorithm = request.Headers["paypal-auth-algo"],
        //                CertUrl = request.Headers["paypal-cert-url"],
        //                TransmissionId = request.Headers["paypal-transmission-id"],
        //                TransmissionSignature = request.Headers["paypal-transmission-sig"],
        //                TransmissionTime = request.Headers["paypal-transmission-time"],
        //                WebhookId = requestConfig.WebhookId,
        //                WebhookEvent = tmpPayPalWebhookEvent
        //            })
        //            .ReceiveJson<PayPalVerifyWebhookSignatureResult>(),
        //            requestConfig);

        //        if (result != null && result.VerificationStatus == "SUCCESS")
        //        {
        //            payPalWebhookEvent =  tmpPayPalWebhookEvent;
        //        }
        //    }

        //    return payPalWebhookEvent;
        //}

        private TResult Request <TResult>(string url, Func <IFlurlRequest, Task <TResult> > func)
        {
            var result = default(TResult);

            try
            {
                var req = new FlurlRequest(_config.BaseUrl + url)
                          .WithOAuthBearerToken(GetAccessToken());

                result = func.Invoke(req).Result;
            }
            catch (FlurlHttpException ex)
            {
                if (ex.Call.HttpStatus == HttpStatusCode.Unauthorized)
                {
                    var req = new FlurlRequest(_config.BaseUrl + url)
                              .WithOAuthBearerToken(GetAccessToken(true));

                    result = func.Invoke(req).Result;
                }
                else
                {
                    throw;
                }
            }

            return(result);
        }
        private static async Task <HttpResponseMessage> PutFileAsync(Uri url, byte[] file)
        {
            var request  = new FlurlRequest(url);
            var response = await request.SendAsync(HttpMethod.Put, new ByteArrayContent(file));

            return(response);
        }
예제 #14
0
        protected IFlurlRequest GetApiFlurlRequest()
        {
            var request = new FlurlRequest(this.settings.ApiBaseUri);

            request.WithOAuthBearerToken(Program.UserSettings.AccesToken);

            return(request);
        }
예제 #15
0
        public void request_gets_global_settings_when_no_client()
        {
            var req = new FlurlRequest();

            Assert.IsNull(req.Client);
            Assert.IsNull(req.Url);
            Assert.AreEqual(FlurlHttp.GlobalSettings.JsonSerializer, req.Settings.JsonSerializer);
        }
예제 #16
0
        public async Task can_set_cookies_before_setting_url()
        {
            var req = new FlurlRequest().WithCookie("z", "999");

            req.Url = "https://httpbin.org/cookies";
            var resp = await req.GetJsonAsync();

            Assert.AreEqual("999", resp.cookies.z);
        }
예제 #17
0
        /// <summary>
        /// request url that specified by yourself with post method.
        /// according the package Flurl.Http.GeneratedExtensions to rewrite it in order to debug, develop.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <param name="cancellationToken"></param>
        /// <param name="completionOption"></param>
        /// <returns></returns>
        public static Task <T> PostJsonAsync <T>(this Flurl.Url url, object data, CancellationToken cancellationToken = default(CancellationToken), HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead)
        {
            var request = new FlurlRequest(url);
            CapturedJsonContent content = new CapturedJsonContent(request.Settings.JsonSerializer.Serialize(data));
            var response = request.SendAsync(HttpMethod.Post, content, cancellationToken, completionOption);
            var result   = response.ReceiveJson <T>();

            return(result);
        }
예제 #18
0
        public async Task <IEnumerable <Transaction> > GetTransactions(string address)
        {
            var url = _nodeAddress.AppendPathSegments("api", "transactions", address);

            var request = new FlurlRequest(url);

            var response = await request.SendAsync(HttpMethod.Get);

            return(await Task.FromResult(response).ReceiveJson <IEnumerable <Transaction> >());
        }
예제 #19
0
        public async Task <Block> GetBlock(int id)
        {
            var url     = _nodeAddress.AppendPathSegments("api", "block", id.ToString());
            var request = new FlurlRequest(url);

            var response = await request.SendAsync(HttpMethod.Get);

            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                return(null);
            }

            return(await Task.FromResult(response).ReceiveJson <Block>());
        }
예제 #20
0
        private IFlurlRequest BuildBaseRequest(string url, object?query, object?cookies)
        {
            IFlurlRequest req = new FlurlRequest(url);

            if (query != null)
            {
                req = req.SetQueryParams(query);
            }
            if (cookies != null)
            {
                req = req.WithCookies(cookies);
            }

            return(req);
        }
예제 #21
0
        public async Task <Transaction> GetTransaction(string hash)
        {
            var url = _nodeAddress.AppendPathSegments("api", "transactions");

            url.QueryParams.Add("transactionHash", hash);

            var request = new FlurlRequest(url);

            var response = await request.SendAsync(HttpMethod.Get);

            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                return(null);
            }

            return(await Task.FromResult(response).ReceiveJson <Transaction>());
        }
예제 #22
0
        public static async Task <byte[]> GetDataAsync(this string url)
        {
            if (string.IsNullOrWhiteSpace(url))
            {
                return(new byte[] { });
            }
            var request = new FlurlRequest(url);

            using (var response = await request.SendAsync(HttpMethod.Get))
                using (var httpStream = await response.Content.ReadAsStreamAsync())
                    using (var memStream = new MemoryStream())
                    {
                        await httpStream.CopyToAsync(memStream);

                        return(memStream.ToArray());
                    }
        }
예제 #23
0
        public async Task DownloadImage(Credential creds, Guid sourceId, string imageId, string imageUrl, string token, string downloadPath, string[] tokenUrls)
        {
            if (!Directory.Exists(downloadPath))
            {
                Directory.CreateDirectory(downloadPath);
            }

            IFlurlRequest url = new FlurlRequest(imageUrl);

            if (tokenUrls.Any(turl => new Uri(imageUrl).Host == turl))
            {
                url = imageUrl.WithOAuthBearerToken(token);
            }

            var response = await url.GetBytesAsync();

            File.WriteAllBytes(Path.Combine(downloadPath, imageId.ToString()), response);
        }
예제 #24
0
        public static async Task <Client> GetClientFromCookieAsync(string cookie)
        {
            var request = new FlurlRequest();

            request.EnableCookies();

            var loginHint = await GetGoogleLoginHintAsync(request, cookie.Split(new[] { "; " }, StringSplitOptions.RemoveEmptyEntries).Select(c => c.Split('=')).ToDictionary(k => k[0], v => string.Join("=", v.Skip(1))));

            var googleLoginToken = await GetGoogleLoginTokenAsync(request, loginHint.Sessions[0].Login_Hint);

            var eeuTokenResponse = await "https://auth.ee-universe.com/auth/token".PostJsonAsync(new
            {
                method = "google",
                token  = googleLoginToken.Id_Token
            }).ReceiveJson <LoginResponse>();

            return(new ActualClient(eeuTokenResponse.Token));
        }
예제 #25
0
        [Test]         // #246
        public void test_settings_dont_override_request_settings_when_not_set_explicitily()
        {
            var ser1 = new FakeSerializer();
            var ser2 = new FakeSerializer();

            using (var test = new HttpTest()) {
                var cli = new FlurlClient();
                cli.Settings.JsonSerializer = ser1;
                Assert.AreSame(ser1, cli.Settings.JsonSerializer);

                var req = new FlurlRequest {
                    Client = cli
                };
                Assert.AreSame(ser1, req.Settings.JsonSerializer);

                req.Settings.JsonSerializer = ser2;
                Assert.AreSame(ser2, req.Settings.JsonSerializer);
            }
        }
예제 #26
0
        private IFlurlRequest GetRequest(string url)
        {
            var requestUrl = new FlurlRequest($"{_config.ApiUrl}/{url}").WithClient(_flurlClient);

            if (!string.IsNullOrEmpty(_token))
            {
                requestUrl.WithOAuthBearerToken(_token);
            }
            else
            {
                if (!string.IsNullOrEmpty(_apiKey))
                {
                    requestUrl.SetQueryParam("key", _apiKey);
                }
                else
                {
                    throw new Exception("No token and no key for ipb client");
                }
            }

            return(requestUrl);
        }
        private IFlurlRequest CreateRequest()
        {
            var flurlRequest = new FlurlRequest(url)
            {
                Client = flurlClientFactory.Get(url)
            };

            flurlRequest.WithHeader(CorrelationContext.CorrelationIdName, CorrelationContext.CorrelationId.Value);

            if (headers != null)
            {
                foreach (var header in headers)
                {
                    flurlRequest.WithHeader(header.Key, header.Value);
                }
            }

            flurlRequest.Settings = defaultFlurlHttpSettings.Create();

            flurlRequest.WithTimeout(timeout);

            return(flurlRequest);
        }
        public async Task <IRwsResponse> SendRequestAsync(IRwsRequest request, int?timeout = null)
        {
            var client = new FlurlRequest(Url.Combine(BaseUrl, request.UrlPath()));

            if (timeout != null)
            {
                client.WithTimeout((int)timeout);
            }

            if (request.RequiresAuthentication)
            {
                client.WithBasicAuth(_username, _password);
            }

            client.WithHeaders(request.Headers);

            var stopwatch = Stopwatch.StartNew();

            HttpResponseMessage response = null;

            try {
                response = await client.AllowAnyHttpStatus().SendAsync(request.Method, request.RequestBody);
            } catch (FlurlHttpTimeoutException ex)
            {
                throw new RwsException($"Connection timeout for {client.Url.ToString()}", ex);
            }

            stopwatch.Stop();

            LastResult  = response;
            RequestTime = stopwatch.Elapsed;

            RwsExceptionHandler.Parse(response);

            return(request.Result(response));
        }
예제 #29
0
 public void GivenPath(string path)
 {
     CurrentRequest = new FlurlRequest($"http://localhost:5000/{path}");
 }
예제 #30
0
        private async Task <T> ProcessGetAsync <T>(string url, Dictionary <string, string> headers = null, object requestParams = null, string accessToken = "", int timeout = 10, bool isThrowException = true)
        {
            var result = new RestApiResponse <T>();

            try
            {
                var _request      = new Url(url);
                var _flurlRequest = new FlurlRequest(_request);

                if (!string.IsNullOrEmpty(accessToken))
                {
                    _flurlRequest = _flurlRequest.WithHeader("Authorization", accessToken);
                }

                if (headers != null && headers.Count > 0)
                {
                    foreach (var item in headers)
                    {
                        _flurlRequest = _flurlRequest.WithHeader(item.Key, item.Value);
                    }
                }

                var response = requestParams != null ? await _flurlRequest.SetQueryParams(requestParams).WithTimeout(timeout).GetAsync() : await _flurlRequest.WithTimeout(timeout).GetAsync();

                if (response.IsSuccessStatusCode)
                {
                    var stringResponse = await response.Content.ReadAsStringAsync();

                    result.Result    = JsonConvert.DeserializeObject <T>(stringResponse);
                    result.IsSuccess = true;
                }
            }
            catch (FlurlHttpTimeoutException timeoutex)
            {
                result.IsSuccess        = false;
                result.ExceptionMessage = "Time out";
                result.Exception        = timeoutex;
            }
            catch (FlurlHttpException ex)
            {
                var errorResponse = await ex.GetResponseStringAsync();

                result.Exception        = ex;
                result.ExceptionMessage = ex.Message;
                result.IsSuccess        = false;
                //Try parse response to Custom Exception
                try
                {
                    var customException = JsonConvert.DeserializeObject <CustomExceptionResponse>(errorResponse);
                    if (customException != null)
                    {
                        result.IsCustomException = true;
                        result.CustomException   = customException;
                        result.IsSuccess         = false;
                    }
                }
                catch
                {
                    // KhuongDang : No content
                }
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Exception = ex;
            }

            WriteLog(url, result, requestParams, isThrowException);

            return(result.Result);
        }