Пример #1
0
        public override async Task <(string Dump, Exception Exception)> ExecuteJobAsync(RuleJobData job)
        {
            var requestBody = job["RequestBody"].ToString(Formatting.Indented);
            var requestMsg  = BuildRequest(job, requestBody);

            HttpResponseMessage response = null;

            try
            {
                response = await HttpClientPool.GetHttpClient().SendAsync(requestMsg);

                var responseString = await response.Content.ReadAsStringAsync();

                var requestDump = DumpFormatter.BuildDump(requestMsg, response, requestBody, responseString, TimeSpan.Zero, false);

                return(requestDump, null);
            }
            catch (Exception ex)
            {
                if (requestMsg != null)
                {
                    var requestDump = DumpFormatter.BuildDump(requestMsg, response, requestBody, ex.ToString(), TimeSpan.Zero, false);

                    return(requestDump, ex);
                }
                else
                {
                    var requestDump = ex.ToString();

                    return(requestDump, ex);
                }
            }
        }
Пример #2
0
        protected override async Task <(string Dump, Exception Exception)> ExecuteJobAsync(WebhookJob job)
        {
            var requestBody    = job.Body;
            var requestMessage = BuildRequest(job, requestBody);

            HttpResponseMessage response = null;

            try
            {
                response = await HttpClientPool.GetHttpClient().SendAsync(requestMessage);

                var responseString = await response.Content.ReadAsStringAsync();

                var requestDump = DumpFormatter.BuildDump(requestMessage, response, requestBody, responseString, TimeSpan.Zero, false);

                Exception ex = null;

                if (!response.IsSuccessStatusCode)
                {
                    ex = new HttpRequestException($"Response code does not indicate success: {(int)response.StatusCode} ({response.StatusCode}).");
                }

                return(requestDump, ex);
            }
            catch (Exception ex)
            {
                var requestDump = DumpFormatter.BuildDump(requestMessage, response, requestBody, ex.ToString(), TimeSpan.Zero, false);

                return(requestDump, ex);
            }
        }
Пример #3
0
        public void MultiThreadProxy()
        {
            IHttpClientPool pool       = new HttpClientPool();
            var             spider     = new DefaultSpider();
            var             downloader = new HttpClientDownloader();

            ConcurrentDictionary <HttpClientEntry, int> tonggi = new ConcurrentDictionary <HttpClientEntry, int>();

            Parallel.For(0, 1000, new ParallelOptions {
                MaxDegreeOfParallelism = 1
            }, (i) =>
            {
                var port  = i % 10;
                var proxy = new UseSpecifiedUriWebProxy(new Uri($"http://192.168.10.1:{port}"));
                var item  = pool.GetHttpClient(proxy.Hash);

                if (tonggi.ContainsKey(item))
                {
                    tonggi[item] = tonggi[item] + 1;
                }
                else
                {
                    tonggi.TryAdd(item, 1);
                }
            });

            Assert.Equal(10, tonggi.Count);
            foreach (var pair in tonggi)
            {
                Assert.Equal(100, pair.Value);
            }
        }
Пример #4
0
        protected override async Task <(string Dump, Exception Exception)> ExecuteJobAsync(FastlyJob job)
        {
            if (string.IsNullOrWhiteSpace(job.Key))
            {
                return(null, new InvalidOperationException("The action cannot handle this event."));
            }

            var requestMsg = BuildRequest(job);

            HttpResponseMessage response = null;

            try
            {
                response = await HttpClientPool.GetHttpClient().SendAsync(requestMsg);

                var responseString = await response.Content.ReadAsStringAsync();

                var requestDump = DumpFormatter.BuildDump(requestMsg, response, null, responseString, TimeSpan.Zero, false);

                return(requestDump, null);
            }
            catch (Exception ex)
            {
                var requestDump = DumpFormatter.BuildDump(requestMsg, response, null, ex.ToString(), TimeSpan.Zero, false);

                return(requestDump, ex);
            }
        }
Пример #5
0
        public static async Task <UserProfileInfo> GetUserProfileInfo(string userId, string userAccessToken)
        {
            using (var response = await HttpClientPool.GetHttpClient()
                                  .GetAsync(FacebookApi.GetBasicUserInfoUrl(userId, userAccessToken)))
            {
                if (response.IsSuccessStatusCode)
                {
                    var responseJson = await response.Content.ReadAsStringAsync();

                    var userProfileInfo = JsonConvert.DeserializeObject <UserProfileInfo>(responseJson);
                    return(userProfileInfo);
                }

                throw new Exception("Error fetching user profile info.");
            }
        }
