/// <summary> /// Delete the specified resource /// </summary> /// <param name="url"></param> /// <param name="encodedId">The user's base64 encoded user ID</param> /// <returns></returns> public async Task <HttpResponseMessage> DeleteData(string url, string encodedId) { using (var client = new HttpClient()) { string token = JsonWebTokenServices.CreateJsonWebtoken(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(WebServiceConstants.Subscriber, token); client.DefaultRequestHeaders.Add("WebRequestUserID", encodedId); return(await client.DeleteAsync(new Uri(url))); } }
/// <summary> /// Posts HTTP data to the specified endpoint /// </summary> /// <param name="url"></param> /// <param name="content"></param> /// <returns></returns> public async Task <HttpResponseMessage> PostData(string url, HttpContent content) { string token = JsonWebTokenServices.CreateJsonWebtoken(); using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(WebServiceConstants.Subscriber, token); return(await client.PostAsync(new Uri(url), content)); } }
/// <summary> /// Retrieves data from the specified endpoint /// </summary> /// <param name="url"></param> /// <param name="encodedId">The user's base64 encoded user ID. /// N.B. Not needed when initially retrieveing the user's information.</param> /// <returns></returns> public async Task <string> GetData(string url, string encodedId = null) { using (var client = new HttpClient()) { string token = JsonWebTokenServices.CreateJsonWebtoken(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(WebServiceConstants.Subscriber, token); if (!string.IsNullOrEmpty(encodedId)) { client.DefaultRequestHeaders.Add("WebRequestUserID", encodedId); } using (var r = await client.GetAsync(new Uri(url))) { string result = await r.Content.ReadAsStringAsync(); return(result); } } }
/// <summary> /// Creates a JSON web token - used for unit testing by allowing us to inject a Datetime into the method. /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string CreateJsonWebtoken(DateTime dt) { return JsonWebTokenServices.CreateToken(dt); }
/// <summary> /// Creates a JSON web token /// </summary> /// <returns></returns> public static string CreateJsonWebtoken() { return JsonWebTokenServices.CreateToken(DateTime.UtcNow); }