예제 #1
0
        /// <summary>
        /// Get the list movie now showing
        /// </summary>
        /// <returns></returns>
        public async Task <List <Movie> > GetNowShowingMovies()
        {
            var result = new List <Movie>();

            var reqResult = await _httpHandler.GetAsync(URL_MOVIES);

            string content = await reqResult.Content.ReadAsStringAsync();

            JObject responseObj = JObject.Parse(content);

            List <JToken> entries = responseObj["movieShowing"] != null ? responseObj["movieShowing"].ToList() : new List <JToken>();

            foreach (JToken entry in entries)
            {
                Movie tmpMovie = new Movie();
                tmpMovie.MovieId        = entry["id"] != null ? entry["id"].ToString() : string.Empty;
                tmpMovie.EnglishName    = entry["name"] != null ? entry["name"].ToString() : string.Empty;
                tmpMovie.VietNameseName = entry["subName"] != null && !string.IsNullOrWhiteSpace(entry["subName"].ToString()) ? entry["subName"].ToString() : tmpMovie.EnglishName;
                tmpMovie.PosterUrl      = entry["imagePortrait"] != null?string.Format(URL_POSTER, entry["imagePortrait"].ToString()) : string.Empty;

                tmpMovie.ViewGradeNameEN = entry["age"] != null ? entry["age"].ToString() : string.Empty;
                tmpMovie.ViewGradeCode   = entry["age"] != null ? entry["age"].ToString() : tmpMovie.ViewGradeNameEN;
                tmpMovie.PlayTime        = entry["duration"] != null ? (int)entry["duration"] : 0;
                tmpMovie.Rating          = entry["point"] != null ? (double)entry["point"] : 0;
                tmpMovie.Description     = entry["description"] != null ? entry["description"].ToString() : string.Empty;

                result.Add(tmpMovie);
            }

            return(result);
        }
