selling-partner-api icon indicating copy to clipboard operation
selling-partner-api copied to clipboard

[Feeds API] support for custom charset in document upload

Open quazardous opened this issue 2 years ago • 5 comments

Currently th charset seams to be hard wired to UTF-8

I need to upload CSV in non UTF-8.

how can I do it ?

quazardous avatar Jul 20 '22 16:07 quazardous

I've done a workaround with reflection

<?php

namespace XYZ\Override\SellingPartnerApi;

use GuzzleHttp\RequestOptions;
use Psr\Http\Message\StreamInterface;
use ReflectionProperty;
use RuntimeException;
use SellingPartnerApi\Document as SellingPartnerApiDocument;
use SellingPartnerApi\ReportType;

class Document extends SellingPartnerApiDocument
{
    public function __construct(
        object $documentInfo,
        ?array $documentType = ReportType::__FEED_RESULT_REPORT,  // $documentType will be required in the next major version
        string $charset = null
    ) {
        parent::__construct($documentInfo, $documentType);
        if ($charset) {
            self::set_parent_property($this, 'contentType', $documentType['contentType'] . "; charset={$charset}");
        }
    }

    /**
     * Uploads data to the document specified in the constructor.
     *
     * @param resource|StreamInterface|callable|\Iterator $feedDataStream The contents of the feed to be uploaded
     *
     * @return void
     */
    public function uploadStream($feedDataStream): void
    {

        /** @var \GuzzleHttp\Client $client */
        $client = self::get_parent_property($this, 'client');

        /** @var string $url */
        $url = self::get_parent_property($this, 'url');

        /** @var string $contentType */
        $contentType = self::get_parent_property($this, 'contentType');

        $response = $client->put($url, [
            RequestOptions::HEADERS => [
                "content-type" => $contentType,
                "host" => parse_url($url, PHP_URL_HOST),
            ],
            RequestOptions::BODY => $feedDataStream,
        ]);

        if ($response->getStatusCode() >= 300) {
            throw new RuntimeException("Upload failed ({$response->getStatusCode()}): {$response->getBody()}");
        }
    }

    protected static function get_parent_property(self $self, string $name)
    {
        $contentTypeProperty = new ReflectionProperty(SellingPartnerApiDocument::class, $name);
        $contentTypeProperty->setAccessible(true);
        return $contentTypeProperty->getValue($self);
    }

    protected static function set_parent_property(self $self, string $name, $value)
    {
        $contentTypeProperty = new ReflectionProperty(SellingPartnerApiDocument::class, $name);
        $contentTypeProperty->setAccessible(true);
        $contentTypeProperty->setValue($self, $value);
    }
}

quazardous avatar Jul 21 '22 06:07 quazardous

thanks for flagging this -- could you open a pr with your changes?

jlevers avatar Jul 25 '22 02:07 jlevers

ok thx !

quazardous avatar Jul 25 '22 06:07 quazardous

I think it's not the same as #340

I was waiting after #348 to propose a PR for this one

quazardous avatar Aug 01 '22 15:08 quazardous

my bad, i'll reopen

jlevers avatar Aug 02 '22 15:08 jlevers

up :)

quazardous avatar Jan 13 '23 09:01 quazardous

closed by #431.

jlevers avatar Jan 24 '23 16:01 jlevers