Last active
August 3, 2018 12:39
-
-
Save Delaire/2dbacafad7fd59d9db148a718067a0a4 to your computer and use it in GitHub Desktop.
DataService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DataService | |
{ | |
public async Task<string> MakeRequest(RequestBase req) | |
{ | |
if (req == null) | |
{ | |
throw new ArgumentNullException("req"); | |
} | |
//creating http message | |
HttpRequestMessage message = PrepareMessageGraphQL(req); | |
//creating http response | |
HttpResponseMessage response = await HttpClient.SendAsync(message); | |
//if empty return error | |
if (response == null) | |
{ | |
throw new Exception("The server did not return a response."); | |
} | |
if (response.IsSuccessStatusCode) | |
{ | |
return await response.Content.ReadAsStringAsync(); | |
} | |
//if status code is not success | |
else | |
{ | |
throw new Exception($"Server returned status code {response.StatusCode}."); | |
} | |
} | |
private async Task<HttpRequestMessage> PrepareMessageGraphQL(RequestBase req) | |
{ | |
HttpRequestMessage message = null; | |
//Creating a POST Request Message | |
message = new HttpRequestMessage(HttpMethod.Post, ApiConstants.GRAPHQL_URL); | |
//Setting the content of the query | |
message.Content = new StringContent(req.ParamsToCall, Encoding.UTF8, "application/json"); | |
//you might want to set some headers? | |
//message.Headers.Add("Authorization", "Bearer WithToken"); | |
return message; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment