Advertisement
Guest User

Untitled

a guest
May 30th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.73 KB | None | 0 0
  1. public async Task<T> ExecuteAsync<T>(RestRequest request)
  2.         {
  3.             var tcs = new TaskCompletionSource<T>();
  4.             App.Client.ExecuteAsync(request, resp =>
  5.             {
  6.                 //has there been an error?
  7.                 if (resp.StatusCode != System.Net.HttpStatusCode.OK)
  8.                 {
  9.                     tcs.SetException(new Exception("oh no"));
  10.                 }
  11.                 else
  12.                 {
  13.                     var result = JsonConvert.DeserializeObject<T>(resp.Content);
  14.                     tcs.TrySetResult(result);
  15.                 }
  16.             });
  17.             return await tcs.Task;
  18.         }
  19.  
  20.         public async Task<bool> doLogin(string email, string password)
  21.         {
  22.             RestRequest loginRequest = new RestRequest("/login");
  23.             setupHeaders(loginRequest, true);
  24.             loginRequest.Method = Method.POST;
  25.             loginRequest.AddParameter("email",email);
  26.             loginRequest.AddParameter("password", password);
  27.  
  28.             MyLoginObject loginResult;
  29.  
  30.             //following is used it lots of other methods that need to make web requests, uses REST Sharp
  31.             try
  32.             {
  33.                 //make the request
  34.                 loginResult = await ExecuteAsync<CannesConnectLoginDetails>(loginRequest);
  35.             }
  36.             catch (Exception ex)
  37.             {
  38.                 return false;
  39.             }
  40.  
  41.             //check the result
  42.             if (loginResult == null)
  43.                 return false;
  44.  
  45.             //save login details and return
  46.             App.Settings[USER_ID_SETTINGS_KEY] = loginResult.USERID;
  47.             App.Settings[USER_TOKEN_KEY] = loginResult.USERTOKEN;
  48.  
  49.             return true;
  50.  
  51.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement