Skip to content

Commit d12d51d

Browse files
committed
Merge pull request #4 from Nekland/1.0
1.0
2 parents 8693162 + a8d8413 commit d12d51d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1874
-290
lines changed

Diff for: .travis.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: php
2+
3+
php:
4+
- 5.4
5+
- 5.5
6+
- 5.6
7+
- hhvm
8+
9+
before_script:
10+
- composer install --dev --prefer-source
11+
12+
script: ./vendor/bin/phpspec run

Diff for: CHANGELOG.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
1.0.0 (2014-09-XX)
2+
==================
3+
4+
New awesome features
5+
--------------------
6+
7+
* Drop Guzzle dependency
8+
* Added dependency on Symfony Event Dispatcher Component
9+
* Added specificity tests with phpspec
10+
* Added cache strategy support
11+
* Added transformer strategies
12+
* Made the ApiFactory "IDE friendly"
13+
14+
Compatibility breaks:
15+
---------------------
16+
17+
Almost everything, sorry guys this is a new, very very major version :-).

Diff for: Readme.md

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
Nekland Base API
22
================
33

4+
[![Build Status](https://travis-ci.org/Nekland/BaseApi.svg)](https://travis-ci.org/Nekland/BaseApi)
5+
46
Why
57
---
68

79
I made this project because I created a lib for Youtube API and another for Souncloud API.
810

911
Both libs have the same needs. To avoid duplicated code, I made this little project that can be used as a base for the API lib you want to create.
1012

11-
Inspiration
12-
-----------
13+
How
14+
---
15+
16+
[x] [Semver](http://semver.org) compliant
17+
[x] [HHVM](http://hhvm.com/) compatible
18+
[x] [Composer](http://packagist) installable
19+
20+
Documentation
21+
-------------
1322

14-
This project look like php-github-api, and it's not innocent. I used it as model.
23+
This project does not need so much documentation but I wrote some for interested people. Checkout the [doc](doc) folder.
1524

16-
Thanks to KnpLabs & Contributors on this project.
25+
> This project is inspirated by KNPLabs api libs.

Diff for: composer.json

+21-6
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,35 @@
77
"authors": [
88
{
99
"name": "Maxime Veber",
10-
"email": "nekland@gmail.com",
10+
"email": "nek.dev@gmail.com",
1111
"homepage": "http://nekland.fr"
12+
},
13+
{
14+
"name": "Nekland Team",
15+
"email": "[email protected]",
16+
"homepage": "http://team.nekland.fr"
1217
}
1318
],
19+
"require": {
20+
"php": ">=5.4",
21+
"symfony/event-dispatcher": "~2.3"
22+
},
23+
"require-dev": {
24+
"phpspec/phpspec": "dev-master",
25+
"guzzlehttp/guzzle": "~4.2"
26+
},
1427
"suggest": {
1528
"nekland/youtube-api": "Youtube API made easy !",
1629
"nekland/soundcloud-api": "Soundcloud API made easy !",
17-
"guzzle/guzzle": "The default HttpClient will not work without this library."
30+
"guzzlehttp/guzzle": "The only http adapter available for now is for this library."
1831
},
1932
"autoload": {
2033
"psr-0": { "Nekland\\": "lib/" }
2134
},
22-
"require-dev": {
23-
"php": ">=5.4",
24-
"phpunit/phpunit": ">=3.7"
35+
"minimum-stability": "dev",
36+
"extra": {
37+
"branch-alias": {
38+
"dev-master": "1.0-dev"
39+
}
2540
}
26-
}
41+
}

Diff for: doc/api_classes.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Api classes
2+
===========
3+
4+
5+
They should simply extends the `AbstractApi` class.
6+
7+
She provide you useful methods (get/put/post/delete rest methods) and as the class do some job, you just have to create methods like:
8+
9+
* getResourceById
10+
* deleteResource
11+
12+
13+
> You have to register a namespace to your api objects in the api factory

Diff for: doc/api_factory.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
The ApiFactory
2+
==============
3+
4+
This class instantiate "[Api classes](api_classes.md)" when you use it like that thanks to the magic method `call`:
5+
6+
```php
7+
<?php
8+
9+
$apiFactory->getMyAwesomeApi();
10+
```
11+
12+
So the first thing you have to do when building your API is to extend it and implement the only needed method.
13+
14+
You can also:
15+
16+
* Redefine the `getTransformer` method to change the default one.
17+
18+
Now, build your [API classes](api_classes.md).

Diff for: doc/auth_and_cache.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Auth & Cache strategies
2+
=======================
3+
4+
In order to help you manage authentication or cache systems, this lib provides you useful strategies managements.
5+
6+
> Since theses classes are register as event listener (it use the event dispatcher of Symfony), you can add multiple strategies of each feature.
7+
8+
The cache strategy
9+
==================
10+
11+
This are classes that implements the `CacheStrategyInterface`.
12+
13+
You register them using the method `useCache` of the ApiFactory.
14+
15+
16+
The authentication strategy
17+
===========================
18+
19+
This are classes implementing the `AuthenticationStrategyInterface`.
20+
21+
You register them using the method `useAuthentication` of the ApiFactory

Diff for: doc/index.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Nekland base api
2+
================
3+
4+
The idea is to made easy build of new PHP api libs. So this lib provide you some abstract class that will work together with other classes.
5+
6+
You will learn more about things in the dedicated pages of doc:
7+
8+
* [ApiFactory](api_factory.md)
9+
* [Api classes](api_classes.md)
10+
* [Auth and Cache strategies](auth_and_cache.md)
11+
* [Transformers](transformers.md)

Diff for: doc/transformers.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Transformers
2+
============
3+
4+
Transformers are classes implementing the `TransformerInterface` interface.
5+
6+
You can set them by using the `setTransformer` of the `ApiFactory` class.
7+
8+
> You can only set one transformer because you can't retrieve the same data in many forms in the same request.

Diff for: lib/Nekland/BaseApi/Api.php

-51
This file was deleted.

Diff for: lib/Nekland/BaseApi/Api/AbstractApi.php

+104-6
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,118 @@
1111

1212
namespace Nekland\BaseApi\Api;
1313

14-
1514
use Nekland\BaseApi\Api;
15+
use Nekland\BaseApi\Http\AbstractHttpClient;
16+
use Nekland\BaseApi\Transformer\JsonTransformer;
17+
use Nekland\BaseApi\Transformer\TransformerInterface;
1618

1719
abstract class AbstractApi
1820
{
19-
protected $api;
21+
/**
22+
* @var AbstractHttpClient
23+
*/
24+
private $client;
25+
26+
/**
27+
* @var TransformerInterface
28+
*/
29+
private $transformer;
30+
31+
public function __construct(AbstractHttpClient $client, TransformerInterface $transformer = null) {
32+
$this->client = $client;
33+
$this->transformer = $transformer ?: new JsonTransformer();
34+
}
35+
36+
/**
37+
* Set the transformer that will be used to return data
38+
*
39+
* @param TransformerInterface $transformer
40+
* @return self
41+
*/
42+
public function setTransformer(TransformerInterface $transformer)
43+
{
44+
$this->transformer = $transformer;
45+
46+
return $this;
47+
}
48+
49+
/**
50+
* Execute a http get query
51+
*
52+
* @param string $path
53+
* @param array $body
54+
* @param array $headers
55+
* @return array|mixed
56+
*/
57+
protected function get($path, array $body = [], array $headers = [])
58+
{
59+
$client = $this->getClient();
60+
$request = $client::createRequest('GET', $path, $body, $headers);
61+
62+
return $this->transformer->transform($client->send($request));
63+
}
64+
65+
/**
66+
* Execute a http put query
67+
*
68+
* @param string $path
69+
* @param array $body
70+
* @param array $headers
71+
* @return array|mixed
72+
*/
73+
protected function put($path, array $body = [], array $headers = [])
74+
{
75+
$client = $this->getClient();
76+
$request = $client::createRequest('PUT', $path, $body, $headers);
77+
78+
return $this->transformer->transform($client->send($request));
79+
}
80+
81+
/**
82+
* Execute a http post query
83+
*
84+
* @param string $path
85+
* @param array $body
86+
* @param array $headers
87+
* @return array|mixed
88+
*/
89+
protected function post($path, array $body = [], array $headers = [])
90+
{
91+
$client = $this->getClient();
92+
$request = $client::createRequest('POST', $path, $body, $headers);
93+
94+
return $this->transformer->transform($client->send($request));
95+
}
96+
97+
/**
98+
* Execute a http delete query
99+
*
100+
* @param string $path
101+
* @param array $body
102+
* @param array $headers
103+
* @return array|mixed
104+
*/
105+
protected function delete($path, array $body = [], array $headers = [])
106+
{
107+
$client = $this->getClient();
108+
$request = $client::createRequest('DELETE', $path, $body, $headers);
109+
110+
return $this->transformer->transform($client->send($request));
111+
}
20112

21-
public function __construct(Api $api)
113+
/**
114+
* @return AbstractHttpClient
115+
*/
116+
protected function getClient()
22117
{
23-
$this->api = $api;
118+
return $this->client;
24119
}
25120

26-
protected function get($path, array $parameters = [], array $requestHeaders = [])
121+
/**
122+
* @return TransformerInterface
123+
*/
124+
protected function getTransformer()
27125
{
28-
return json_decode((string) $this->api->getClient()->get($path, $parameters, $requestHeaders), true);
126+
return $this->transformer;
29127
}
30128
}

0 commit comments

Comments
 (0)