Handling responses - mattiasnordqvist/Web-Anchor GitHub Wiki
You can declare the return type of your api methods in four different ways.
The content of the HttpResponseMessage will be deserialized into YourObject. Throws ApiException on all ResponseMessages that are not successful.
Return the responsemessage as it is. Does not throw exceptions on non-success status codes.
Drops the responsemessage and just returns the Task for when you just don't care about the response. Throws ApiException on all ResponseMessages that are not successful.
Returns a stream of the content of the response. Throws ApiException on all ResponseMessages that are not successful. NB! Don't forget to dispose the stream! Otherwise the connection will be left hanging which is basically gonna stop you from doing more than 2 requests before things starts hanging. (https://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.defaultconnectionlimit(v=vs.110).aspx) Example usage:
public interface IFileApi
{
[Get("wikipedia/commons/2/2c/A_new_map_of_Great_Britain_according_to_the_newest_and_most_exact_observations_%288342715024%29.jpg")]
Task<Stream> GetFile();
}
var api = Api.For<IFileApi>("https://upload.wikimedia.org");
using(var file = await api.GetFile())
{
var fileInfo = File.Create("GreatBritain.jpg");
await file.CopyToAsync(fileInfo);
}