Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Когда я хочу скачать документ, он выдает ошибку #34

Closed
nicolae-soitu opened this issue Oct 4, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@nicolae-soitu
Copy link

Когда я хочу скачать документ, он выдает ошибку InvalidArgumentException

$ms->query()
    ->endpoint('entity')
    ->method('customerorder')
    ->byId($orderID)
    ->method('export')
    ->send('POST',
    [
      "template" =>  [
        "meta" =>  [
          "href"=> "https://api.moysklad.ru/api/remap/1.2/entity/customerorder/metadata/customtemplate/45f9e4cd-ecb8-4e26-9876-343ee4c8afe7",
          "type"=> "customtemplate",
          "mediaType" => "application/json"
        ]
      ],
      "extension" => "xls"
    ])

Evgeek\Moysklad\Formatters\StdClassFormat.php

  try {
            $encodedContent = json_decode($content, false, 512, JSON_THROW_ON_ERROR);
        } catch (Throwable) {
            $this->throwContentIsNotValidJsonObject($content);
        }

на самом деле $content — это содержимое файла,
но здесь он просит JSON

Есть ли другой способ скачать документы?

@nicolae-soitu
Copy link
Author

Сейчас я сделал такои костыли, но я бы хотел, решение по лучшее

Evgeek\Moysklad\Formatters\StdClassFormat.php

try {
            $encodedContent = json_decode($content, false, 512, JSON_THROW_ON_ERROR);
        } catch (Throwable) {
            header("Content-type:application/pdf");
            flush();
            exit($content);
            $this->throwContentIsNotValidJsonObject($content);
        }

@evgeek
Copy link
Owner

evgeek commented Oct 4, 2023

Здравствуйте! И правда, вариант с файлами я не предусмотрел.
Релизнул версию v0.9.1. В ней содержимое файла можно получить при помощи Evgeek\Moysklad\Formatters\StringFormat. Для этого потребуется инстанцировать дополнительный объект MoySklad, как в примере ниже. Возможно, позже придумаю более изящное решение, но это не точно :)

use Evgeek\Moysklad\MoySklad;
use Evgeek\Moysklad\Formatters\StringFormat;

$msForFiles = new MoySklad(['token'], new StringFormat());

$rawFileContent = $msForFiles->query()
    ->endpoint('entity')
    ->method('customerorder')
    ->byId('23a96bf9-62a9-11ee-0a80-07360018422f')
    ->method('export')
    ->send('POST',
        [
            'template' => [
                'meta' => [
                    'href' => 'https://api.moysklad.ru/api/remap/1.2/entity/customerorder/metadata/customtemplate/f30fcb2b-2b9e-4d21-be4a-7063205af0cc',
                    'type' => 'customtemplate',
                    'mediaType' => 'application/json'
                ]
            ],
            'extension' => 'xls'
        ]);

var_dump($rawFileContent);

@evgeek evgeek added the bug Something isn't working label Oct 4, 2023
@nicolae-soitu
Copy link
Author

nicolae-soitu commented Oct 4, 2023

Спасибо за вашу отзывчивость

Возможно, в send() указать другой параметр
или сделать -> formatString()

$ms -> query()
    ->endpoint('entity')
    ->method('customerorder')
    ->byId('23a96bf9-62a9-11ee-0a80-07360018422f')
    ->method('export')
   -> formatString() или -> format('String') или  -> returnString() 
    ->send('POST', .....)

@evgeek
Copy link
Owner

evgeek commented Oct 4, 2023

Или дополнительный CRUD-метод специально для файлов сделать, ага. Если вообще красиво - изучить все эндпойнты, из которых можно скачать файл, вывести какой-то общий шаблон, по нему в пейлоаде определять, что в ответе должен быть именно файл, и возвращать строку без валидации из любого форматтера. Варианты есть, надо подумать - но пока это не в приоритете, сначала хотелось бы все сущности Моего Склада добавить

@evgeek evgeek closed this as completed Oct 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants