Ejemplo n.º 1
0
        /// <summary>
        /// Uploads a given piece of code to the service, and returns the URL to the post.
        /// </summary>
        /// <param name="code">The code to post</param>
        /// <returns>The URL to the newly created post</returns>
        public async Task <string> UploadCodeAsync(string code, string language = null)
        {
            var usingFallback = false;
            var content       = FormatUtilities.BuildContent(code);
            HttpResponseMessage response;

            try
            {
                response = await _client.PostAsync($"{ApiReferenceUrl}documents", content);
            }
            catch (TaskCanceledException)
            {
                usingFallback = true;
                response      = await _client.PostAsync($"{FallbackApiReferenceUrl}documents", content);
            }

            if (!response.IsSuccessStatusCode)
            {
                var body = await response.Content?.ReadAsStringAsync();

                throw new Exception($"{response.StatusCode} returned when calling {response.RequestMessage.RequestUri}. Response body: {body}");
            }

            var urlResponse = await response.Content.ReadAsStringAsync();

            var pasteKey = JObject.Parse(urlResponse)["key"].Value <string>();

            var domain = usingFallback ? FallbackApiReferenceUrl : ApiReferenceUrl;

            return($"{domain}{pasteKey}.{language ?? (FormatUtilities.GetCodeLanguage(code) ?? "cs")}");
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Uploads a given piece of code to the service, and returns the URL to the post.
        /// </summary>
        /// <param name="code">The code to post</param>
        /// <returns>The URL to the newly created post</returns>
        public async Task <string> UploadCode(string code, string language = null)
        {
            var usingFallback = false;
            var content       = FormatUtilities.BuildContent(code);
            HttpResponseMessage response;

            try
            {
                response = await client.PostAsync($"{_ApiReferenceUrl}documents", content);
            }
            catch (TaskCanceledException)
            {
                usingFallback = true;
                response      = await client.PostAsync($"{_FallbackApiReferenceUrl}documents", content);
            }

            if (!response.IsSuccessStatusCode)
            {
                throw new WebException("Something failed while posting code to Hastebin.");
            }

            var urlResponse = await response.Content.ReadAsStringAsync();

            var pasteKey = JObject.Parse(urlResponse)["key"].Value <string>();

            var domain = usingFallback ? _FallbackApiReferenceUrl : _ApiReferenceUrl;

            return($"{domain}{pasteKey}.{language ?? (FormatUtilities.GetCodeLanguage(code) ?? "cs")}");
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Uploads a given piece of code to the service, and returns the URL to the post.
        /// </summary>
        /// <param name="code">The code to post</param>
        /// <returns>The URL to the newly created post</returns>
        public async Task <string> UploadCode(string code, string language = null)
        {
            var response = await client.PostAsync($"{_ApiReferenceUrl}documents", FormatUtilities.BuildContent(code));

            if (!response.IsSuccessStatusCode)
            {
                throw new WebException("Something failed while posting code to Hastebin.");
            }

            var urlResponse = await response.Content.ReadAsStringAsync();

            var pasteKey = JObject.Parse(urlResponse)["key"].Value <string>();

            return($"{_ApiReferenceUrl}{pasteKey}.{language ?? (FormatUtilities.GetCodeLanguage(code) ?? "cs")}");
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Uploads a given piece of code to the service, and returns the URL to the post.
        /// </summary>
        /// <param name="code">The code to post</param>
        /// <returns>The URL to the newly created post</returns>
        public async Task <string> UploadCodeAsync(string code, string language = null)
        {
            var content = FormatUtilities.BuildContent(code);

            var client = _httpClientFactory.CreateClient(HttpClientNames.TimeoutFiveSeconds);

            var response = await client.PostAsync($"{ApiReferenceUrl}documents", content);

            if (!response.IsSuccessStatusCode)
            {
                var body = await response.Content?.ReadAsStringAsync();

                throw new Exception($"{response.StatusCode} returned when calling {response.RequestMessage?.RequestUri}. Response body: {body}");
            }

            var urlResponse = await response.Content.ReadAsStringAsync();

            var pasteKey = JObject.Parse(urlResponse)["key"]?.Value <string>();

            return($"{ApiReferenceUrl}{pasteKey}.{language ?? FormatUtilities.GetCodeLanguage(code) ?? "cs"}");
        }