public void LoginInBackground(string authenticationToken, Action <MobileServiceUser, Exception> continueWith)
        {
            // Proper Async Tasks Programming cannot integrate with Windows Phone (stupid) Async Mechanisim which use Events... (ex: UploadStringCompleted)
            //var asyncTask = new Task<MobileServiceUser>(() => this.StartLoginAsync(authenticationToken));
            //asyncTask.Start();
            //return asyncTask;

            if (authenticationToken == null)
            {
                throw new ArgumentNullException("authenticationToken");
            }

            if (string.IsNullOrEmpty(authenticationToken))
            {
                throw new ArgumentException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              "Error! Empty Argument: {0}",
                              "authenticationToken"));
            }


            var client = _clientFactory.GetClient();
            var url    = _serviceUrl + LoginAsyncUriFragment + "?mode=" + LoginAsyncAuthenticationTokenKey;

            client.Headers[HttpRequestHeader.ContentType] = RequestJsonContentType;
            var payload = new JObject();

            payload[LoginAsyncAuthenticationTokenKey] = authenticationToken;

            client.UploadStringCompleted += (x, args) =>
            {
                if (args.Error != null)
                {
                    var ex = args.Error;
                    if (args.Error.InnerException != null)
                    {
                        ex = args.Error.InnerException;
                    }
                    continueWith(null, ex);
                    return;
                }
                var result = JObject.Parse(args.Result);
                CurrentAuthToken = result["authenticationToken"].Value <string>();
                CurrentUser      =
                    new MobileServiceUser(result["user"]["userId"].Value <string>());
                continueWith(CurrentUser, null);
            };

            //Go!

            client.UploadStringAsync(new Uri(url), payload.ToString());
        }
Ejemplo n.º 2
0
 private void cacheData()
 {
     using (var client = factory.GetClient())
     {
         var jsonString = client.DownloadString("https://jsonplaceholder.typicode.com/posts");
         data      = JsonConvert.DeserializeObject <List <Post> >(jsonString);
         HasLoaded = true;
     }
 }
Ejemplo n.º 3
0
        public void DownLoad(string url, string destination)
        {
            var client = _webClientFactory.GetClient();

            var waiter = new ManualResetEventSlim();

            using (waiter)
            {
                client.InvalidUrlRequested += (sender, args) =>
                {
                    var oldColor = Console.BackgroundColor;
                    Console.BackgroundColor = ConsoleColor.DarkRed;
                    Console.WriteLine($"Invalid URL {args.Value}");
                    Console.BackgroundColor = oldColor;
                };

                client.DownloadProgressChanged += (sender, args) =>
                {
                    Console.WriteLine($"Downloading...{args.ProgressPercentage}% complete ({args.BytesReceived:N0} bytes)");
                };

                client.DownloadCompleted += (sender, args) =>
                {
                    Console.WriteLine($"Downloaded to {destination} ");
                    waiter.Set();
                };

                Console.WriteLine($"Downloading {url}...");
                var disposable = client.DownloadFile(url, destination);
                if (disposable == null)
                {
                    return;
                }

                using (disposable)
                {
                    if (waiter.Wait(TimeSpan.FromSeconds(10D)))
                    {
                        Console.WriteLine($"Launching {destination}");
                        Process.Start("explorer.exe", destination);
                    }
                    else
                    {
                        Console.WriteLine($"Timedout downloading {url}");
                    }
                }
            }
        }
Ejemplo n.º 4
0
 public RequestHelper(ILogger <RequestHelper> logger, IWebClientFactory clientFactory)
 {
     _logger     = logger;
     _httpClient = clientFactory.GetClient(this);
     _httpClient.AddRequestHelperDefaults();
 }