示例#1
0
        public async Task <HttpContent> SendRequest(HttpRequestMessage httpRequestMessage, string userToken)
        {
            // generate token
            string token = !String.IsNullOrEmpty(userToken) ? userToken : Token.GenerateJSONWebToken(accessKey, secretKey, expiryInSeconds);

            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
                httpClient.DefaultRequestHeaders.Add("User-Agent", ".Net OneBlink.SDK / " + GetType().Assembly.GetName().Version.ToString());

                HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage);

                if (!httpResponseMessage.IsSuccessStatusCode)
                {
                    string result = await httpResponseMessage.Content.ReadAsStringAsync();

                    if (httpResponseMessage.Content.Headers.ContentType.MediaType == "application/json")
                    {
                        APIErrorResponse apiErrorResponse = JsonConvert.DeserializeObject <APIErrorResponse>(result);
                        throw new OneBlinkAPIException(apiErrorResponse);
                    }
                    throw new OneBlinkAPIException(httpResponseMessage.StatusCode, result);
                }
                return(httpResponseMessage.Content);
            }
        }
示例#2
0
 internal OneBlinkAPIException(APIErrorResponse apiErrorResponse)
     : base(apiErrorResponse.message)
 {
     this.StatusCode = apiErrorResponse.statusCode;
 }