Exemplo n.º 1
0
        /// <summary>
        /// Makes the API request
        /// </summary>
        /// <typeparam name="T">The type of response</typeparam>
        /// <param name="command">The command to call.</param>
        /// <param name="settings">The app id.</param>
        /// <param name="querystring">The querystring params.</param>
        /// <param name="callback">The callback to hit when done.</param>
        /// <param name="requestHeaders">HTTP headers to add to the request</param>
        public void SendRequestAsync <T>(
            MusicClientCommand command,
            IMusicClientSettings settings,
            List <KeyValuePair <string, string> > querystring,
            IResponseCallback <T> callback,
            Dictionary <string, string> requestHeaders = null)
        {
            this._lastSettings = settings;
            this._queryString  = querystring;

            // Ensure URI building is exercised...
            Uri uri = this.UriBuilder.BuildUri(command, settings, querystring);

            // Ensure we call this method to make
            // sure the code gets a run through...
            string body = command.BuildRequestBody();

            if (this._responseInfo != null)
            {
                command.SetAdditionalResponseInfo(this._responseInfo);
            }

            this.NextFakeResponse.DoCallback <T>(callback);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Performs the callback specified with the previously queued up response
        /// </summary>
        /// <typeparam name="T">The type of response</typeparam>
        /// <param name="callback">The callback action</param>
        public void DoCallback <T>(IResponseCallback <T> callback)
        {
            var resp = this._response as Response <T>;

            callback.Callback(resp);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Makes the API request
        /// </summary>
        /// <typeparam name="T">The type of response item</typeparam>
        /// <param name="command">The command to call.</param>
        /// <param name="settings">The music client settings.</param>
        /// <param name="queryParams">The querystring.</param>
        /// <param name="callback">The callback to hit when done.</param>
        /// <param name="requestHeaders">HTTP headers to add to the request</param>
        /// <exception cref="System.ArgumentNullException">Thrown when no callback is specified</exception>
        public void SendRequestAsync <T>(
            MusicClientCommand command,
            IMusicClientSettings settings,
            List <KeyValuePair <string, string> > queryParams,
            IResponseCallback <T> callback,
            Dictionary <string, string> requestHeaders = null)
        {
            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }

            Uri uri = this.UriBuilder.BuildUri(command, settings, queryParams);

            DebugLogger.Instance.WriteLog("Calling {0}", uri.ToString());

            TimedRequest request = new TimedRequest(uri);

            this.AddRequestHeaders(request.WebRequest, requestHeaders);

            this.TryBuildRequestBody(
                (IAsyncResult ar) =>
            {
                if (request.HasTimedOut)
                {
                    return;
                }

                WebResponse response        = null;
                HttpWebResponse webResponse = null;
                T responseItem            = default(T);
                HttpStatusCode?statusCode = null;
                Exception error           = null;
                string responseBody       = null;

                try
                {
                    response    = request.WebRequest.EndGetResponse(ar);
                    webResponse = response as HttpWebResponse;
                    if (webResponse != null)
                    {
                        statusCode = webResponse.StatusCode;
                        command.SetAdditionalResponseInfo(new ResponseInfo(webResponse.ResponseUri, webResponse.Headers));

                        // Capture Server Time offset if we haven't already...
                        this.DeriveServerTimeOffset(webResponse.Headers);
                    }
                }
                catch (WebException ex)
                {
                    DebugLogger.Instance.WriteVerboseInfo("Web Exception: {0} when calling {1}", ex, uri);
                    error = ex;
                    if (ex.Response != null)
                    {
                        response    = ex.Response;
                        webResponse = (HttpWebResponse)ex.Response;
                        statusCode  = webResponse.StatusCode;
                    }
                }

                string contentType = null;

                if (response != null)
                {
                    contentType = response.ContentType;
                    try
                    {
                        using (Stream responseStream = this._gzipHandler.GetResponseStream(response))
                        {
                            responseBody = responseStream.AsString();
                            responseItem = callback.ConvertFromRawResponse(responseBody);
                        }
                    }
                    catch (Exception ex)
                    {
                        DebugLogger.Instance.WriteVerboseInfo("Exception: {0} trying to handle response stream when calling {1}", ex, uri);
                        error        = ex;
                        responseItem = default(T);
                    }
                }

                DoCallback(callback.Callback, responseItem, statusCode, contentType, error, responseBody, command.RequestId, uri, IsFake404(response));
            },
                () => DoCallback(callback.Callback, default(T), null, null, new ApiCallFailedException(), null, command.RequestId, uri),
                request,
                command);
        }