Skip to content

Instantly share code, notes, and snippets.

@AlexArchive
Last active August 29, 2015 13:59
Show Gist options
  • Select an option

  • Save AlexArchive/10567095 to your computer and use it in GitHub Desktop.

Select an option

Save AlexArchive/10567095 to your computer and use it in GitHub Desktop.
public class ImgurService : IDisposable
{
private HttpClient _client;
public string ClientID { get; private set; }
public ImgurService(string clientId)
{
ClientID = clientId;
InitializeHttpClient();
}
public void InitializeHttpClient()
{
_client = new HttpClient();
_client.BaseAddress = new Uri("https://api.imgur.com/3/");
_client.DefaultRequestHeaders.Add(
"Authorization", "Client-ID " + ClientID);
}
public async Task<string> UploadImageAsync(byte[] imageData)
{
var content = new ByteArrayContent(imageData);
var response = await _client.PostAsync("image", content);
response.EnsureSuccessStatusCode();
var responseContent = await response.Content.ReadAsStringAsync();
var model = new JavaScriptSerializer().Deserialize<dynamic>(responseContent);
var imageLink = model["data"]["link"];
return imageLink;
}
public void Dispose()
{
_client.Dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment