Exemplo n.º 1
0
        /// <summary>
        /// Asynchronously queries this method, fetching all pages if necessary, with the
        /// provided request data.
        /// </summary>
        /// <typeparam name="T">The subtype to deserialize (the deserialized type being
        /// <see cref="CCPAPIResult{T}" />). It must be a collection type of U!</typeparam>
        /// <typeparam name="U">The item type to deserialize.</typeparam>
        /// <param name="method">The method to query</param>
        /// <param name="callback">The callback to invoke once the query has been completed.
        /// </param>
        /// <param name="data">The parameters to use for the request, including the token,
        /// arguments, and POST data.</param>
        /// <param name="state">State to be passed to the callback when it is used.</param>
        /// <exception cref="System.ArgumentNullException">callback; The callback cannot be
        /// null.</exception>
        public void QueryPagedEsi <T, U>(Enum method, ESIRequestCallback <T> callback,
                                         ESIParams data, object state = null) where T : List <U> where U : class
        {
            callback.ThrowIfNull(nameof(callback), "The callback cannot be null.");

            Uri url = GetESIUrl(method, data);

            Util.DownloadJsonAsync <T>(url, GetRequestParams(data)).ContinueWith(task =>
            {
                var esiResult = GetESIResult(task.Result);
                // Check page count
                int pages = esiResult.Response.Pages;
                if (pages > 1 && esiResult.HasData && !esiResult.HasError)
                {
                    // Fetch the other pages
                    QueryEsiPageHelper(method, callback, data, new PageInfo <T, U>(esiResult,
                                                                                   pages, state));
                }
                else
                {
                    // Invokes the callback
                    Dispatcher.Invoke(() => callback.Invoke(esiResult, state));
                }
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// Asynchronously queries this method with the provided ID and HTTP POST data.
        /// </summary>
        /// <typeparam name="T">The subtype to deserialize (the deserialized type being
        /// <see cref="CCPAPIResult{T}" />).</typeparam>
        /// <param name="method">The method to query</param>
        /// <param name="callback">The callback to invoke once the query has been completed.
        /// </param>
        /// <param name="data">The parameters to use for the request, including the token,
        /// arguments, and POST data.</param>
        /// <param name="state">State to be passed to the callback when it is used.</param>
        /// <exception cref="System.ArgumentNullException">callback; The callback cannot be
        /// null.</exception>
        public void QueryEsi <T>(Enum method, ESIRequestCallback <T> callback, ESIParams
                                 data, object state = null) where T : class
        {
            var response = data.LastResponse;

            // Check callback not null
            callback.ThrowIfNull(nameof(callback), "The callback cannot be null.");
            Uri url = GetESIUrl(method, data.ParamOne, data.ParamTwo, data.GetData);

            Util.DownloadJsonAsync <T>(url, new RequestParams(data.PostData)
            {
                Authentication  = data.Token,
                AcceptEncoded   = SupportsCompressedResponse,
                ETag            = response?.ETag,
                IfModifiedSince = response?.Expires
            }).ContinueWith(task =>
            {
                var esiResult = new EsiResult <T>(task.Result);
                // Sync clock on the answer if necessary and provided
                var sync      = esiResult.Result as ISynchronizableWithLocalClock;
                DateTime?when = esiResult.CurrentTime;
                if (sync != null && when != null)
                {
                    sync.SynchronizeWithLocalClock(DateTime.UtcNow - (DateTime)when);
                }
                // Invokes the callback
                Dispatcher.Invoke(() => callback.Invoke(esiResult, state));
            });
        }
Exemplo n.º 3
0
 /// <summary>
 /// Creates the request parameters for ESI.
 /// </summary>
 /// <param name="data">The ESI parameters.</param>
 /// <returns>The required request parameters, including the ETag/Expiry (if supplied)
 /// and POST data/token.</returns>
 private RequestParams GetRequestParams(ESIParams data)
 {
     return(new RequestParams(data.LastResponse, data.PostData)
     {
         Authentication = data.Token,
         AcceptEncoded = SupportsCompressedResponse
     });
 }
Exemplo n.º 4
0
        /// <summary>
        /// Asynchronously queries this method with the provided request data.
        /// </summary>
        /// <typeparam name="T">The subtype to deserialize (the deserialized type being
        /// <see cref="CCPAPIResult{T}" />).</typeparam>
        /// <param name="method">The method to query</param>
        /// <param name="callback">The callback to invoke once the query has been completed.
        /// </param>
        /// <param name="data">The parameters to use for the request, including the token,
        /// arguments, and POST data.</param>
        /// <param name="state">State to be passed to the callback when it is used.</param>
        /// <exception cref="System.ArgumentNullException">callback; The callback cannot be
        /// null.</exception>
        public void QueryEsi <T>(Enum method, ESIRequestCallback <T> callback, ESIParams
                                 data, object state = null) where T : class
        {
            callback.ThrowIfNull(nameof(callback), "The callback cannot be null.");

            Uri url = GetESIUrl(method, data);

            Util.DownloadJsonAsync <T>(url, GetRequestParams(data)).ContinueWith(task =>
            {
                // Invokes the callback
                Dispatcher.Invoke(() => callback.Invoke(GetESIResult(task.Result), state));
            });
        }
Exemplo n.º 5
0
        /// <summary>
        /// Returns the full canonical ESI URL for the specified APIMethod as constructed from
        /// the Server and APIMethod properties.
        /// </summary>
        /// <param name="requestMethod">An APIMethodsEnum enumeration member specifying the
        /// method for which the URL is required.</param>
        /// <param name="data">The ESI parameters for this URL.</param>
        /// <param name="page">The page to fetch; 0 or 1 will fetch without requesting a page
        /// </param>
        /// <returns>A String representing the full URL path of the specified method.</returns>
        private Uri GetESIUrl(Enum requestMethod, ESIParams data, int page = 1)
        {
            long   id       = data.ParamOne;
            string paramStr = string.IsNullOrEmpty(data.GetData) ? data.ParamTwo.ToString(
                CultureConstants.InvariantCulture) : data.GetData;
            string path = string.Format(GetESIMethod(requestMethod).Path, id, paramStr);

            // Build the URI
            var builder = new UriBuilder(new Uri(NetworkConstants.ESIBase));

            builder.Path = Path.Combine(builder.Path, path);
            if (page > 1)
            {
                builder.Query = "page=" + page.ToString(CultureConstants.InvariantCulture);
            }
            return(builder.Uri);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Helper method for fetching paginated items.
        /// </summary>
        /// <typeparam name="T">The subtype to deserialize (the deserialized type being
        /// <see cref="CCPAPIResult{T}" />). It must be a collection type of U!</typeparam>
        /// <typeparam name="U">The item type to deserialize.</typeparam>
        /// <param name="method">The method to query</param>
        /// <param name="callback">The callback to invoke once the query has been completed.
        /// </param>
        /// <param name="data">The parameters to use for the request, including the token,
        /// arguments, and POST data.</param>
        /// <param name="state">State to be passed to the callback when it is used.</param>
        private void QueryEsiPageHelper <T, U>(Enum method, ESIRequestCallback <T> callback,
                                               ESIParams data, PageInfo <T, U> state) where T : List <U> where U : class
        {
            int page    = state.CurrentPage;
            var first   = state.FirstResult;
            Uri pageUrl = GetESIUrl(method, data, page);

            // Create RequestParams manually to zero out the ETag/Expiry since it was already
            // checked
            Util.DownloadJsonAsync <T>(pageUrl, new RequestParams(null, data.PostData)
            {
                Authentication = data.Token,
                AcceptEncoded  = SupportsCompressedResponse
            }).ContinueWith(task =>
            {
                var esiResult        = GetESIResult(task.Result);
                object callbackState = state.State;
                if (esiResult.HasError)
                {
                    // Invoke the callback if an error occurred
                    Dispatcher.Invoke(() => callback.Invoke(esiResult, callbackState));
                }
                else if (!esiResult.HasData)
                {
                    // This should not occur
                    Dispatcher.Invoke(() => callback.Invoke(first, callbackState));
                }
                else
                {
                    first.Result.AddRange(esiResult.Result);
                    if (page >= state.LastPage)
                    {
                        // All pages fetched
                        Dispatcher.Invoke(() => callback.Invoke(first, callbackState));
                    }
                    else
                    {
                        // Go to the next page
                        QueryEsiPageHelper(method, callback, data, state.NextPage());
                    }
                }
            });
        }