Overview

Namespaces

  • Baguette
    • Mastodon
      • Config
      • Entity
      • Grant
      • Service
  • GuzzleHttp
    • Cookie
    • Exception
    • Handler
    • Promise
  • Psr
    • Http
      • Message
  • Teto
    • Object

Classes

  • Baguette\Mastodon\Client
  • Baguette\Mastodon\Config\DotEnvStorage
  • Baguette\Mastodon\Entity\Account
  • Baguette\Mastodon\Entity\Application
  • Baguette\Mastodon\Entity\Attachment
  • Baguette\Mastodon\Entity\Card
  • Baguette\Mastodon\Entity\Context
  • Baguette\Mastodon\Entity\Entity
  • Baguette\Mastodon\Entity\Error_
  • Baguette\Mastodon\Entity\Instance
  • Baguette\Mastodon\Entity\Mention
  • Baguette\Mastodon\Entity\Notification
  • Baguette\Mastodon\Entity\Status
  • Baguette\Mastodon\Entity\Tag
  • Baguette\Mastodon\EntityCaster
  • Baguette\Mastodon\Grant\CodeGrant
  • Baguette\Mastodon\Grant\Grant
  • Baguette\Mastodon\Grant\NoopGrant
  • Baguette\Mastodon\Grant\PasswordCredential
  • Baguette\Mastodon\Mastodon
  • Baguette\Mastodon\Requester
  • Baguette\Mastodon\Service\AuthFactory
  • Baguette\Mastodon\Service\Authorization
  • Baguette\Mastodon\Service\Scope
  • Baguette\Mastodon\Service\SessionStorage
  • Baguette\Mastodon\Service\Toot

Interfaces

  • Baguette\Mastodon\Config\Storage

Exceptions

  • Baguette\Mastodon\Service\AuthorizationException

Constants

  • Baguette\Mastodon\Entity\DATETIME_FORMAT

Functions

  • Baguette\Mastodon\authorization
  • Baguette\Mastodon\credential
  • Baguette\Mastodon\Entity\map
  • Baguette\Mastodon\Entity\mapValues
  • Baguette\Mastodon\Entity\toArrayValue
  • Baguette\Mastodon\grant
  • Baguette\Mastodon\http
  • Baguette\Mastodon\request
  • Baguette\Mastodon\scope
  • Baguette\Mastodon\session
  • Baguette\Mastodon\toot
  • Overview
  • Namespace
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 
<?php

/**
 * Mastodon API functions
 *
 * @author    USAMI Kenta <tadsan@zonu.me>
 * @copyright 2017 Baguette HQ
 * @license   https://www.gnu.org/licenses/gpl-3.0.html GPL-3.0
 */

namespace Baguette\Mastodon;

use Baguette\Mastodon\Grant;
use Baguette\Mastodon\Service;
use Baguette\Mastodon\Service\Scope;

/**
 * @param  string   $instance
 * @param  string   $client_id
 * @param  string   $client_secret
 * @param  array    $options
 * @return Mastodon
 */
function session($instance, $client_id, $client_secret, array $options)
{
    $scope = null;
    $grant = null;
    $authorization = null;

    $client = new Client($instance);

    if (isset($options['scope'])) {
        $scope = scope($options['scope']);
    }

    if (isset($options['credential'])) {
        trigger_error('`credential` is obsolete option.  Use `grant` instead.', E_USER_DEPRECATED);
        $grant = credential($options['credential']);
    }

    if (isset($options['grant'])) {
        $grant = grant($options['grant']);
    }

    if (isset($options['authorization'])) {
        $authorization = authorization($options['authorization']);
    }

    //throw new \LogicException('"scope" is not set.');

    $auth_factory = new Service\AuthFactory($client, $client_id, $client_secret);
    if ($grant !== null) {
        $auth_factory->setGrant($grant);
    }

    $session = new Service\SessionStorage($auth_factory, $scope);
    if ($authorization !== null) {
        $session->setAuthorization($authorization);
    }

    return new Mastodon($client, $session);
}

/**
 * @param  Scope|string|string[]
 * @return Scope
 */
function scope($scope)
{
    if (is_array($scope)) {
        return new Scope($scope);
    } elseif ($scope instanceof Scope) {
        return $scope;
    }

    return new Scope(explode(' ', $scope));
}

/**
 * @param  string $toot_string
 * @param  array  $options
 * @return Service\Toot
 */
function toot($toot_string, array $options = [])
{
    return new Service\Toot($toot_string, $options);
}

/**
 * @deprecated
 */
function credential(array $data)
{
    trigger_error('credential() is obsolete function.  Use grant() instead.', E_USER_DEPRECATED);
    return grant($data);
}

/**
 * @return Grant\Grant
 */
function grant(array $data)
{
    if (isset($data['username'], $data['password'])) {
        return new Grant\PasswordCredential($data['username'], $data['password']);
    }
}

/**
 * @return Service\Authorization
 */
function authorization(array $data)
{
    return Service\Authorization::fromObject((object)$data);
}

/**
 * @return \GuzzleHttp\ClientInterface
 */
function http(\GuzzleHttp\ClientInterface $client = null)
{
    /** @var \GuzzleHttp\ClientInterface */
    static $cached_client;

    if ($client !== null) {
        $cached_client = $client;
    } elseif ($cached_client === null) {
        $cached_client = new \GuzzleHttp\Client;
    }

    return $cached_client;
}

/**
 * Manually API Request
 *
 * @param  Mastodon        $service A instance object of Mastodon class
 * @param  string          $method  HTTP Method (GET, POST, PUT, DELETE, ...)
 * @param  string          $path    API Path (URL)
 * @param  array           $options Options for GuzzleHttp
 * @param  string|string[] $class   A class name of return value
 * @return \Psr\Http\Message\ResponseInterface|Entity\Entity|Entity\Entity[]|mixed Returns ResponseInterface if $class is NULL.
 */
function request(Mastodon $service, $method, $path, $options, $class = null)
{
    $response = $service->client->requestAPI($method, $path, $options, $service->session);

    if ($class === null) {
        return $response;
    }

    return Entity\map($class, \GuzzleHttp\json_decode($response->getBody(), true));
}
ξ ^ω^)ξ Baguette PHP Mastodon API Client / SDK API documentation generated by ApiGen