예제 #2
0
        /// <summary>
        /// Search GIPHY's library of millions of GIFs for a word or phrase.
        /// </summary>
        /// <param name="parameter">Specifies search parameters</param>
        /// <returns>Root object</returns>
        public async Task <RootObject> Search(SearchParameter parameter)
        {
            if (parameter is null)
            {
                throw new NullReferenceException("Paramter cannot be null");
            }

            if (string.IsNullOrEmpty(parameter.Query))
            {
                throw new FormatException("Query paramter cannot be null or empty.");
            }

            /* Finish exception checks */

            string url = $@"{ _searchUrl }?api_key={ _apiKey }&q={ parameter.Query }&limit={ parameter.Limit }&offset={ parameter.Offset }&lang={ parameter.Language }&rating={ parameter.Rating.ToString() }";

            var response = await _httpHandler.GetAsync(url);

            if (!response.IsSuccessStatusCode)
            {
                throw new HttpRequestException(response.ReasonPhrase);
            }

            return(await response.Content.ReadAsAsync <RootObject>());
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="categoryName"></param>
        /// <param name="itemName"></param>
        /// <returns></returns>
        public async Task <Guid> GetReferenceDataGuId(string categoryName, string itemName)
        {
            using (loggingHelper.RMTraceManager.StartTrace("Integration.GetReferenceDataGuId"))
            {
                string methodName = MethodHelper.GetActualAsyncMethodName();
                loggingHelper.Log(methodName + LoggerTraceConstants.COLON + LoggerTraceConstants.MethodExecutionStarted, TraceEventType.Verbose, null, LoggerTraceConstants.Category, LoggerTraceConstants.PostalAddressAPIPriority, LoggerTraceConstants.PostalAddressIntegrationServiceMethodEntryEventId, LoggerTraceConstants.Title);

                Guid referenceId           = Guid.Empty;
                HttpResponseMessage result = await httpHandler.GetAsync(referenceDataWebAPIName + "simpleLists?listName=" + categoryName);

                if (!result.IsSuccessStatusCode)
                {
                    var responseContent = result.ReasonPhrase;
                    throw new ServiceException(responseContent);
                }

                Tuple <string, SimpleListDTO> apiResult = JsonConvert.DeserializeObject <Tuple <string, SimpleListDTO> >(result.Content.ReadAsStringAsync().Result);
                if (apiResult != null && apiResult.Item2 != null && apiResult.Item2.ListItems != null && apiResult.Item2.ListItems.Count > 0)
                {
                    referenceId = apiResult.Item2.ListItems.Where(n => n.Value.Equals(itemName, StringComparison.OrdinalIgnoreCase)).SingleOrDefault().Id;
                }

                loggingHelper.Log(methodName + LoggerTraceConstants.COLON + LoggerTraceConstants.MethodExecutionCompleted, TraceEventType.Verbose, null, LoggerTraceConstants.Category, LoggerTraceConstants.PostalAddressAPIPriority, LoggerTraceConstants.PostalAddressIntegrationServiceMethodExitEventId, LoggerTraceConstants.Title);

                return(referenceId);
            }
        }
예제 #4
0
        public async Task <KeyedCollection <string, PwnedPassword> > GetPasswordResultsAsync(string shaHash)
        {
            if (shaHash is null)
            {
                throw new ArgumentNullException(nameof(shaHash));
            }

            if (shaHash.Length < 5)
            {
                throw new ArgumentException("shaHash prefix must be at least five characters");
            }

            string prefix   = shaHash.Substring(0, 5);
            var    response = await _httpHandler.GetAsync($"range/{prefix}");

            string results = await response.Content.ReadAsStringAsync();

            if (!response.IsSuccessStatusCode)
            {
                throw new ApplicationException($"Error response received from server: {results}");
            }

            var resultsArray = results.Replace("\n", "").Split('\r');

            var pwnedPasswords = new PwnedPasswordList();

            pwnedPasswords.AddRange(resultsArray.Select(result => new PwnedPassword(prefix, result)));
            return(pwnedPasswords);
        }
예제 #5
0
        internal async Task <FundaResponse> GetFundaResponse(string city, string amenity, int page = 1, int pageSize = 25)
        {
            string urlCity    = string.IsNullOrWhiteSpace(city) ? string.Empty : "/" + city;
            string urlAmenity = string.IsNullOrWhiteSpace(amenity) ? string.Empty : "/" + amenity;

            return(await Policy.Handle <HttpRequestException>().WaitAndRetryAsync(100, retryAttempt =>
            {
                Console.WriteLine($"retry : {retryAttempt}");
                return TimeSpan.FromSeconds(retryAttempt);
            }).ExecuteAsync(async() =>
            {
                using (var response = await httpHandler.GetAsync(configuration.GetValue <string>("fundaUrl")
                                                                 .Replace("{urlCity}", urlCity)
                                                                 .Replace("{urlAmenity}", urlAmenity)
                                                                 .Replace("{page}", page.ToString())
                                                                 .Replace("{pageSize}", pageSize.ToString())))
                {
                    if (!response.IsSuccessStatusCode)
                    {
                        throw new HttpRequestException();
                    }
                    string apiResponse = await response.Content.ReadAsStringAsync();
                    return JsonConvert.DeserializeObject <FundaResponse>(apiResponse);
                }
            }));
        }
예제 #6
0
        /// <summary> Gets the name of the reference data categories by category. </summary> <param
        /// name="categoryNames">The category names.</param> <returns>List of <see cref="ReferenceDataCategoryDTO"></returns>
        public async Task <List <ReferenceDataCategoryDTO> > GetReferenceDataNameValuePairs(List <string> categoryNames)
        {
            using (loggingHelper.RMTraceManager.StartTrace("Integration.GetReferenceDataNameValuePairs"))
            {
                string methodName = typeof(NetworkManagerIntegrationService) + "." + nameof(GetReferenceDataNameValuePairs);
                loggingHelper.LogMethodEntry(methodName, priority, entryEventId);

                List <ReferenceDataCategoryDTO> listReferenceCategories = new List <ReferenceDataCategoryDTO>();
                List <NameValuePair>            nameValuePairs          = new List <NameValuePair>();
                foreach (var category in categoryNames)
                {
                    HttpResponseMessage result = await httpHandler.GetAsync(referenceDataWebAPIName + "nameValuePairs?appGroupName=" + category);

                    if (!result.IsSuccessStatusCode)
                    {
                        var responseContent = result.ReasonPhrase;
                        throw new ServiceException(responseContent);
                    }

                    Tuple <string, List <NameValuePair> > apiResult = JsonConvert.DeserializeObject <Tuple <string, List <NameValuePair> > >(result.Content.ReadAsStringAsync().Result);
                    nameValuePairs.AddRange(apiResult.Item2);
                }

                listReferenceCategories.AddRange(ReferenceDataHelper.MapDTO(nameValuePairs));
                loggingHelper.LogMethodExit(methodName, priority, exitEventId);
                return(listReferenceCategories);
            }
        }
예제 #7
0
        public async Task WhenFetchingFromTheApiService_CallsTheHttpClientWithGivenPath()
        {
            var api      = new ApiService(_fakeClient);
            var response = await api.FetchAsync("api/call");

            A.CallTo(() => _fakeClient.GetAsync("api/call")).MustHaveHappened();
        }
        /// <summary>
        /// Sets the query string param, http method(get), uri string, version, client id, client secret and access token.
        /// </summary>
        /// <returns>
        ///  Gets the respective result.
        /// </returns>
        public string GetRequestResolver(Dictionary <string, string> queryParams, string version, string clientId, string clientSecret, string uriPath, AccessToken accessToken, DateTime dateTime)
        {
            GetRefreshToken(version, clientId, clientSecret, accessToken, dateTime);
            var response = _httpHandler.GetAsync(queryParams, version, uriPath, accessToken.Type, accessToken.Token);
            var content  = response.Content.ReadAsStringAsync().Result;

            return(content);
        }
예제 #9
0
        public async Task <bool> Download(string curFileName)
        {
            var response = await client.GetAsync($"{baseUrl}{fileServiceName}/{curFileName}");

            if (!response.IsSuccessStatusCode)
            {
                return(false);
            }
            using (var stream = response.Content.ReadAsStreamAsync())
            {
                var fileInfo = new FileInfo(curFileName);
                using (var fileStream = fileInfo.Create())
                {
                    await stream.Result.CopyToAsync(fileStream);
                }
            }
            return(true);
        }
        /// <summary>
        /// Get the street DTO for operational object.
        /// </summary>
        /// <param name="networkLinkID">networkLink unique identifier Guid.</param>
        /// <returns>Nearest street and the intersection point.</returns>
        public async Task <NetworkLinkDTO> GetNetworkLink(Guid networkLinkID)
        {
            using (loggingHelper.RMTraceManager.StartTrace("Integration.GetNetworkLink"))
            {
                string methodName = typeof(AccessLinkIntegrationService) + "." + nameof(GetNearestNamedRoad);
                loggingHelper.LogMethodEntry(methodName, priority, entryEventId);

                HttpResponseMessage result = await httpHandler.GetAsync(networkManagerDataWebAPIName + "networklink/" + networkLinkID);

                if (!result.IsSuccessStatusCode)
                {
                    var responseContent = result.ReasonPhrase;
                    throw new ServiceException(responseContent);
                }

                NetworkLinkDTO networkLink = JsonConvert.DeserializeObject <NetworkLinkDTO>(result.Content.ReadAsStringAsync().Result);

                loggingHelper.LogMethodExit(methodName, priority, exitEventId);
                return(networkLink);
            }
        }
        public GoogleResponseDto SearchByExpression(string searchTerm)
        {
            var query = $"{_baseAddress}v1?key={_apiKey}&cx={_cx}&q={searchTerm}";

            var response = _handler.GetAsync(query).Result;

            var content = response.Content.ReadAsStringAsync().Result;

            var result = JsonConvert.DeserializeObject <GoogleResponseDto>(content);

            return(result);
        }
        public async void GetMovieAsync_should_return_movie()
        {
            // Arrange
            var json = "{'Title':'Star Wars: Episode IV - A New Hope','Year':'1977','Rated':'PG','Released':'25 May 1977','Runtime':'121 min','Genre':'Action, Adventure, Fantasy, Sci-Fi','Director':'George Lucas'," +
                       "'Writer':'George Lucas','Actors':'Mark Hamill, Harrison Ford, Carrie Fisher, Peter Cushing','Plot':'Luke Skywalker joins.','Language':'English','Country':'USA','Awards':'Won 6 Oscars. Another 52 wins & 29 nominations.'," +
                       "'Poster':'https://m.media-amazon.com/images/M/MV5BNzVlY2MwMjktM2E4OS00Y2Y3LWE3ZjctYzhkZGM3YzA1ZWM2XkEyXkFqcGdeQXVyNzkwMjQ5NzM@._V1_SX300.jpg'," +
                       "'Ratings':[{'Source':'Internet Movie Database','Value':'8.6/10'},{'Source':'Rotten Tomatoes','Value':'92%'},{'Source':'Metacritic','Value':'90/100'}]," +
                       "'Metascore':'90','imdbRating':'8.6','imdbVotes':'1,227,656','imdbID':'tt0076759','Type':'movie','DVD':'N/A','BoxOffice':'$460,998,507','Production':'Lucasfilm Ltd.','Website':'N/A','Response':'True'}";
            HttpContent content = new StringContent(json);

            A.CallTo(() => _httpHandler.GetAsync(A <string> .Ignored))
            .Returns(new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK, Content = content
            });

            // Act
            var result = await _sourceMovieService.GetMovieAsync("a");

            // Assert
            A.CallTo(() => _httpHandler.GetAsync(A <string> .Ignored)).MustHaveHappenedOnceExactly();
            Assert.NotNull(result);
            Assert.Equal("Star Wars: Episode IV - A New Hope", result.Title);
            Assert.Equal("1977", result.Year);
            Assert.Equal("PG", result.Rated);
            Assert.Equal("25 May 1977", result.Released);
            Assert.Equal("121 min", result.Runtime);
            Assert.Equal("Action, Adventure, Fantasy, Sci-Fi", result.Genre);
            Assert.Equal("George Lucas", result.Director);
            Assert.Equal("George Lucas", result.Writer);
            Assert.Equal("Mark Hamill, Harrison Ford, Carrie Fisher, Peter Cushing", result.Actors);
            Assert.Equal("Luke Skywalker joins.", result.Plot);
            Assert.Equal("English", result.Language);
            Assert.Equal("USA", result.Country);
            Assert.Equal("Won 6 Oscars. Another 52 wins & 29 nominations.", result.Awards);
            Assert.Equal("https://m.media-amazon.com/images/M/MV5BNzVlY2MwMjktM2E4OS00Y2Y3LWE3ZjctYzhkZGM3YzA1ZWM2XkEyXkFqcGdeQXVyNzkwMjQ5NzM@._V1_SX300.jpg", result.Poster);
            Assert.Equal("8.6", result.ImdbRating);
            Assert.Equal("1,227,656", result.ImdbVotes);
            Assert.Equal("tt0076759", result.ImdbID);
        }
        private async Task <UserRepos> ExecuteUserReposRequest(string userName, int page, int per_page)
        {
            Uri baseUri = new Uri(GITHUB_HOST);
            Uri fullUri = new Uri(baseUri, "users/" + userName + "/repos?page=" + page + "&per_page=" + per_page);

            HttpResponseMessage responseMessage = await HttpHandler.GetAsync(fullUri);

            if (responseMessage.StatusCode == HttpStatusCode.NotFound)
            {
                return(new UserRepos());
            }
            else if (responseMessage.StatusCode != HttpStatusCode.OK)
            {
                return(null);
            }
            responseMessage.EnsureSuccessStatusCode();                      // If there is an error, this will throw
            string result = await responseMessage.Content.ReadAsStringAsync();

            MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(result));
            DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(UserRepo[]));

            UserRepo[] userRepos = (UserRepo[])jsonSerializer.ReadObject(stream);

            //
            foreach (UserRepo repo in userRepos)
            {
                UserRepo repoPRs = await GetPullRequetsByRepoName(userName, repo.name);

                //Filter Open PRs and reassign
                var openPRs = from p in repoPRs.PullRequests where p.state == "open" select p;

                repo.PullRequests = openPRs.ToList();
            }
            // Order by no of PRs in the list
            userRepos = userRepos.OrderByDescending(x => x.PullRequests.Count).ToArray();

            return(new UserRepos {
                Repos = new List <UserRepo>(userRepos)
            });
        }
