/// <summary> /// Begins an asynchronous request for the country-specific Alexa ranking. /// </summary> /// <param name="callback">The callback method.</param> /// <param name="country">The country.</param> /// <param name="pages">The number of pages to return.</param> /// <param name="ranking">The ranking list.</param> /// <param name="userState">The user state.</param> public IAsyncResult BeginGetCountryRanking(AsyncCallback callback, AlexaCountry country, int pages, AlexaRanking ranking = null, object userState = null) { // Create the request state. AlexaRankingRequestState asyncState = new AlexaRankingRequestState(callback, country, pages, ranking, userState); // Call the internal request method. this.BeginGetRankingInternal(asyncState, this.GetUriCountryRanking); // Return the request state. return asyncState; }
/// <summary> /// Computes the request URI for the Alexa country ranking. /// </summary> /// <param name="asyncState">The request state</param> /// <param name="uri">The request URI.</param> /// <param name="country">The request country.</param> private void GetUriCountryRanking(AlexaRankingRequestState asyncState, out Uri uri, out AlexaCountry country) { // Set the URI. uri = new Uri(AlexaRequest.urlRankingCountry.FormatWith(asyncState.Page++, asyncState.Country.Code)); // Set the country. country = asyncState.Country; }
/// <summary> /// Computes the request URI for the Alexa global ranking. /// </summary> /// <param name="asyncState">The request state</param> /// <param name="uri">The request URI.</param> /// <param name="country">The request country.</param> private void GetUriGlobalRanking(AlexaRankingRequestState asyncState, out Uri uri, out AlexaCountry country) { // Set the URI. uri = new Uri(AlexaRequest.urlRankingGlobal.FormatWith(asyncState.Page++)); // Set the country. country = AlexaCountry.Global; }
/// <summary> /// Begins an asynchronous request for the Alexa ranking. /// </summary> /// <param name="asyncState">The asynchronous state.</param> private void BeginGetRankingInternal(AlexaRankingRequestState asyncState, AlexaRankingAction actionUri) { // The URI. Uri uri; // The country. AlexaCountry country; // Call the ranking action. actionUri(asyncState, out uri, out country); // Begin an asynchronous request for the Alexa ranking. asyncState.Result = base.Begin(uri, (AsyncWebResult asyncResult) => { try { // The request data. string data; // Complete the request. AsyncWebResult result = base.End(asyncResult, out data); // Parse the list of countries. asyncState.Ranking.Parse(data, country); // If there are more pages. if (asyncState.Page < asyncState.Pages) { // Begin a new request. this.BeginGetRankingInternal(asyncState, actionUri); } else { // Complete the request. asyncState.Complete(); // Call the callback method. if (null != asyncState.Callback) asyncState.Callback(asyncState); // Dispose the state. asyncState.Dispose(); } } catch (Exception exception) { // Set the exception. asyncState.Exception = exception; // Complete the request. asyncState.Complete(); // Call the callback method. if (null != asyncState.Callback) asyncState.Callback(asyncState); // Dispose the state. asyncState.Dispose(); } }, asyncState); }