Giter Club home page Giter Club logo

sylius-api's Introduction

FriendsOfApi Boilerplate

Latest Version Build Status Code Coverage Quality Score Total Downloads

Install

Via Composer

$ composer require friendsofapi/boilerplate

Usage

$apiClient = new ApiClient();
$total = $apiClient->stats()->total();
echo $total->getCount(); // 22;

Example

This repository contains an example API client for FakeTwitter. The API for FakeTwitter has the following endpoints.

Method URI Parameters
GET /v1/tweets (string) hashtag
POST /v1/tweets/new (string) message, (string) location, (array) hashtags
GET /v1/tweets/{id}
PUT /v1/tweets/{id}/edit (string) message, (string) location, (array) hashtags
DELETE /v1/tweets/{id}/delete
GET /v1/stats/{username} (int) start, (int) end
GET /v1/stats/total (int) start, (int) end

Develop

APIs are usually split into categories, called Resources. In your implementation you should also reflect these categories, for example by having their own classes in Api/. Let's take a look at Api/Stats in our case. The response of any call should be an object in Model/Stats/X, like Model/Stats/Total.

Hydrator

The end user chooses which hydrator to use. The default one should return domain objects.

Request builder

The request builder creates a PSR-7 request with a multipart stream when necessary If the API does not require multipart streams you should remove the RequestBuilder and replace it with a RequestFactory.

Domain objects as parameters

If the API requires lots of parameters for a specific endpoint it could be tempting to create a domain object and pass it as an argument to that endpoint.

public function create(string $username, Tweet $model) {
    // send the Tweet to Fake Twitter API
    // ...
}

$model = new Tweet();
$model->setMessage('foobar');
$model->addHashTag('stuff');
$model->addHashTag('test');
$model->setLocation('Stockhom/Sweden');
// ...
$api->create('foobar', $model);

This approach, however, is not preferred as the created Tweet object is an unnecessary overhead. It could also conflict with the application developers' Tweet object. Also, requests usually don't have the same parameters as responses, so using the same object for both is impossible in most of the cases. Instead of forcing the users to use your Tweet object you should use an array for passing parameters to the request.

public function create(string $username, array $param) {
    // send the Tweet to Fake Twitter API
    // ...
}

$param['message' => 'foobar'];
$param['hashtags' => ['stuff', 'test']];
$param['location' => 'Stockhom/Sweden'];
// ...
$api->create('foobar', $param);

If your parameters are complex, you can provide a TweetBuilder. Since it's a builder, fluent interface might be a good idea here. But be aware that fluent interfaces are evil.

public function create(string $username, array $param) {
    // send the Tweet to Fake Twitter API
    // ...
}

$builder = (new TweetBuilder())
    ->setMessage('foobar');
    ->addHashTag('stuff');
    ->addHashTag('test');
    ->setLocation('Stockhom/Sweden')
;

// ...
$api->create('foobar', $builder->build());

License

The MIT License (MIT). Please see License File for more information.

sylius-api's People

Contributors

kaszim avatar nyholm avatar sagikazarmark avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.