Skip to content

Instantly share code, notes, and snippets.

@Delaire
Last active August 3, 2018 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Delaire/2dbacafad7fd59d9db148a718067a0a4 to your computer and use it in GitHub Desktop.
Save Delaire/2dbacafad7fd59d9db148a718067a0a4 to your computer and use it in GitHub Desktop.
DataService.cs
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