예제 #14
0
        public async Task <Location> GetGeocodeJsone(string addressText)
        {
            var encodedAddress = HttpUtility.UrlEncode(addressText);
            HttpResponseMessage httpResponse = await _client.GetAsync($"{_baseUrl}/geocode/json?address={encodedAddress}&key={_apiKey}");

            if (!httpResponse.IsSuccessStatusCode)
            {
                _logger.LogError("");
                throw new Exception("Cannot retrieve tasks");
            }

            var content = await httpResponse.Content.ReadAsStringAsync();

            GeocodeResponse deserializedResponce = JsonConvert.DeserializeObject <GeocodeResponse>(content);
            Location        result = null;

            if (deserializedResponce.Status == "OK" && deserializedResponce.Results.Length != 0)
            {
                result           = new Location();
                result.Latitude  = deserializedResponce.Results.Select(x => x.Geometry.Location.Lat).FirstOrDefault();
                result.Longitude = deserializedResponce.Results.Select(x => x.Geometry.Location.Lng).FirstOrDefault();

                var country = deserializedResponce.Results.Select(x => x.AddressComponents.Where(y => y.Types.Contains("country"))).FirstOrDefault().FirstOrDefault().ShortName;

                var cultureInfosList = CultureInfo.GetCultures(CultureTypes.AllCultures).Where(c => c.Name.EndsWith("-" + country));
                result.CountryCode = country != "GB"? cultureInfosList.FirstOrDefault().ToString() : cultureInfosList.LastOrDefault().ToString();

                result.FormattedAddress = deserializedResponce.Results.Select(x => x.FormatedAddress).FirstOrDefault();

                var      utcTimeNow             = DateTime.UtcNow;
                TimeSpan utcDateNowSpan         = utcTimeNow - new DateTime(1970, 1, 1, 0, 0, 0);
                var      googleTimeZoneResponce = this.GetTimeZoneJsone(result, utcDateNowSpan).Result;
                if (googleTimeZoneResponce.Status == "OK")
                {
                    result.GoogleTimeZone = googleTimeZoneResponce;
                }
            }
            return(result);
        }
