public async Task <MessengerResponse <TResponse> > Post <TResponse, TRequest>(string url, TRequest data) { var content = new StringContent(serializer.Serialize(data)); content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var response = await client.PostAsync(url, content); var result = new MessengerResponse <TResponse>(); result.RawResponse = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { result.Succeed = false; return(result); } if (typeof(TResponse) == typeof(string)) { object raw = result.RawResponse; result.Result = (TResponse)raw; } else { result.Result = serializer.Deserialize <TResponse>(result.RawResponse); } return(result); }
protected async Task <MessengerResponse <T> > Get <T>(string url) { var httpResponse = await client.GetAsync(url); var result = new MessengerResponse <T>(); result.Succeed = httpResponse.IsSuccessStatusCode; result.RawResponse = await httpResponse.Content.ReadAsStringAsync(); if (result.Succeed) { result.Result = serializer.Deserialize <T>(result.RawResponse); } return(result); }
public async Task <MessengerResponse <MessengerUserProfile> > GetUserProfileAsync(string userId, string accessToken = null) { var token = accessToken ?? MessengerConfig.AccessToken; var url = String.Format(UrlTemplate, userId, token); var httpResponse = await client.GetAsync(url); var response = new MessengerResponse <MessengerUserProfile>(); response.Succeed = httpResponse.IsSuccessStatusCode; var content = await httpResponse.Content.ReadAsStringAsync(); response.RawResponse = content; if (httpResponse.IsSuccessStatusCode) { response.Result = serializer.Deserialize <MessengerUserProfile>(content); } return(response); }