Last active
February 28, 2018 22:14
-
-
Save jzeferino/c990ab0e22b3f015f6a65ab5d330001d to your computer and use it in GitHub Desktop.
Retrieves a random photo byte[]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ============================================= | |
// 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