Code Monkey home page Code Monkey logo

fake-api-server's Introduction

Fake API Server

Usage

All that you need is ... Data Provider! It's provide a data depend on a request

Data Providers

Each Data Provider implements interface Sata\FakeServerApi\DataProvider\IDataProvider Now there is only one method data that receive Psr\Http\Message\ServerRequestInterface and returns some data

RouterDataProvider

It's an compose data provider. That retrieves routes with data providers It uses nikic/FastRoute under the hood, so every route should meet the requirements.

Example

$provider = new RouterDataProvider([
    // limit and offset parameters only make sense
    '/local/articles[/]' => new PathDataProvider($filesystem, ['limit', 'offset']),
    // all other local data get from local data folder
    '/local/{trail:.*}' => new PathDataProvider($filesystem),
    // all r get from reddit (try request /r/PHP/hot.json)
    // requested every time cause of VoidCache
    '/r/{stub:.*}.json' => new ProxyDataProvider($redditGuzzle, $voidCache),
    // all r get from reddit (try request /get?your=paramter)
    '/get{stub:.*}' => new ProxyDataProvider($httpbinGuzzle, $cache),
    // all r get from reddit (try request /post?your=paramter with POST)
    '/post{stub:.*}' => new ProxyDataProvider($httpbinGuzzle, $cache)
]);

$data = $provider->data($request);

Features

After request satisfy the route, new instance of a request (with matched parameters) pass to the target data provider.

FileDataProvider

Just returns content of specified file.

Example

$provider = new FileDataProvider($filesystem, 'path/to/the/file.json');
$data = $provider->data($request);

Features

It's receives an instance of League\Flysystem\Filesystem. About Flysystem read more here - thephpleague/flysystem.

PathDataProvider

Like a FileDataProvider it's returns a file content, but with some additional bahavior.

First of it's mount some folder (via Flysystem) to the request (or a visa versa). Also it can handle significant parameters.

Example

For example, if you have an posts API, you can mount data/posts/ folder to all /posts requests. And of course, your API has a pagination logic. Like a /posts?page=1, /posts?page=2.

So by here is map of your request to the files

  • /posts -> data/posts/default.json
  • /posts?page=1 -> data/posts/page_1.json
  • /posts?page=2 -> data/posts/page_2.json
  • /posts?some=parameter -> data/posts/default.json

By default, if the file is not found the default file will be mounted.

$filesystem = new Filesystem(new Local(__DIR__ . '/data'));
$provider = new PathDataProvider($filesystem, ['page']);
$data = $provider->data($request);

ProxyDataProvider

Primarily this data provider used for a slow connections.

It's receives an instances of Guzzle Client and Doctrine Cache

So every request will be proxied to the remote server and saved in cache.

Example

$cache = new FilesystemCache(__DIR__ . '/cache');
$voidCache = new VoidCache();
$filesystem = new Filesystem(new Local(__DIR__ . '/data'));
$redditGuzzle = new GuzzleHttp\Client(['base_uri' => 'https://www.reddit.com']);
$httpbinGuzzle = new GuzzleHttp\Client(['base_uri' => 'http://httpbin.org']);

// request everytime cause of VoidCache
$redditProvider = new ProxyDataProvider($redditGuzzle, $voidCache);
// request only once an save to the FilesystemCache
$httpbinProvider = new ProxyDataProvider($httpbinGuzzle, $cache);

Restrictions

There are really bad work with other response/request parameters. If your application relies on http statuses, so it's may be a huge problem.

fake-api-server's People

Contributors

satahippy avatar

Watchers

 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.