/// <summary> /// Uploads data to the API. /// </summary> /// <param name="dinero">An instance of the Dinero class. This is needed since it know about the user, the app and their credentials.</param> /// <param name="method">Method to use for the request. Currently just POST.</param> /// <param name="url">URL to dupload the data to.</param> /// <param name="data">Data to upload. Should be a JSON string.</param> /// <param name="callback">Actin to call when the data has been uploaded and the server has returned.</param> private static void UploadDataAsync(Dinero dinero, string method, string url, string data, Action<DineroResult<string>> callback) { using (var httpClient = new HttpClient()) { httpClient.SetBearerToken(dinero.GetRequestToken()); if (method == "POST") { var stringContent = new StringContent(data, Encoding.UTF8, "application/json"); var r = httpClient.PostAsync(new Uri(url), stringContent).Result; var local = new DineroResult<string>(); if (r.IsSuccessStatusCode) { local.Result = r.Content.ReadAsStringAsync().Result; } else { local.HasError = true; local.ErrorMessage = r.Content.ReadAsStringAsync().Result; } callback(local); } } }
/// <summary> /// Download data from the API. /// </summary> /// <param name="dinero">An instance of the Dinero class. This is needed since it know about the user, the app and their credentials.</param> /// <param name="method">Method to be used for the request. Currently just GET.</param> /// <param name="url">URL to download the data from.</param> /// <param name="callback">Action to call when data has downloaded.</param> private static void DownloadDataAsync(Dinero dinero, string method, string url, Action<DineroResult<string>> callback) { using (var httpClient = new HttpClient()) { httpClient.SetBearerToken(dinero.GetRequestToken()); if (method == "GET") { var r = httpClient.GetAsync(new Uri(url)).Result; var local = new DineroResult<string>(); if (r.IsSuccessStatusCode) { local.Result = r.Content.ReadAsStringAsync().Result; } else { local.HasError = true; local.ErrorMessage = r.Content.ReadAsStringAsync().Result; } callback(local); } } }