Exemplo n.º 1
0
        public async Task <IActionResult> GetAsync(string manufacturerGLN, string productCode)
        {
            var authenticationData = new AuthenticationManager().GetAccessToken();

            if (authenticationData == null || string.IsNullOrEmpty(authenticationData.AccessToken))
            {
                return(Unauthorized());
            }

            using var client = WebClientManager.GetWebHttpClient("Data", authenticationData.AccessToken);
            try
            {
                var url = HttpUtility.UrlEncode($"productdata/{manufacturerGLN}/{productCode}");

                using var response = await client.GetAsync(url).ConfigureAwait(false);

                var contentString = string.Empty;
                if (response.IsSuccessStatusCode)
                {
                    contentString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                    var result = JsonConvert.DeserializeObject <CADProduct>(contentString);

                    return(new JsonResult(result));
                }
                else if (response.StatusCode == HttpStatusCode.NotFound)
                {
                    return(default);
        private async Task <IActionResult> GetTemplates(string application, string applicationVersion, List <CADProduct> cadProducts)
        {
            var authenticationData = new AuthenticationManager().GetAccessToken();

            if (authenticationData == null || string.IsNullOrEmpty(authenticationData.AccessToken))
            {
                return(Unauthorized());
            }

            using var client = WebClientManager.GetWebHttpClient("Template", authenticationData.AccessToken);
            try
            {
                var url     = $"export/{application}/{applicationVersion}";
                var content = new StringContent(JsonConvert.SerializeObject(cadProducts), Encoding.UTF8, "application/json");

                using var response = await client.PostAsync(url, content).ConfigureAwait(false);

                var contentString = string.Empty;
                if (response.IsSuccessStatusCode)
                {
                    using var contentStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

                    // When the HttpClient is disposed, the contentStream is no longer readable, seekable, etc. To
                    // fix this, we clone the contentStream into a copy that is returned instead of the contentStream.
                    var contentStreamClone = new MemoryStream();

                    await contentStream.CopyToAsync(contentStreamClone).ConfigureAwait(false);

                    contentStreamClone.Seek(0, SeekOrigin.Begin);
                    return(new FileStreamResult(contentStreamClone, "application/octet-stream"));
                }
                else if (response.StatusCode == HttpStatusCode.NotFound)
                {
                    return(default);