buzzi-io/buzzi-sdk-php

2.0.1 2017-10-23 16:27 UTC

This package is not auto-updated.

Last update: 2024-09-15 01:20:07 UTC


README

Buzzi SDK for PHP

Install

The recommended way to install buzzi-io/buzzi-sdk-php is through composer.

composer require buzzi-io/buzzi-sdk-php

Usage

Don't forget to autoload:

<?php
require 'vendor/autoload.php';

Initialize Sdk object:

use Buzzi\Sdk;

$sdk = new Sdk([
    Sdk::CONFIG_AUTH_ID => '<your-buzzi-api-id-here>',
    Sdk::CONFIG_AUTH_SECRET => '<your-buzzi-api-secret-here>',
    
    // Optional parameters:
    //
    // Sdk::CONFIG_HOST => <buzzi-host>, if is not set will be used either production or sandbox predefined hosts depends on Sdk::CONFIG_SANDBOX
    // Sdk::CONFIG_SANDBOX => false,
    // Sdk::CONFIG_DEBUG => false,
    // Sdk::CONFIG_LOG_FILE_NAME => \Buzzi\Http::DEFAULT_LOG_FILE_NAME,
    // Sdk::CONFIG_LOG_LINE_FORMAT => \Buzzi\Http::DEFAULT_LOG_LINE_FORMAT,
    // Sdk::CONFIG_LOG_MESSAGE_FORMAT => \Buzzi\Http::DEFAULT_LOG_MESSAGE_FORMAT
]);

In order to verify credentials:

$supportService = $sdk->getSupportService();

try {

    $isAuthorized = $supportService->isAuthorized();

} catch (\Buzzi\Exception\HttpException $e) {

    $isAuthorized = false;
}

In order to send (publish) data:

$publishService = $sdk->getPublishService();

try {

    $eventId = $publishService->send('buzzi.generic.test', ["message" => "Hello, World", "timestamp" => date(DATE_ATOM)]);
    
} catch (\Buzzi\Exception\HttpException $e) {

    // The exceptions is thrown if http request is not successful
    
} catch (\RuntimeException $e) {

    // The exceptions is thrown if response does not contain event ID
}

In order to get count of deliveries:

$consumeService = $sdk->getConsumeService();

try {

    $countDeliveries = $consumeService->getCount();
    
} catch (\Buzzi\Exception\HttpException $e) {

    // The exceptions is thrown if http request is not successful
    
} catch (\RuntimeException $e) {

    // The exceptions is thrown if response does not contain count value
}

In order to fetch one delivery:

$consumeService = $sdk->getConsumeService();

try {

    $delivery = $consumeService->fetch();
    
} catch (\Buzzi\Exception\HttpException $e) {

    // The exceptions is thrown if http request is not successful
}

In order to fetch a batch of deliveries:

// fetchs all deliveries but not more then 100
list($deliveries, $exceptions) = $consumeService->batchFetch(100);

In order to confirm that delivery is processed:

try {

    $consumeService->confirmDelivery($delivery);
    // OR
    $consumeService->confirm($delivery->getReceipt());

} catch (\Buzzi\Exception\HttpException $e) {

    // The exceptions is thrown if http request is not successful
}

In order to submit error during delivery processing:

try {

    // $errorData could contain any information about the error
    $errorData = [
        'message' => 'reason for error'
    ];
    $consumeService->submitError($delivery->getReceipt(), $errorData);

} catch (\Buzzi\Exception\HttpException $e) {

    // The exceptions is thrown if http request is not successful
}

Information which could be taken from Buzzi\Exception\HttpException:

try {

// request

} catch (\Buzzi\Exception\HttpException $e) {
    // The exceptions is thrown if http request is not successful

    $e->getMessage();           // returns either grabbed message from response body or reason phrase
    $e->getStatusCode();        // returns http status code of the response
    $e->getResponse();          // returns \Psr\Http\Message\ResponseInterface|\GuzzleHttp\Psr7\Response
    $e->getResponseBody();      // returns decoded response body
    $e->getResponseBody(false); // returns response body as json string
}