A PHP client library for making HTTP request to Application Broker API https://docs.geeny.io/api/application-broker
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples
src
tests
.editorconfig
.gitignore
.gitlab-ci.yml
.travis.yml
LICENSE.md
README.md
composer.json
docker_install_ci.sh
docs.md
phpunit.xml.dist

README.md

Geeny Application Broker API PHP Client

Minimum PHP Version License: MPL 2.0

The Application Broker API is used for connecting applications to the Geeny platform and accessing data from connected data sources via message streams.

Introduction

The library is based on Guzzle, an extensible PHP HTTP client. Everything what you need before start using it is to setup application on the Geeny platform. Follow the instruction Step 1: Set up application on the Geeny platform if you haven't done it yet.

Requirements

  • PHP >= 7.0
  • cURL Extension (ext-curl)
  • JSON Extension (ext-json)

Installation

You can use Composer or simply Download the Release.

Composer

The preferred method is via composer. Follow the installation instructions if you do not already have composer installed.

Once composer is installed, execute the following command in your project root to install this library:

composer require geeny/application-broker

Download the Release

If you abhor using composer, you can download the package in its entirety. The Releases page lists all stable versions. Download any file with the corresponding release version for a package including this library and its dependencies.

Uncompress the zip file you download.


Finally, be sure to include the autoloader:

require_once 'vendor/autoload.php';

Examples

See the /examples directory for examples of the key client features. You can run them only in your command line shell by running separate script.

 $ php examples/index.php

Usage

require 'vendor/autoload.php';

use Geeny\ApplicationBroker\Client;

$config = [
    'application_id'  => '<your-application-id>',
    'message_type_id' => '<your-message-type-id>',
    'token'           => '<your-application-jwt-token>',
    'http'            => [
            // guzzle http client options
            // see for details http://docs.guzzlephp.org/en/stable/request-options.html
            // 'debug' => true,
    ],
    // 'base_url' => '<geeny_base_url>' // optional parameter
];

$apiClient = new Client($config);

Making Requests

For more information read documentation of Guzzle Promises.

use GuzzleHttp\Promise as promise;
use Geeny\ApplicationBroker\SchemaDefinitions as schema;
use Geeny\ApplicationBroker\Exception\ApplicationBrokerRequestException;
    
$messages = promise\coroutine(
    function () use ($api) {
        /* 1. Getting shards */
        $shards = yield $api->getListOfShards()->then(
            function (schema\ShardsResponse $shardsResponse) {
                return $shardsResponse->getShards();
            }
        )->otherwise(
            function (ApplicationBrokerRequestException $applicationBrokerRequestException) {
                // catch exception
            }
        )
        ;

        /* 2.1. Prepare request body before getting iterators */
        $iteratorRequests = array_map(
            function (schema\Shard $shard) {
                return new schema\IteratorRequest(
                    $shard, 
                    $iteratorType = schema\Types\Iterator\Earliest::create(), 
                    $maxBatchSize = 100, 
                    $startingSequenceNumber = null
                );
            },
            $shards
        );

        /* 2.2. Getting shard iterators */
        $shardIterators = yield promise\all(
            array_map(
                [$api, 'createShardIterator'],
                $iteratorRequests
            )
        )->then(
            function (array $result) {
                return array_map(
                    function (schema\IteratorResponse $iteratorResponse) {
                        return $iteratorResponse->getShardIterator();
                    },
                    $result
                );
            }
        )->otherwise(
            function (ApplicationBrokerRequestException $applicationBrokerRequestException) {
                // catch exception
            }
        )
        ;

        $iterator = promise\iter_for(
            array_map(
                [$api, 'getMessageData'],
                $shardIterators
            )
        );

        /**
         * 3. Getting messages in infinite loop
         *
         * Highly recommend Event Loop Integration instead of infinite loop
         * @link https://github.com/guzzle/promises#event-loop-integration
         * @link https://github.com/reactphp/event-loop
         */
        while ($iterator->valid()) {
            $promise = $iterator->current();

            $nextIterator = (yield $promise->then(
                function (schema\MessageResponse $messageResponse) {
                    return $messageResponse->getNextIterator();
                }
            ));

            $iterator->offsetSet($iterator->key(), $api->getMessageData($nextIterator));

            if ($iterator->key() === $iterator->count() - 1) {
                $iterator->rewind();
            } else {
                $iterator->next();
            }
        }
    }
)->wait();

API Documentation

Follow the API Documentation link to read more.

License

Copyright (C) 2018 Telefónica Germany Next GmbH, Charlottenstrasse 4, 10969 Berlin.

This project is licensed under the terms of the Mozilla Public License Version 2.0.