예제 #15
0
        public async Task <TDto> GetAsync(string uri)
        {
            var response = await _httpHandler.GetAsync(uri).ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                return(JsonConvert.DeserializeObject <TDto>(content, new UnixDateTimeConverter()));
            }

            return(null);
        }
예제 #16
0
        private async Task <SourceMovieDto> GetMovieFromSourceAsync(string source)
        {
            using (var response = await _httpHandler.GetAsync(source))
            {
                string apiResponse = await response.Content.ReadAsStringAsync();

                if (apiResponse.Contains("Movie not found!"))
                {
                    return(null);
                }

                return(JsonConvert.DeserializeObject <SourceMovieDto>(apiResponse));
            }
        }
        /// <inheritdoc/>
        public async Task <List <Domain.PetOwner> > GetAsync()
        {
            var request  = BuildRequest(_petOwnersServiceUri);
            var response = await _httpHandler.GetAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                throw new ApplicationException($"Cannot communicate with the pet owners service. Details: {response.StatusCode} - {response.ReasonPhrase}");
            }

            var deserializedContent = await GetResponseContent <List <Domain.PetOwner> >(response);

            return(deserializedContent);
        }
예제 #18
0
        public async Task <HttpResponseMessage> DownloadTvShowDataAsync(string seriesName)
        {
            string seriesUrl = ApiUrlBase + seriesName;
            HttpResponseMessage tvShowResponseMessage = await _httpHandler.GetAsync(seriesUrl);

            // <2> Check if Response status is OK. If not -> handle it
            if (!_responseHelper.IsResponseStatusOk(tvShowResponseMessage))
            {
                _responseHelper.HandleError(tvShowResponseMessage);
            }
            // </2>

            return(tvShowResponseMessage);
        }
