arangodb / arangodb-php

PHP ODM for ArangoDB
https://www.arangodb.com
Apache License 2.0
183 stars 43 forks source link

Deploy a foxx-app #191

Closed tomterl closed 7 years ago

tomterl commented 8 years ago

I would like to deploy a foxx-app via the php-client; we have a php system that creates graphs in arangodb that are 'executed' with a foxx-app; it would be nice if we could deploy the app during the import-process. Is this possible already? Or a possibilty for the future?

tomterl commented 8 years ago

I found the answer, I guess: It is not implemented, but should be possibly easy to add using /_api/upload and /_admin/foxx/install REsT endpoints.. Hopefully I'll get around to take a shot at the implementation myself, but that will be some time

tomterl commented 7 years ago

I have it implemented, but for the 2.8 Branch, as \triagens\ArangoDb\FoxxHandler.php with the following sole (for now) Method:

    /**
     * Upload and install a foxx app.
     *
     * @throws Exception
     *
     * @param string $localZip          - the path to the local foxx-app zip-archive to upload/install
     * @param string mountPoint         - the mountpoint for the app, must begin with a '/'
     * @param array $options            - for future usage
     * @return array - the server response
     */
    public function installFoxxZip($localZip, $mountPoint, $options = array())
    {
        if (!file_exists($localZip)) {
            throw new ClientException("Foxx-Zip {$localZip} does not exist (or file is unreadable).");
        }

        $post = file_get_contents($localZip);
        $response = $this->getConnection()->post(Urls::URL_UPLOAD, $post);

        if ($response->getHttpCode() < 400) {
            $response = $this->getConnection()->put(Urls::URL_FOXX_INSTALL, json_encode(array('appInfo' => $response->getJson()['filename'], 'mount' => $mountPoint)));
            if ($response->getHttpCode() < 400) {
                return $response->getJson();
            } else { 
                throw new ClientException('Foxx-Zip install failed');
            }
        } else { 
            throw new ClientException('Foxx-Zip upload failed');
        }
    }

Urls.php has the correct api-paths added (const URL_UPLOAD = '/_api/upload' and const URL_FOXX_INSTALL = '/_admin/foxx/install').

Would you consider accepting a pull-request with this code? If so, I will create one for master/3.0 once we switched over from 2.8

frankmayer commented 7 years ago

Hello @tomterl and thanks for offering to contribute. Yes, that looks fine. Just remember to also provide the functional tests. Thank you

tomterl commented 7 years ago

Merged.

frankmayer commented 7 years ago

Nice!! :+1: