示例#1
0
        private async Task <FilesUploadResponse> UploadFileOverWebApi(File file, ILogger logger)
        {
            string postUrl        = $"https://slack.com/api/files.upload?token={this.slackToken}";
            var    requestContent = file.ToKeyValuePairs();

            HttpResponseMessage response;

            try
            {
                response = await HttpClient.PostAsync(postUrl, new FormUrlEncodedContent(requestContent));
            }
            catch (Exception ex)
            {
                logger.LogError($"Failed to POST file upload to Slack. Exception: {ex.Message}");
                throw;
            }

            if (!response.IsSuccessStatusCode)
            {
                logger.LogError($"Non-success response was returned when POSTing a file upload to Slack. Response: {response.StatusCode}; {response.ReasonPhrase}");
            }

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

            var  parsedContent = JObject.Parse(content);
            bool isOk          = parsedContent["ok"].Value <bool>();

            if (!isOk)
            {
                logger.LogError($"Slack response indicated things were not OK with the file upload. Here's the content: {content}");
            }

            if (!response.IsSuccessStatusCode && !isOk)
            {
                throw new HttpRequestException(content);
            }

            return(JsonConvert.DeserializeObject <FilesUploadResponse>(content));
        }
示例#2
0
 public Task UploadFileAsync(File file, ILogger logger)
 => this.UploadFileOverWebApi(file, logger);