Пример #6
0
        public static async Task <string> ExchangeTokenForLongLivedToken(string tokenToExchange)
        {
            using (var response = await HttpClientPool.GetHttpClient()
                                  .GetAsync(FacebookApi.GetExchangeTokenUrl(tokenToExchange)))
            {
                if (response.IsSuccessStatusCode)
                {
                    var responseJson = await response.Content.ReadAsStringAsync();

                    var exchangeTokenData = JsonConvert.DeserializeObject <ExchangeTokenData>(responseJson);
                    return(exchangeTokenData.access_token);
                }

                throw new Exception("Could not exchange token for a long lived one.");
            }
        }
Пример #7
0
        public static async Task <DebugTokenData> GetFacebookAccessTokenDebugInfo(string inputToken)
        {
            using (var response =
                       await HttpClientPool.GetHttpClient().GetAsync(FacebookApi.GetDebugTokenUrl(inputToken)))
            {
                if (response.IsSuccessStatusCode)
                {
                    var responseJson = await response.Content.ReadAsStringAsync();

                    var debugTokenData = JsonConvert.DeserializeObject <DebugTokenDataRoot>(responseJson).data;
                    return(debugTokenData);
                }

                throw new Exception("Could not verify login with facebook. Please try again in some time.");
            }
        }
Пример #8
0
        public override async Task <(string Dump, Exception Exception)> ExecuteJobAsync(RuleJobData job)
        {
            var requestBody = job["RequestBody"].ToString(Formatting.Indented);
            var requestMsg  = BuildRequest(job, requestBody);

            HttpResponseMessage response = null;

            try
            {
                response = await HttpClientPool.GetHttpClient().SendAsync(requestMsg);

                var responseString = await response.Content.ReadAsStringAsync();

                var requestDump = DumpFormatter.BuildDump(requestMsg, response, requestBody, responseString, TimeSpan.Zero, false);

                Exception ex = null;

                if (!response.IsSuccessStatusCode)
                {
                    ex = new HttpRequestException($"Response code does not indicate success: {(int)response.StatusCode} ({response.StatusCode}).");
                }

                return(requestDump, ex);
            }
            catch (Exception ex)
            {
                if (requestMsg != null)
                {
                    var requestDump = DumpFormatter.BuildDump(requestMsg, response, requestBody, ex.ToString(), TimeSpan.Zero, false);

                    return(requestDump, ex);
                }
                else
                {
                    throw;
                }
            }
        }
Пример #9
0
        public override async Task <(string Dump, Exception Exception)> ExecuteJobAsync(RuleJobData job)
        {
            if (!job.TryGetValue("Key", out var keyToken))
            {
                return(null, new InvalidOperationException("The action cannot handle this event."));
            }

            var requestMsg = BuildRequest(job, keyToken.Value <string>());

            HttpResponseMessage response = null;

            try
            {
                response = await HttpClientPool.GetHttpClient().SendAsync(requestMsg);

                var responseString = await response.Content.ReadAsStringAsync();

                var requestDump = DumpFormatter.BuildDump(requestMsg, response, null, responseString, TimeSpan.Zero, false);

                return(requestDump, null);
            }
            catch (Exception ex)
            {
                if (requestMsg != null)
                {
                    var requestDump = DumpFormatter.BuildDump(requestMsg, response, null, ex.ToString(), TimeSpan.Zero, false);

                    return(requestDump, ex);
                }
                else
                {
                    var requestDump = ex.ToString();

                    return(requestDump, ex);
                }
            }
        }
Пример #10
0
        protected override async Task <(string Dump, Exception Exception)> ExecuteJobAsync(SlackJob job)
        {
            var requestBody    = job.Body;
            var requestMessage = BuildRequest(job, requestBody);

            HttpResponseMessage response = null;

            try
            {
                response = await HttpClientPool.GetHttpClient().SendAsync(requestMessage);

                var responseString = await response.Content.ReadAsStringAsync();

                var requestDump = DumpFormatter.BuildDump(requestMessage, response, requestBody, responseString, TimeSpan.Zero, false);

                return(requestDump, null);
            }
            catch (Exception ex)
            {
                var requestDump = DumpFormatter.BuildDump(requestMessage, response, requestBody, ex.ToString(), TimeSpan.Zero, false);

                return(requestDump, ex);
            }
        }