Esempio n. 1
0
        public void ShortUrlAsync(string longUrl, object userState, AsyncCompletedCallback <string> callback)
        {
            if (!string.IsNullOrEmpty(ApiKey))
            {
                Shrink(longUrl, userState, callback);
            }
            else
            {
                ApiKeyInputWindow apiKeyInput = new ApiKeyInputWindow();

                apiKeyInput.Show();

                apiKeyInput.ApiKeyReceived += (s, e) =>
                {
                    ApiKey = apiKeyInput.ApiKey;
                    Shrink(longUrl, userState, callback);
                };
            }
        }
Esempio n. 2
0
        private void Shrink(string longUrl, object userState, AsyncCompletedCallback <string> callback)
        {
            string     requestUrl = String.Format(Endpoint, longUrl, ApiKey);
            WebRequest request    = WebRequest.Create(new Uri(requestUrl));

            request.BeginGetResponse(
                s =>
            {
                Exception error;
                string shortUrl = String.Empty;

                try
                {
                    WebResponse response = request.EndGetResponse(s);

                    using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                    {
                        shortUrl = sr.ReadToEnd();
                    }

                    error = ValidateShortUrl(shortUrl);

                    if (error != null && error.Message.Contains("ApiKey"))
                    {
                        ApiKey = string.Empty;
                    }
                }
                catch (Exception ex)
                {
                    error = ex;
                }

                callback(this, new AsyncCompletedEventArgs <string>(error, false, userState, shortUrl));
            },
                null);
        }