private async Task<Entity> ProcessResponse(HttpResponseMessage response)
{
if ((int)response.StatusCode == 204 || response.Content.Headers.ContentLength == 0)
{
return null;
}
Entity entity = null;
MediaTypeHeaderValue contentType = response.Content.Headers.ContentType;
bool isText = contentType.MediaType.Contains("text/");
bool isJson = contentType.MediaType == ContentType.JSON || contentType.MediaType == ContentType.NXENTITY;
bool isMultipart = response.Content.IsMimeMultipartContent();
if ((int)response.StatusCode >= 400 && (int)response.StatusCode <= 499)
{
throw new ClientErrorException(response.StatusCode,
isText || isJson ? await response.Content.ReadAsStringAsync() : string.Empty);
}
if ((int)response.StatusCode >= 500 && (int)response.StatusCode <= 599)
{
throw new ServerErrorException(response.StatusCode,
isText || isJson ? await response.Content.ReadAsStringAsync() : string.Empty);
}
if (isText || isJson)
{
string result = await response.Content.ReadAsStringAsync();
if (isJson)
{
entity = Marshaller.UnMarshal(JToken.Parse(result));
}
else
{
FileInfo tmpFile = IOHelper.CreateTempFile(result);
entity = new Blob(response.Content.Headers.ContentDisposition.FileNameStar ??
response.Content.Headers.ContentDisposition.FileName).SetFile(tmpFile);
}
}
else if (response.Content.IsMimeMultipartContent())
{
MultipartMemoryStreamProvider mp = await response.Content.ReadAsMultipartAsync();
BlobList blobs = new BlobList();
foreach (HttpContent part in mp.Contents)
{
blobs.Add(new Blob(IOHelper.CreateTempFile(await part.ReadAsStreamAsync())));
}
entity = blobs;
}
else
{
entity = new Blob(IOHelper.CreateTempFile(await response.Content.ReadAsStreamAsync()));
}
return entity;
}
}