예제 #19
0
        /// <summary>
        /// Coordinates will be in the format [longitude, latitude].
        /// Our zones are not bound by zip code borders.If you need to check to see if an address is within a given zone, use the Delivery Quote
        /// </summary>
        /// <returns>if sucessful DeliveryZoneCollection else error</returns>
        public async Task <IResponse> GetDeliveryZonesAsync()
        {
            var response      = new Response();
            var contentString = "";

            using (var zoneResponse = await _httpClient.GetAsync("/v1/delivery_zones").ConfigureAwait(false)) {
                try {
                    contentString = await zoneResponse?.Content.ReadAsStringAsync();

                    zoneResponse.EnsureSuccessStatusCode();
                    response._response = new DeliveryZoneCollection {
                        DeliveryZones =
                            Newtonsoft.Json.JsonConvert.DeserializeObject <IEnumerable <DeliveryZone> >(contentString)
                    };;
                    response.Kind = ResponseKinds.delivery_zones;
                }
                catch (Exception ex) {
                    response._response = GetErrorResponse(contentString, zoneResponse.StatusCode, ex);
                    response.Kind      = ResponseKinds.error;
                }
            }
            return(response);
        }
예제 #20
0
        /// <summary>
        /// Retreive reference data details from
        /// </summary>
        /// <param name="categoryName">categoryname</param>
        /// <param name="itemName">Reference data Name</param>
        /// <returns>GUID</returns>
        public Guid GetReferenceDataGuId(string categoryName, string itemName)
        {
            using (loggingHelper.RMTraceManager.StartTrace("IntegrationService.GetReferenceDataGuId"))
            {
                string methodName = typeof(DeliveryPointIntegrationService) + "." + nameof(GetReferenceDataGuId);
                loggingHelper.LogMethodEntry(methodName, priority, entryEventId);

                string actionUrl           = "/simplelists?listName=" + categoryName;
                string requestUrl          = referenceDataWebAPIName + actionUrl;
                HttpResponseMessage result = httpHandler.GetAsync(requestUrl).Result;
                if (!result.IsSuccessStatusCode)
                {
                    var responseContent = result.ReasonPhrase;
                    throw new ServiceException(responseContent);
                }

                Tuple <string, SimpleListDTO> simpleListDTO = JsonConvert.DeserializeObject <Tuple <string, SimpleListDTO> >(result.Content.ReadAsStringAsync().Result);
                var getReferenceDataGuId = simpleListDTO.Item2.ListItems.Where(li => li.Value.Trim().Equals(itemName.Trim(), StringComparison.OrdinalIgnoreCase)).SingleOrDefault().Id;
                loggingHelper.LogMethodExit(methodName, priority, exitEventId);

                return(getReferenceDataGuId);
            }
        }
