BatchScoring/RestRequest.cs
/// <summary>
/// Generic call to an endpoint. 
/// </summary>
/// <param name="requestInfo">Request method and URL</param>
/// <param name="authToken">AAD Token for SP</param>
/// <param name="payload">Optional payload parameter</param>
/// <param name="payloadType">Optional payload type parameter, i.e. "application/json"</param>
/// <returns></returns>
Generic call to an endpoint.
Generic call to an endpoint .
private static async Task<RestResponse> MakeRequest(PipelineRequest requestInfo, string authToken, string payload = null, string payloadType = null)
        {
            RestResponse returnResponse = null;
            using (HttpClient httpClient = new HttpClient())
            {
                String bearer = String.Format("Bearer {0}", authToken);
                httpClient.DefaultRequestHeaders.Add("Authorization", bearer);
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                httpClient.BaseAddress = new Uri(requestInfo.Url);
                var httpRequest = new HttpRequestMessage(requestInfo.Method, string.Empty);
                if (requestInfo.Method == HttpMethod.Post)
                {
                    httpRequest.Content = new StringContent(payload);
                    httpRequest.Content.Headers.ContentType = new MediaTypeHeaderValue(payloadType);
                }
                HttpResponseMessage res = await httpClient.SendAsync(httpRequest);
                returnResponse = new RestResponse()
                {
                    StatusCode = (int)res.StatusCode
                };
                if (res.IsSuccessStatusCode)
                {
                    HttpContent content = res.Content;
                    returnResponse.Content = await content.ReadAsStringAsync().ConfigureAwait(false);
                }
            }
            return returnResponse;
        }
private static async Task < RestResponse > MakeRequest ( PipelineRequest requestInfo , string authToken , string payload = null , string payloadType = null ) { RestResponse returnResponse = null ; using ( HttpClient httpClient = new HttpClient ( ) ) { String bearer = String . Format ( " " , authToken ) ; httpClient . DefaultRequestHeaders . Add ( " " , bearer ) ; httpClient . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( " " ) ) ; httpClient . BaseAddress = new Uri ( requestInfo . Url ) ; var httpRequest = new HttpRequestMessage ( requestInfo . Method , string . Empty ) ; if ( requestInfo . Method == HttpMethod . Post ) { httpRequest . Content = new StringContent ( payload ) ; httpRequest . Content . Headers . ContentType = new MediaTypeHeaderValue ( payloadType ) ; } HttpResponseMessage res = await httpClient . SendAsync ( httpRequest ) ; returnResponse = new RestResponse ( ) { StatusCode = ( int ) res . StatusCode } ; if ( res . IsSuccessStatusCode ) { HttpContent content = res . Content ; returnResponse . Content = await content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ; } } return returnResponse ; }
Generic call to an endpoint.
Generic call to an endpoint .
None
requestInfo
PipelineRequest
Request method and URL
Request method and URL
authToken
string
AAD Token for SP
AAD Token for SP
payload
string
Optional payload parameter
Optional payload parameter
payloadType
string
Optional payload type parameter, i.e. "application/json"
Optional payload type parameter i . e . " application / json "