/// <summary> /// Sending HTTP request to the MindSphere API /// </summary> public async Task <string> HttpActionAsync(HttpMethod method, string specUri, HttpContent body = null, List <KeyValuePair <string, string> > headers = null) { // always try to validate / renew token await RenewTokenAsync(); // prepare HTTP request HttpRequestMessage request = new HttpRequestMessage(); request.Method = method; request.RequestUri = GetFullUri(specUri); request.Headers.Add("Authorization", "Bearer " + _accessToken.Token); // headers from parametr if (headers != null) { foreach (var header in headers) { request.Headers.TryAddWithoutValidation(header.Key, header.Value); } } request.Content = body; HttpResponseMessage response = await _httpClient.SendAsync(request); await MindSphereApiExceptionHandler.HandleUnsuccessfulResponseAsync(response); string responseBody = await response.Content.ReadAsStringAsync(); return(responseBody); }
/// <summary> /// Acquire MindSphere access token with app credentials /// </summary> public override async Task AcquireTokenAsync() { // prepare HTTP request HttpRequestMessage request = new HttpRequestMessage(); request.Method = HttpMethod.Post; request.RequestUri = GetFullUri("/api/technicaltokenmanager/v3/oauth/token"); // X-SPACE-AUTH-KEY is needed request.Headers.Add("X-SPACE-AUTH-KEY", GetBasicAuth()); request.Content = new StringContent(JsonConvert.SerializeObject(_credentials), Encoding.UTF8, "application/json"); HttpResponseMessage response = await _httpClient.SendAsync(request); // handle error response await MindSphereApiExceptionHandler.HandleUnsuccessfulResponseAsync(response); string responseBody = await response.Content.ReadAsStringAsync(); _accessToken = JsonConvert.DeserializeObject <AccessToken>(responseBody); }