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);
        }
Example #4
0
        private async Task <Stream> GetFileEx(string filespaceId, string fullName, int retries)
        {
            Log.LogDebug($"{nameof(GetFileEx)}: filespaceId={filespaceId}, fullName={fullName}, retries={retries}");

            byte[] file;
            var    cacheable = TCCFile.FileCacheable(fullName);

            if (cacheable)
            {
                Log.LogDebug("Trying to extract from cache {0} with cache size {1}", fullName, fileCache.Count);

                if (fileCache.TryGetValue(fullName, out file))
                {
                    Log.LogDebug("Serving TCC tile request from cache {0}", fullName);

                    if (file.Length == 0)
                    {
                        Log.LogDebug("Serving TCC tile request from cache empty tile");
                        return(null);
                    }

                    return(new MemoryStream(file));
                }
            }

            var getFileParams = new GetFileParams
            {
                filespaceid = filespaceId,
                path        = fullName
            };

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

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

            var(requestString, headers) = FormRequest(getFileParams, "GetFile");

            try
            {
                if (!cacheable)
                {
                    using (var responseStream = await(await gracefulClient.ExecuteRequestAsStreamContent(requestString, HttpMethod.Get, headers, retries: 0)).ReadAsStreamAsync())
                    {
                        responseStream.Position = 0;
                        file = new byte[responseStream.Length];
                        responseStream.Read(file, 0, file.Length);
                        return(new MemoryStream(file));
                    }
                }

                using (var responseStream = await(await gracefulClient.ExecuteRequestAsStreamContent(requestString, HttpMethod.Get, headers, retries: retries)).ReadAsStreamAsync())
                {
                    Log.LogDebug("Adding TCC tile request to cache {0}", fullName);
                    responseStream.Position = 0;
                    file = new byte[responseStream.Length];
                    responseStream.Read(file, 0, file.Length);
                    fileCache.Set(fullName, file, DateTimeOffset.MaxValue);
                    Log.LogDebug("About to extract file name for {0}", fullName);
                    var baseFileName = TCCFile.ExtractFileNameFromTileFullName(fullName);
                    Log.LogDebug("Extracted file name is {0}", baseFileName);
                    cacheLookup.AddFile(baseFileName, fullName);
                    return(new MemoryStream(file));
                }
            }
            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)}");
                }
                //let's cache the response anyway but for a limited time
                fileCache.Set(fullName, new byte[0], DateTimeOffset.UtcNow.AddHours(12));
            }
            catch (Exception e)
            {
                Log.LogWarning(e, "Can not execute request TCC response.");
            }
            return(null);
        }