Giter Club home page Giter Club logo

rest-client's Introduction

Flex \ RestClient

Gère les méthodes http GET, POST (creation), PUT (mise à jour) et DELETE

Installation

Ajouter à votre composer.json, le dépot suivant

"repositories": [
    {
        "type": "git",
        "url": "https://github.com/mallsbill/rest-client.git"
    }
]

et ajouter au require

composer require pephpit/rest-client

Exemple d'utilisation

Récupérer une ressource

use Flex\RestClient;

$Client = new RestClient\Client('http://monapi/ressources/1');
$Response = $Client->execute();

if($Response->isOk()){
	$ressource = $Response->getJsonDecode();
}

Ajouter une ressource

use Flex\RestClient;

$body = array( 'name' => 'ma ressource' );

$Client = new RestClient\Client('http://monapi/ressources', RestClient\Method::POST, $body );
$Response = $Client->execute();

if($Response->isCreated()){
	$id = $Response->getBody();
}

Mettre à jour une ressource

use Flex\RestClient;

$body = array( 'name' => 'nouveau nom' );

$Client = new RestClient\Client('http://monapi/ressources/1', RestClient\Method::PUT, $body );
$Response = $Client->execute();

if($Response->isSuccessful()){
	$id = $Response->getBody();
}

Supprimer une ressource

use Flex\RestClient;

$Client = new RestClient\Client('http://monapi/ressources/1', RestClient\Method::DELETE);
$Response = $Client->execute();

if($Response->isSuccessful()){
	// delete ok
}

Requètes en parallèle

use Flex\RestClient;

$ClientCollection = new RestClient\ClientCollection();
// ajoute la suite de la collection
$ClientCollection->add(new RestClient\Client('http://monapi/ressources/1');
// ajoute en définissant la clé
$ClientCollection->set('res2', new RestClient\Client('http://monapi/ressources/2');
// ajoute en définissant la clé, utilise offsetSet d'ArrayAccess
$ClientCollection['res42'] = new RestClient\Client('http://monapi/ressources/42');

// on exécute les requètes
$ResponseCollection = $ClientCollection->execute();

// on récupère le retour de la requète ajouté avec add (index 0)
$body = $ResponseCollection->get(0)->getBody();
// on récupère le retour de la requète res2 grâce à offsetGet d'ArrayAccess
$body2 = $ResponseCollection['res2']->getBody();
// on récupère le retour de la requète res42
$body = $ResponseCollection->get('res42')->getBody();

// on peut aussi boucler sur la collection
foreach($ResponseCollection as $Response){
	if($Response->isSuccessful()){
		// do something
	}
}

Tests unitaires

Lancer tout les tests

composer test

Lancer un test en particulier

composer atoum -- -f tests/Client.php

rest-client's People

Contributors

mallsbill 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.