jolicode / slack-php-api

:hash: PHP Slack Client based on the official OpenAPI specification
https://jolicode.github.io/slack-php-api/
MIT License
222 stars 56 forks source link

Fix File Upload on Slack API with Jane #62

Closed damienalexandre closed 4 years ago

damienalexandre commented 4 years ago

This is not working! :warning:

Trying to solve / understand issue #58;

I tried to make it works for hours :cold_sweat:, there is two issues:

Here is what I tried. In the specification I changed the type to "file":

--- i/resources/slack-openapi-patched.json
+++ w/resources/slack-openapi-patched.json
@@ -12578,7 +12578,7 @@
         "/files.upload": {
             "post": {
                 "consumes": [
-                    "application/x-www-form-urlencoded"
+                    "multipart/form-data"
                 ],
                 "description": "Uploads or creates a file.",
                 "externalDocs": {
@@ -12633,7 +12633,7 @@
                         "description": "File contents via `multipart/form-data`. If omitting this parameter, you must submit `content`.",
                         "in": "formData",
                         "name": "file",
-                        "type": "string"
+                        "type": "file"
                     },
                     {
                         "description": "Provide another message's `ts` value to upload this file as a reply. Never use a reply's `ts` value; use its parent instead.",

I generated the new client vendor/bin/jane-openapi generate --config-file=.jane-openapi.php and then tried this code:

        $client = ClientFactory::create($_SERVER['SLACK_TOKEN']);

        /** @var FilesUploadPostResponse200 $response */
        $response = $client->filesUpload([
            'channels' => $_SERVER['SLACK_TEST_CHANNEL'],
            'title' => 'Uploaded image',
            'filename' => 'test-image.png',
            'filetype' => 'png',
            'file' => Stream::create(fopen(__DIR__.'/test-image.png', 'r')),
        ]);

And got this error:

1) JoliCode\Slack\Tests\WritingTest::testItCanUploadFile TypeError: Argument 1 passed to Jane\OpenApiRuntime\Client\BaseEndpoint::getMultipartBody() must be an instance of Http\Message\StreamFactory or null, instance of Nyholm\Psr7\Factory\Psr17Factory given, called in /home/dalexandre/Dev/slack-php-api/generated/Endpoint/FilesUpload.php on line 50

It looks like Jane is not building the proper body.

@Korbeil @joelwurtz Mates, can I ping you about this? :grimacing: I'm stuck.

damienalexandre commented 4 years ago

Mergé depuis le bar open source de l'AFUP :)