I'm implementing a service that makes API calls to an external service using HttpClient
.
One of these calls in particular does not always return the same answer that is expected. Pratically:
this is my call:
using (HttpClientHandler handler = new HttpClientHandler { Credentials = new NetworkCredential(key, "") })
using (HttpClient client = new HttpClient(handler))
{
client.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);
using (HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead))
{
using (Stream body = await response.Content.ReadAsStreamAsync())
{
using (StreamReader reader = new StreamReader(body))
{
string read = string.Empty;
while (!reader.EndOfStream)
{
read += reader.ReadLine();
}
return JsonObject.Parse(read);
}
}
}
this is the aspected response body object:
{
"id" : 15,
"key" : "API_KEY",
"status" : "successful",
"sandbox" : true,
"created_at" : "2013-10-27T13:41:00Z",
"finished_at" : "2013-10-27T13:41:13Z",
"source_file" : {"id":2,"name":"testfile.pdf","size":90571},
"target_files" : [{"id":3,"name":"testfile.pptx","size":15311}],
"target_format" : "png",
"credit_cost" : 1
}
where the status
parameter is successful
and the target_files
parameter is an array of objects
.
Sometimes however, the answer basically comes back saying that it hasn't finished converting yet (this API is a files conversion service), with this type of body:
{
"id" : 15,
"key" : "API_KEY",
"status" : "converting",
"sandbox" : true,
"created_at" : "2013-10-27T13:41:00Z",
"finished_at" : "2013-10-27T13:41:13Z",
"source_file" : {"id":2,"name":"testfile.pdf","size":90571},
"target_files" : [{}],
"target_format" : "png",
"credit_cost" : 0
}
where the status
parameter is converting
and the target_files
parameter is empty.
There is a way to manage the call in order to return the object of the response but only when the status
parameter is successfull
? Thanks