1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- using SinochemCloudClient.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Text;
- using System.Threading.Tasks;
- namespace SinochemCloudClient
- {
- public class CloudCommunicator<TRequest, TResponse>
- where TRequest : RequestBase
- where TResponse : ResponseBase
- {
- private HttpClient client;
- private string url;
- public CloudCommunicator(string apiUrl)
- {
- client = new HttpClient();
- client.DefaultRequestHeaders.Accept.Clear();
- client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
- this.url = apiUrl;
- }
- public void SendRequest(TRequest request)
- {
- try
- {
- HttpResponseMessage httpResponse = client.PostAsJsonAsync<TRequest>(url, request).Result;
- httpResponse.EnsureSuccessStatusCode();
- Response = httpResponse.Content.ReadAsAsync<TResponse>().Result;
- }
- catch (Exception ex)
- {
- Response = null;
- Console.WriteLine(ex);
- }
- }
- public TResponse Response
- {
- get;
- private set;
- }
- }
- }
|