예제 #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));
                }
            });
        }
예제 #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="state">State to be passed to the callback when it is used.</param>
        /// <param name="data">The parameters to use for the request, including the token, arguments, and POST data.</param>
        /// <exception cref="System.ArgumentNullException">callback; The callback cannot be null.</exception>
        private void QueryEsiAsync <T>(Enum method, ESIRequestCallback <T> callback, object state,
                                       EsiParams data) where T : class
        {
            // Check callback not null
            callback.ThrowIfNull(nameof(callback), "The callback cannot be null.");

            // Lazy download
            Uri url = GetESIUrl(method, data.ParamOne, data.ParamTwo, data.GetData);

            Util.DownloadJsonAsync <T>(url, data.Token, SupportsCompressedResponse, data.PostData)
            .ContinueWith(task =>
            {
                var esiResult = new EsiResult <T>(task.Result);

                // Sync clock on the answer if necessary
                var sync = esiResult.Result as ISynchronizableWithLocalClock;
                if (sync != null)
                {
                    sync.SynchronizeWithLocalClock(DateTime.UtcNow - esiResult.CurrentTime);
                }

                // Invokes the callback
                Dispatcher.Invoke(() => callback.Invoke(esiResult, state));
            });
        }
예제 #3
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));
            });
        }
예제 #4
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());
                    }
                }
            });
        }
예제 #5
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));
            });
        }