/// <summary> /// this method prepares a http request and get the response for the request url. /// </summary> /// <typeparam name="TResult"></typeparam> /// <param name="httpMethod"></param> /// <param name="requestUrl"></param> /// <param name="accept"></param> /// <param name="content"></param> /// <param name="contentType"></param> /// <param name="resultPredicate"></param> /// <returns></returns> private TResult MakeHttpRequest <TResult>(string httpMethod, string requestUrl, string accept = null, object content = null, string contentType = null, Func <HttpResponseMessage, TResult> resultPredicate = null) { //Prepare the variable to hold the resukt if any TResult result = default(TResult); //Get the access token of the current login user string accessToken = spoAuthorization.GetGraphAccessToken(); if (!string.IsNullOrEmpty(accessToken)) { HttpClient httpClient = new HttpClient(); //Set the authorization bearer token httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); //If there is an accept argument, set the corresponding accept header if (!string.IsNullOrEmpty(accept)) { httpClient.DefaultRequestHeaders.Accept.Clear(); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(accept)); } Uri requestUri = new Uri(requestUrl); //Prepare the content of the request if any HttpContent requestContent = (content != null) ? new StringContent(JsonConvert.SerializeObject(content, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, ContractResolver = new CamelCasePropertyNamesContractResolver() }), Encoding.UTF8, contentType) : null; //Prepare http request message with proper http method HttpRequestMessage request = new HttpRequestMessage(new HttpMethod(httpMethod), requestUrl); //Set the request content if any if (requestContent != null) { request.Content = requestContent; } //Fire the http request HttpResponseMessage response = httpClient.SendAsync(request).Result; if (response.IsSuccessStatusCode) { if (resultPredicate != null) { result = resultPredicate(response); } } else { throw new ApplicationException( $"Exception occured while invoking the endpoint {requestUrl}", new HttpRequestException(response.Content.ReadAsStringAsync().Result)); } } return(result); }
/// <summary> /// This method will get user profile picture of the login user /// </summary> /// <param name="client"></param> /// <returns></returns> public async Task <Users> GetUserProfilePicture(Client client) { Users users = new Users(); try { string accessToken = spoAuthorization.GetGraphAccessToken(); using (var httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); var url = $"{generalSettings.GraphUrl}/v1.0/me/photo/$value"; var resultAsString = await httpClient.GetStreamAsync(url); using (var newStream = new MemoryStream()) { resultAsString.CopyTo(newStream); byte[] bytes = newStream.ToArray(); users.LargePictureUrl = ServiceConstants.BASE64_IMAGE_FORMAT + Convert.ToBase64String(bytes); } return(users); } } catch (Exception ex) when(ex.Message.Contains("404")) { users.LargePictureUrl = ""; return(users); } catch (Exception ex) { throw; } }