示例#1
0
        public async Task <AkeneoResponse> UploadAsync(MediaUpload media, CancellationToken ct = default(CancellationToken))
        {
            if (!File.Exists(media.FilePath))
            {
                throw new FileNotFoundException($"File with path {media.FilePath} not found.");
            }
            var filename    = media.FileName ?? Path.GetFileName(media.FilePath);
            var formContent = new MultipartFormDataContent
            {
                { new JsonContent(media.Product, AkeneoSerializerSettings.Update), "product" },
                { new StreamContent(File.OpenRead(media.FilePath)), "file", filename }
            };

            _logger.Debug($"Preparing to upload image '{filename}' from '{media.FilePath}'.");
            var response = await HttpClient.PostAsync(Endpoints.MediaFiles, formContent, ct);

            if (response.StatusCode == HttpStatusCode.Unauthorized)
            {
                await AddAuthHeaderAsync(ct);

                response = await HttpClient.PostAsync(Endpoints.MediaFiles, new MultipartFormDataContent
                {
                    { new JsonContent(media.Product, AkeneoSerializerSettings.Update), "product" },
                    { new StreamContent(File.OpenRead(media.FilePath)), "file", filename }
                }, ct);
            }
            return(response.IsSuccessStatusCode
                                ? AkeneoResponse.Success(response.StatusCode, new KeyValuePair <string, PaginationLink>(PaginationLinks.Location, new PaginationLink {
                Href = response.Headers?.Location?.ToString()
            }))
                                : await response.Content.ReadAsJsonAsync <AkeneoResponse>());
        }
示例#2
0
        public async Task <AkeneoResponse> DeleteAsync <TModel>(string code, CancellationToken ct = default(CancellationToken)) where TModel : ModelBase
        {
            var endpoint = _endpointResolver.ForResource <TModel>(code);

            _logger.Debug($"Deleting resource '{typeof(TModel).Name}' from URL '{endpoint}'.");
            var response = await DeleteAsync(endpoint, ct);

            return(response.IsSuccessStatusCode
                                ? AkeneoResponse.Success(response.StatusCode)
                                : await response.Content.ReadAsJsonAsync <AkeneoResponse>());
        }
示例#3
0
        public async Task <AkeneoResponse> UpdateAsync <TModel>(string identifier, object model, CancellationToken ct = default(CancellationToken)) where TModel : ModelBase
        {
            var endpoint = $"{_endpointResolver.ForResourceType<TModel>()}/{identifier}";

            _logger.Debug($"Updating resource '{typeof(TModel).Name}' from URL '{endpoint}'.");
            var response = await PatchAsJsonAsync(endpoint, model, AkeneoSerializerSettings.Update, ct);

            return(response.IsSuccessStatusCode
                                ? AkeneoResponse.Success(response.StatusCode, new KeyValuePair <string, PaginationLink>(PaginationLinks.Location, new PaginationLink {
                Href = response.Headers?.Location?.ToString()
            }))
                                : await response.Content.ReadAsJsonAsync <AkeneoResponse>());
        }
示例#4
0
        public async Task <AkeneoResponse> CreateAsync <TModel>(TModel model, CancellationToken ct = default(CancellationToken)) where TModel : ModelBase
        {
            var option   = model as AttributeOption;
            var endpoint = _endpointResolver.ForResourceType <TModel>(option?.Attribute ?? string.Empty);

            _logger.Debug($"Creating resource '{typeof(TModel).Name}' from URL '{endpoint}'.");
            var response = await PostAsync(endpoint, model, AkeneoSerializerSettings.Create, ct);

            return(response.IsSuccessStatusCode
                                ? AkeneoResponse.Success(response.StatusCode, new KeyValuePair <string, PaginationLink>(PaginationLinks.Location, new PaginationLink {
                Href = response.Headers?.Location?.ToString()
            }))
                                : await response.Content.ReadAsJsonAsync <AkeneoResponse>());
        }
示例#5
0
 public OperationUnsuccessfulException(string message, AkeneoResponse response) : base(message)
 {
     Response = response;
 }