selling-partner-api
selling-partner-api copied to clipboard
[Feeds API] support for custom charset in document upload
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 ?
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);
}
}
thanks for flagging this -- could you open a pr with your changes?
ok thx !
I think it's not the same as #340
I was waiting after #348 to propose a PR for this one
my bad, i'll reopen
up :)
closed by #431.