Exemplo n.º 1
0
        /// <summary>
        /// Posts data to the specified URL.
        /// </summary>
        /// <param name="url">URL to retrieve data from.</param>
        /// <param name="ct">Cancellation token.</param>
        /// <param name="httpConnectionSettings">Settings to create the HttpContent value.  Doing it inside the method allows for connection retries.</param>
        /// <param name="connectionRetrySettings">The settings to define whether a connection should be retried.</param>
        /// <returns>Response contents as string else null if nothing.</returns>
        protected async Task <string> PostAsync(
            string url,
            CancellationToken ct,
            HttpConnectionSettings httpConnectionSettings,
            ConnectionRetrySettings connectionRetrySettings = null)
        {
            if (connectionRetrySettings == null)
            {
                connectionRetrySettings = new ConnectionRetrySettings();
            }

            HttpResponseMessage response = await this.PostAsync(
                url,
                new StringContent(httpConnectionSettings.Content, httpConnectionSettings.Encoding, httpConnectionSettings.MediaType),
                ct);

            var data = await response.Content?.ReadAsStringAsync();

            if (DoesThisNeedToRetry(connectionRetrySettings, data, response.StatusCode))
            {
                connectionRetrySettings.CurrentTry++;
                return(await PostAsync(url, ct, httpConnectionSettings, connectionRetrySettings));
            }
            ThrowIfAccessLimitReached(data, response.StatusCode);

            return(data);
        }
Exemplo n.º 2
0
        /*
         * The GraphQL endpoint is currently only available in the Yelp Fusion (3.0) Api Beta.
         * To use these endpoints, you have to go to Manage App and opt into the Beta.
         */

        #region Individual Graph Request

        /// <summary>
        /// This method makes a single request to the Yelp GraphQL endpoint.
        /// It formats the entire list of businessIds and the search fragment into the proper json to make the request.
        /// *** NOTE ***
        /// The GraphQL endpoint is currently only available in the Yelp Fusion (3.0) Api Beta.
        /// To use these endpoints, you have to go to Manage App and opt into the Beta.
        /// </summary>
        /// <param name="businessIds">A list of Yelp Business Ids to request from the GraphQL endpoint.</param>
        /// <param name="ct">Cancellation token instance. Use CancellationToken.None if not needed.</param>
        /// <param name="connectionRetrySettings">The settings to define whether a connection should be retried.</param>
        /// <param name="fragment">The search fragment to be used on all requested Business Ids.  The DEFAULT_FRAGMENT is used by default.</param>
        /// <returns>A task of an IEnumerable of all the BusinessResponses from the GraphQL API.</returns>
        public async Task <IEnumerable <BusinessResponse> > GetGraphQlAsync(
            List <string> businessIds,
            CancellationToken ct = default(CancellationToken),
            ConnectionRetrySettings connectionRetrySettings = null,
            string fragment = DEFAULT_FRAGMENT)
        {
            if (!businessIds.Any())
            {
                return(new List <BusinessResponse>());
            }

            var httpConnectionSettings = new HttpConnectionSettings
            {
                Content   = CreateRequestBodyForGraphQl(businessIds, fragment),
                Encoding  = Encoding.UTF8,
                MediaType = "application/graphql"
            };

            ApplyAuthenticationHeaders(ct);
            var jsonResponse = await PostAsync(API_VERSION + "/graphql", ct, httpConnectionSettings, connectionRetrySettings);

            return(ConvertJsonToBusinesResponses(jsonResponse));
        }