Skip to content

Instantly share code, notes, and snippets.

@jzeferino
Last active February 28, 2018 22:14
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 jzeferino/c990ab0e22b3f015f6a65ab5d330001d to your computer and use it in GitHub Desktop.
Save jzeferino/c990ab0e22b3f015f6a65ab5d330001d to your computer and use it in GitHub Desktop.
Retrieves a random photo byte[]
// =============================================
// AUTHOR : jzeferino
// PURPOSE : A simple Xamarin introduction demo
// =============================================
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace SharedCode.Service
{
public class PhotoService
{
private HttpClient _client;
private const string RandomImageUrl = "https://unsplash.it/540/960/?random";
public PhotoService()
{
_client = new HttpClient();
}
private async Task<byte[]> LoadImageAsync(string imageUrl)
{
try
{
var bytes = await _client.GetByteArrayAsync(imageUrl);
if (bytes == null || bytes.Length == 0)
{
throw new Exception("No content.");
}
return bytes;
}
catch (Exception)
{
// TODO:
// Deal with the error.
}
return null;
}
public Task<byte[]> LoadImageAsync() => LoadImageAsync(RandomImageUrl);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment