예제 #1
0
        private async Task <IEnumerable <Place> > GetPlacesNearCoordinateWithPageToken(double latitude, double longitude, double radiusMeters, string pageToken)
        {
            string locationArgument = $"{latitude},{longitude}";
            GoogleApiRequestBuilder requestBuilder = new GoogleApiRequestBuilder()
                                                     .ToApi(PlaceApiUri)
                                                     .ToSubApi(NearbySearchSubApiUri)
                                                     .WithQueryParam("location", locationArgument)
                                                     .WithQueryParam("radius", radiusMeters.ToString());

            if (pageToken != null)
            {
                requestBuilder = requestBuilder.WithQueryParam("pagetoken", pageToken);
            }
            GoogleApiRequest request = requestBuilder.Build();

            GoogleApiResponse response = await this.client.Get(request);

            IEnumerable <Place> places = response.ConvertResults <Place>();

            if (!string.IsNullOrEmpty(response.NextPageToken))
            {
                await Task.Delay(2000); // I've found that you have to wait a little bit to get the next page. Otherwise, it gives back an "invalid request" response.

                places = places.Concat(await GetPlacesNearCoordinateWithPageToken(latitude, longitude, radiusMeters, response.NextPageToken));
            }

            return(places);
        }
예제 #2
0
        public async Task <string> ConvertToHtml5Async(byte[] swf)
        {
            if (swf == null)
            {
                throw new ArgumentNullException(nameof(swf));
            }

            using (var httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Add("Host", "www.googleapis.com");
                httpClient.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
                httpClient.Timeout = TimeSpan.FromMilliseconds(Options.MillisecondsTimeout);

                var base64Data = Convert.ToBase64String(swf)
                                 .Replace('+', '-')
                                 .Replace('/', '_');

                var requestModel = new GoogleApiRequest
                {
                    ApiVersion = "v1",
                    Method     = "swiffy.convertToHtml",
                    Params     = new SwiffyParams
                    {
                        Client = "Swiffy Flash Extension",
                        Input  = base64Data
                    }
                };

                using (var streamContent = new PushStreamContent(stream => Task.Run(() => _requestSerializer.Value.WriteObject(stream, requestModel))))
                {
                    using (var response = await httpClient.PostAsync(GOOGLE_API_URL, streamContent).ConfigureAwait(false))
                    {
                        var responseJson = (GoogleApiResponse)_responseSerializer.Value.ReadObject(await response.Content.ReadAsStreamAsync().ConfigureAwait(false));

                        if (responseJson.Error != null)
                        {
                            throw new Exception($"Conversion error. {responseJson.Error.Message}.");
                        }

                        if (responseJson.Result?.Response?.Status == "SUCCESS")
                        {
                            var base64 = responseJson.Result.Response.Output.Replace('-', '+')
                                         .Replace('_', '/');

                            base64 = base64.PadRight(base64.Length + (4 - base64.Length % 4) % 4, '=');

                            var gzip = Convert.FromBase64String(base64);

                            using (var memoryStream = new MemoryStream(gzip))
                            {
                                using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
                                {
                                    using (var streamReader = new StreamReader(gzipStream))
                                    {
                                        return(await streamReader.ReadToEndAsync().ConfigureAwait(false));
                                    }
                                }
                            }
                        }
                        else
                        {
                            throw new Exception($"Status error. Статус: {responseJson.Result?.Response?.Status}");
                        }
                    }
                }
            }
        }