Last active
August 29, 2015 13:59
-
-
Save AlexArchive/10567095 to your computer and use it in GitHub Desktop.
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
| 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