예제 #21
0
        public async Task <List <PgAccount> > GetPgAccounts(string identifier, int maxLevel, int quantity)
        {
            try
            {
                var builder = new UriBuilder($"{_baseurl}/{GetAccounturl}")
                {
                    Query = $"system_id={identifier}&count={quantity}&min_level=0&max_level={maxLevel-1}&banned_or_new=true"
                };
                var response = await _client.GetAsync(builder.Uri.ToString());

                if (response.IsSuccessStatusCode)
                {
                    var ret = new List <PgAccount>();
                    if (quantity > 0)
                    {
                        if (quantity == 1)
                        {
                            var retAccount = await response.Content.ReadAsAsync <PgAccount>();

                            retAccount.SystemId = identifier;
                            ret.Add(retAccount);
                        }
                        else
                        {
                            var retAccouts = await response.Content.ReadAsAsync <List <PgAccount> >();

                            if (retAccouts != null)
                            {
                                foreach (var t in retAccouts)
                                {
                                    t.SystemId = identifier;
                                    ret.Add(t);
                                }
                            }
                        }
                    }

                    return(ret);
                }

                _logger.Error(response);
                return(null);
            }
            catch (Exception e)
            {
                _logger.Error(e);
                return(null);
            }
        }
예제 #22
0
        public BingResponseDto SearchByExpression(string searchTerm)
        {
            _handler.AddHeader(_subscriptionKey);

            var query = $"{_baseAddress}search?q={searchTerm}&customconfig={_customConfigId}&mkt=en-US";

            System.Threading.Thread.Sleep(1000);

            var response = _handler.GetAsync(query).Result;

            var content = response.Content.ReadAsStringAsync().Result;

            var result = JsonConvert.DeserializeObject <BingResponseDto>(content);

            return(result);
        }
