Example #1
0
        private async Task <T> ExecuteRequest <T>(string token, string contractPath, object requestData)
        {
            if (string.IsNullOrEmpty(tccBaseUrl))
            {
                throw new Exception("Configuration Error - no TCC url specified");
            }

            var gracefulClient = new GracefulWebRequest(logFactory, configStore, _httpClientFactory);

            var(requestString, headers) = FormRequest(requestData, contractPath, token);

            headers.Add("Content-Type", ContentTypeConstants.ApplicationJson);
            var result = default(T);

            try
            {
                result = await gracefulClient.ExecuteRequest <T>(requestString, method : HttpMethod.Get, customHeaders : headers);
            }
            catch (WebException webException)
            {
                using (var response = webException.Response)
                {
                    Log.LogWarning(
                        $"Can not execute request TCC request with error {webException.Status} and {webException.Message}. {GetStringFromResponseStream(response)}");
                }
            }
            catch (Exception e)
            {
                Log.LogWarning(e, "Can not execute request TCC response.");
            }
            return(result);
        }
Example #2
0
        private async Task <T> ExecuteRequestWithAllowedError <T>(string token, string contractPath, object requestData)
            where T : ApiResult, new()
        {
            const string FILE_DOES_NOT_EXIST_ERROR =
                "{\"errorid\":\"FILE_DOES_NOT_EXIST\",\"message\":\"File does not exist\",\"success\":false}";

            if (string.IsNullOrEmpty(tccBaseUrl))
            {
                throw new Exception("Configuration Error - no TCC url specified");
            }

            var gracefulClient = new GracefulWebRequest(logFactory, configStore, _httpClientFactory);

            var(requestString, headers) = FormRequest(requestData, contractPath, token);

            headers.Add("Content-Type", ContentTypeConstants.ApplicationJson);
            var result = default(T);

            try
            {
                result = await gracefulClient.ExecuteRequest <T>(requestString, method : HttpMethod.Get, customHeaders : headers, retries : 0, suppressExceptionLogging : true);
            }
            catch (WebException webException)
            {
                using (var response = webException.Response)
                {
                    var tccError = GetStringFromResponseStream(response);

                    if (tccError == FILE_DOES_NOT_EXIST_ERROR)
                    {
                        var tccErrorResult = JsonConvert.DeserializeObject <ApiResult>(FILE_DOES_NOT_EXIST_ERROR);

                        result = new T
                        {
                            success = tccErrorResult.success,
                            errorid = tccErrorResult.errorid,
                            message = tccErrorResult.message
                        };
                    }
                    else
                    {
                        Log.LogWarning(
                            $"Can not execute request TCC request with error {webException.Status} and {webException.Message}. {tccError}");
                    }
                }
            }
            catch (Exception e)
            {
                Log.LogWarning(e, "Can not execute request TCC response");
            }

            return(result);
        }
Example #3
0
        public async Task <PutFileResponse> PutFileEx(string filespaceId, string path, string filename, Stream contents,
                                                      long sizeOfContents)
        {
            Log.LogDebug("PutFileEx: filespaceId={0}, fullName={1} {2}", filespaceId, path, filename);

            //NOTE: for this to work in TCC the path must exist otherwise TCC either gives an error or creates the file as the folder name
            var sendFileParams = new PutFileRequest
            {
                filespaceid  = filespaceId,
                path         = path,
                replace      = true,
                commitUpload = true,
                filename     = filename
            };

            if (string.IsNullOrEmpty(tccBaseUrl))
            {
                throw new Exception("Configuration Error - no TCC url specified");
            }

            var gracefulClient = new GracefulWebRequest(logFactory, configStore, _httpClientFactory);

            var(requestString, headers) = FormRequest(sendFileParams, "PutFile");

            headers.Add("X-File-Name", WebUtility.UrlEncode(filename));
            headers.Add("X-File-Size", sizeOfContents.ToString());
            headers.Add("X-FileType", "");
            headers.Add("Content-Type", ContentTypeConstants.ApplicationOctetStream);

            var result = default(PutFileResponse);

            try
            {
                result = await gracefulClient.ExecuteRequest <PutFileResponse>(requestString, contents, headers, HttpMethod.Put);
            }
            catch (WebException webException)
            {
                using (var response = webException.Response)
                {
                    Log.LogWarning(
                        $"Can not execute request TCC request with error {webException.Status} and {webException.Message}. {GetStringFromResponseStream(response)}");
                }
            }
            catch (Exception exception)
            {
                Log.LogWarning($"TCC request failed: {exception.Message}");
            }

            return(result);
        }