|
1
|
|
|
<?php |
|
2
|
|
|
namespace Earnould\LaravelVeloApi; |
|
3
|
|
|
|
|
4
|
|
|
use Closure; |
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Illuminate\Support\Facades\Cache; |
|
8
|
|
|
use Earnould\LaravelVeloApi\Exceptions\VeloException; |
|
9
|
|
|
|
|
10
|
|
|
class VeloClient |
|
11
|
|
|
{ |
|
12
|
|
|
public function __construct(Client $client = null) |
|
13
|
|
|
{ |
|
14
|
|
|
$this->guzzleClient = $client ?: new Client(); |
|
|
|
|
|
|
15
|
|
|
$this->client_id = config('laravel-velo-api.client_id'); |
|
|
|
|
|
|
16
|
|
|
$this->client_secret = config('laravel-velo-api.client_secret'); |
|
|
|
|
|
|
17
|
|
|
$this->token_url = "https://antwerp.pub.api.smartbike.com/oauth/v2/token"; |
|
|
|
|
|
|
18
|
|
|
$this->api_url = "https://antwerp.pub.api.smartbike.com/api/en/v3"; |
|
|
|
|
|
|
19
|
|
|
$this->apiResponseFormat = '.json'; |
|
|
|
|
|
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Request Velo API Access Token response |
|
25
|
|
|
* |
|
26
|
|
|
* @return void |
|
27
|
|
|
*/ |
|
28
|
|
|
private function requestAccessToken() : string |
|
29
|
|
|
{ |
|
30
|
|
|
$response = $this->guzzleClient->post($this->token_url, [ |
|
31
|
|
|
'form_params' => [ |
|
32
|
|
|
'client_id' => $this->client_id, |
|
33
|
|
|
'client_secret' => $this->client_secret, |
|
34
|
|
|
'grant_type' => 'client_credentials' |
|
35
|
|
|
], |
|
36
|
|
|
]); |
|
37
|
|
|
|
|
38
|
|
|
$access_token = json_decode($response->getBody()->getContents())->access_token; |
|
39
|
|
|
|
|
40
|
|
|
if(!empty($access_token)) { |
|
41
|
|
|
return $access_token; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
throw new VeloException("An access token is required to perform requests tothe Velo API", 403); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get cached token or request a new one. |
|
49
|
|
|
* |
|
50
|
|
|
* @return void |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getAccessToken() |
|
53
|
|
|
{ |
|
54
|
|
|
return Cache::remember('access_token', 55, Closure::fromCallable([$this, 'requestAccessToken'])); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Prepare the uri for a request |
|
59
|
|
|
* |
|
60
|
|
|
* @param [type] $resource |
|
|
|
|
|
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
|
|
function prepareUri($resource) : string |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
$uri = $this->api_url . '/' . $resource . $this->apiResponseFormat; |
|
66
|
|
|
|
|
67
|
|
|
return $uri; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Fetch all Velo stations |
|
72
|
|
|
* |
|
73
|
|
|
* @return Collection |
|
74
|
|
|
*/ |
|
75
|
|
|
function fetchStations() : Collection |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
|
|
$response = $this->guzzleClient->get($this->prepareUri('stations'), [ |
|
78
|
|
|
'headers' => [ |
|
79
|
|
|
'Authorization' => 'Bearer ' . $this->getAccessToken(), |
|
|
|
|
|
|
80
|
|
|
] |
|
81
|
|
|
]); |
|
82
|
|
|
|
|
83
|
|
|
try |
|
84
|
|
|
{ |
|
85
|
|
|
$stations = json_decode($response->getBody()->getContents())->stations; |
|
86
|
|
|
} |
|
87
|
|
|
catch (\ErrorException $e) { |
|
88
|
|
|
throw new VeloException($e->getMessage(), $e->getCode()); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return collect($stations); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Fetch all Velo stations statuses |
|
96
|
|
|
* |
|
97
|
|
|
* @return Collection |
|
98
|
|
|
*/ |
|
99
|
|
|
function fetchStationsStatuses() : Collection |
|
|
|
|
|
|
100
|
|
|
{ |
|
101
|
|
|
$response = $this->guzzleClient->get($this->prepareUri('stations/status'), [ |
|
102
|
|
|
'headers' => [ |
|
103
|
|
|
'Authorization' => 'Bearer ' . $this->getAccessToken(), |
|
|
|
|
|
|
104
|
|
|
] |
|
105
|
|
|
]); |
|
106
|
|
|
|
|
107
|
|
|
try |
|
108
|
|
|
{ |
|
109
|
|
|
$stationsStatuses = json_decode($response->getBody()->getContents())->stationsStatus; |
|
110
|
|
|
} |
|
111
|
|
|
catch (\ErrorException $e) { |
|
112
|
|
|
throw new VeloException($e->getMessage(), $e->getCode()); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return collect($stationsStatuses); |
|
116
|
|
|
} |
|
117
|
|
|
} |