예제 #23
0
        public async Task <TResponse> GetAsync <TResponse>(string queryPath)
        {
            Uri requestUri = new Uri(_uri, queryPath);

            Console.WriteLine($"GET: {queryPath}");
            HttpResponseMessage response = await _httpHandler.GetAsync(requestUri);

            response.EnsureSuccessStatusCode();
            string jsonResponse = await response.Content.ReadAsStringAsync();

            Console.WriteLine("Response:");
            Console.WriteLine(jsonResponse);
            TResponse responseT = JsonConvert.DeserializeObject <TResponse>(jsonResponse);

            return(responseT);
        }
예제 #24
0
        public async Task <List <Whisky> > GetScrapedWhiskies(Random random, HtmlConverter converter, string scraper)
        {
            var url                   = ConfigurationManager.AppSettings[scraper + "url"];
            var startNumber           = Convert.ToInt32(ConfigurationManager.AppSettings[scraper + "startNumber"]);
            var endNumber             = Convert.ToInt32(ConfigurationManager.AppSettings[scraper + "endNumber"]);
            var increaseNumber        = Convert.ToInt32(ConfigurationManager.AppSettings[scraper + "increaseNumber"]);
            var xPathMain             = ConfigurationManager.AppSettings[scraper + "xPathMain"];
            var xPathType             = ConfigurationManager.AppSettings[scraper + "xPathType"];
            var xPathPriceBase        = ConfigurationManager.AppSettings[scraper + "xPathPriceBase"];
            var xPathPriceDecimals    = ConfigurationManager.AppSettings[scraper + "xPathPriceDecimals"];
            var xPathDiscountBase     = ConfigurationManager.AppSettings[scraper + "xPathPriceDiscountBase"];
            var xPathDiscountDecimals = ConfigurationManager.AppSettings[scraper + "xPathPriceDiscountDecimals"];
            var xPathName             = ConfigurationManager.AppSettings[scraper + "xPathName"];
            var xPathStock            = ConfigurationManager.AppSettings[scraper + "xPathInStock"];
            var xPathStockMatch       = ConfigurationManager.AppSettings[scraper + "xPathInStockMatch"];

            var whiskeysTotal = new List <Whisky>();

            for (var i = startNumber; i < endNumber; i += increaseNumber)
            {
                var urlToScrape = url.Replace("{i}", $"{i}");

                // Wait random time to not over request webpage
                if (i != 0)
                {
                    _utils.Sleep(random.Next(3000, 23000));
                }

                var response = await _httpHandler.GetAsync(urlToScrape);

                if (!response.IsSuccessStatusCode)
                {
                    _logger.LogError($"No 200 statuscode for: {urlToScrape}");
                }

                var pageContent = await _httpHandler.ReadAsStringAsync(response.Content);

                var whiskeys = converter.ReadHtmlDocument(pageContent, xPathMain, xPathPriceBase, xPathName,
                                                          xPathStock, xPathStockMatch, scraper, xPathType,
                                                          xPathPriceDecimals, xPathDiscountBase, xPathDiscountDecimals);

                whiskeysTotal.AddRange(whiskeys);
            }

            return(whiskeysTotal);
        }
예제 #25
0
        public virtual async Task <ApiResponse> FetchAsync(string path)
        {
            IncrementRequestId();
            var response = new ApiResponse {
                RequestId = RequestId
            };

            HttpResponseMessage message = await _httpClient.GetAsync(path);

            if (message.IsSuccessStatusCode)
            {
                var data = await message.Content.ReadAsAsync <Show[]>();

                response.RequestId = RequestId;
                response.Shows     = data.Where(s => s.StartDate >= DateTime.Now).OrderBy(s => s.StartDate).ToArray();
            }

            return(response);
        }
예제 #26
0
        private async Task <T> Get <T>(string endpoint) where T : class
        {
            HttpResponseMessage result = null;

            try
            {
                result = await _client.GetAsync(endpoint);

                result.EnsureSuccessStatusCode();
                return(JsonConvert.DeserializeObject <T>(await result.Content.ReadAsStringAsync()));
            }
            catch (HttpRequestException) when((int)result?.StatusCode == 429)
            {
                throw new TooManyRequestsException();
            }
            catch (HttpRequestException) when(result?.StatusCode == System.Net.HttpStatusCode.NotFound)
            {
                throw new EndOfShowsException();
            }
        }
예제 #27
0
        public async Task <T> Get(string url)
        {
            try
            {
                var response = await _httpClient.GetAsync(url);

                if (!response.IsSuccessStatusCode)
                {
                    return(default(T));
                }
                var result = await response.Content.ReadAsAsync <T>();

                result.Url = url;
                return(result);
            }
            catch (HttpRequestException)
            {
                return(default(T));
            }
        }
예제 #28
0
        //private async Task<T> GetAsync<T>(string uri, CancellationToken cancellationToken)
        //{
        //    HttpResponseMessage response = await _client.GetAsync(uri, cancellationToken);

        //    if (response.IsSuccessStatusCode)
        //    {
        //        var json = await response.Content.ReadAsStringAsync();
        //        var result = JsonConvert.DeserializeObject<T>(json);
        //        return result;
        //    }

        //    return default(T);
        //}

        public async Task <IEnumerable <Meal> > GetMeals(CancellationToken token)
        {
            var meals = new List <Meal>();

            try
            {
                var response = await _handler.GetAsync("api/meals/");

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

                    meals = JsonConvert.DeserializeObject <List <Meal> >(json);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(meals);
        }
예제 #29
0
        public async Task <IJwtToken> ValidateTokenAsync(IOauthToken token)
        {
            IJwtToken jwtToken;

            using (HttpResponseMessage response = await client.GetAsync(config.JwtKeySetEndpoint))
            {
                Stream stream = await response.Content.ReadAsStreamAsync();

                JwtKeySet keySet = await JsonSerializer.DeserializeAsync <JwtKeySet>(stream);

                var headers = JWT.Headers(token.AccessToken);
                var jwk     = keySet.Keys[1];

                RSACryptoServiceProvider key = new RSACryptoServiceProvider();
                key.ImportParameters(new RSAParameters
                {
                    Modulus  = Base64Url.Decode(jwk.n),
                    Exponent = Base64Url.Decode(jwk.e)
                });

                jwtToken = JWT.Decode <JwtToken>(token.AccessToken, key);

                if (jwtToken.Issuer != "login.eveonline.com")
                {
                    throw new Exception("Invalid JWT Token!");
                }

                uint unixTimestamp = (uint)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
                if (jwtToken.Expiery < unixTimestamp)
                {
                    throw new Exception("Invalid JWT Token");
                }
            }

            return(jwtToken);
        }
예제 #30
0
        public async Task <FilmViewModel> GetAllFilms()
        {
            var uri    = "/api/films/";
            var result = _httpHandler.GetAsync(uri).Result;
            var vm     = JsonConvert.DeserializeObject <FilmViewModel>(result);

            var filmDb = _repository.GetAllFilms().ToList();

            if (!filmDb.Any())
            {
                return(vm);
            }
            foreach (var filmResult in vm.Results)
            {
                var first = filmDb.FirstOrDefault(f => f.EpisodeId == filmResult.Episode_Id);

                if (first != null)
                {
                    filmResult.Ranking = first.Average;
                }
            }

            return(vm);
        }