Skip to content

Commit 9263bad

Browse files
author
Car API
authored
Merge pull request #15 from car-api-team/v2
Added support for v2 API
2 parents eeec41d + dcf5f45 commit 9263bad

6 files changed

Lines changed: 152 additions & 71 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ $sdk = \CarApiSdk\CarApi::build([
5353
'secret' => getenv('CARAPI_SECRET'),
5454
'httpVersion' => '2.0', // we recommend keeping the default 1.1
5555
'encoding' => ['gzip'],
56+
'apiVersion' => 'v1' // the default is v2
5657
]);
5758
```
5859

src/BaseApi.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class BaseApi
1818
protected StreamFactoryInterface $streamFactory;
1919
protected UriFactoryInterface $uriFactory;
2020
protected string $jwt;
21+
protected string $apiVersion;
2122

2223
/**
2324
* Construct
@@ -32,6 +33,7 @@ public function __construct(CarApiConfig $config, ?Psr18Client $client = null)
3233
$this->host = ($config->host ?? 'https://carapi.app') . '/api';
3334
$this->streamFactory = Psr17FactoryDiscovery::findStreamFactory();
3435
$this->uriFactory = Psr17FactoryDiscovery::findUriFactory();
36+
$this->apiVersion = $config->apiVersion === 'v2' ? '/v2' : '';
3537
}
3638

3739
/**

src/CarApi.php

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class CarApi extends BaseApi
1313
* @param array $options See CarApiConfig for options
1414
*
1515
* @return self
16+
* @throws CarApiException
1617
*/
1718
public static function build(array $options): self
1819
{
@@ -29,7 +30,7 @@ public static function build(array $options): self
2930
*/
3031
public function years(array $options = []): array
3132
{
32-
return $this->getDecoded('/years', $options, true);
33+
return $this->getDecoded('/years' . $this->apiVersion, $options, true);
3334
}
3435

3536
/**
@@ -42,7 +43,7 @@ public function years(array $options = []): array
4243
*/
4344
public function makes(array $options = []): \stdClass
4445
{
45-
return $this->getDecoded('/makes', $options);
46+
return $this->getDecoded('/makes' . $this->apiVersion, $options);
4647
}
4748

4849
/**
@@ -55,7 +56,20 @@ public function makes(array $options = []): \stdClass
5556
*/
5657
public function models(array $options = []): \stdClass
5758
{
58-
return $this->getDecoded('/models', $options);
59+
return $this->getDecoded('/models' . $this->apiVersion, $options);
60+
}
61+
62+
/**
63+
* Return vehicle submodels
64+
*
65+
* @param array $options An array of options to pass into the request.
66+
*
67+
* @return \stdClass
68+
* @throws CarApiException
69+
*/
70+
public function submodels(array $options = []): \stdClass
71+
{
72+
return $this->getDecoded('/submodels/v2', $options);
5973
}
6074

6175
/**
@@ -68,7 +82,7 @@ public function models(array $options = []): \stdClass
6882
*/
6983
public function trims(array $options = []): \stdClass
7084
{
71-
return $this->getDecoded('/trims', $options);
85+
return $this->getDecoded('/trims' . $this->apiVersion, $options);
7286
}
7387

7488
/**
@@ -81,7 +95,7 @@ public function trims(array $options = []): \stdClass
8195
*/
8296
public function trimItem(int $id): \stdClass
8397
{
84-
return $this->getDecoded(sprintf('/trims/%s', $id));
98+
return $this->getDecoded(sprintf('/trims' . $this->apiVersion . '/%s', $id));
8599
}
86100

87101
/**
@@ -157,7 +171,7 @@ public function obdCodeItem(string $code): \stdClass
157171
*/
158172
public function bodies(array $options = []): \stdClass
159173
{
160-
return $this->getDecoded('/bodies', $options);
174+
return $this->getDecoded('/bodies' . $this->apiVersion, $options);
161175
}
162176

163177
/**
@@ -170,7 +184,7 @@ public function bodies(array $options = []): \stdClass
170184
*/
171185
public function engines(array $options = []): \stdClass
172186
{
173-
return $this->getDecoded('/engines', $options);
187+
return $this->getDecoded('/engines' . $this->apiVersion, $options);
174188
}
175189

176190
/**
@@ -183,7 +197,7 @@ public function engines(array $options = []): \stdClass
183197
*/
184198
public function mileages(array $options = []): \stdClass
185199
{
186-
return $this->getDecoded('/mileages', $options);
200+
return $this->getDecoded('/mileages' . $this->apiVersion, $options);
187201
}
188202

189203
/**
@@ -196,7 +210,7 @@ public function mileages(array $options = []): \stdClass
196210
*/
197211
public function interiorColors(array $options = []): \stdClass
198212
{
199-
return $this->getDecoded('/interior-colors', $options);
213+
return $this->getDecoded('/interior-colors' . $this->apiVersion, $options);
200214
}
201215

202216
/**
@@ -209,7 +223,7 @@ public function interiorColors(array $options = []): \stdClass
209223
*/
210224
public function exteriorColors(array $options = []): \stdClass
211225
{
212-
return $this->getDecoded('/exterior-colors', $options);
226+
return $this->getDecoded('/exterior-colors' . $this->apiVersion, $options);
213227
}
214228

215229
/**

src/CarApiConfig.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class CarApiConfig
1010
public ?string $host;
1111
public string $httpVersion;
1212
public array $encoding;
13+
public string $apiVersion;
1314

1415
/**
1516
* Constructor
@@ -21,19 +22,22 @@ class CarApiConfig
2122
* @param array $encoding Sets the accepts-encoding request header, default: []. To enable decoding
2223
* set this option to ['gzip'] and ensure you have the gzip extension
2324
* loaded.
25+
* @param string $apiVersion The version of the API to make requests for.
2426
*/
2527
public function __construct(
2628
string $token,
2729
string $secret,
2830
?string $host = null,
2931
string $httpVersion = '1.1',
30-
array $encoding = []
32+
array $encoding = [],
33+
string $apiVersion = 'v2'
3134
) {
3235
$this->token = $token;
3336
$this->secret = $secret;
3437
$this->host = $host;
3538
$this->httpVersion = $httpVersion;
3639
$this->encoding = $encoding;
40+
$this->apiVersion = $apiVersion;
3741
}
3842

3943
/**
@@ -50,12 +54,24 @@ public static function build(array $configs): self
5054
throw new CarApiException('Missing token and/or secret');
5155
}
5256

57+
$validVersions = ['v1', 'v2'];
58+
if (isset($configs['apiVersion']) && !in_array($configs['apiVersion'], $validVersions)) {
59+
throw new CarApiException(
60+
sprintf(
61+
'Invalid API version. Must be one of (%s) but was given: %s',
62+
implode(', ', $validVersions),
63+
$configs['apiVersion']
64+
)
65+
);
66+
}
67+
5368
return new self(
5469
$configs['token'],
5570
$configs['secret'],
5671
$configs['host'] ?? null,
5772
$configs['httpVersion'] ?? '1.1',
5873
$configs['encoding'] ?? [],
74+
$configs['apiVersion'] ?? 'v2',
5975
);
6076
}
6177
}

test.php

Lines changed: 63 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -27,88 +27,91 @@ function println(string $string) {
2727
echo "\n\n$string\n\n";
2828
}
2929

30-
$sdk = CarApi::build([
31-
'token' => $env['TOKEN'],
32-
'secret' => $env['SECRET'],
33-
'host' => $env['HOST'],
34-
'httpVersion' => '1.1',
35-
'encoding' => ['gzip'],
36-
]);
30+
foreach (['v1', 'v2'] as $apiVersion) {
31+
$sdk = CarApi::build([
32+
'token' => $env['TOKEN'],
33+
'secret' => $env['SECRET'],
34+
'host' => $env['HOST'],
35+
'httpVersion' => '1.1',
36+
'encoding' => ['gzip'],
37+
'apiVersion' => $apiVersion,
38+
]);
3739

38-
println('JWT:' . $sdk->authenticate());
40+
println('JWT:' . $sdk->authenticate());
3941

40-
println('Years:');
41-
print_r($sdk->years(['query' => ['make' => 'Tesla']]));
42+
println('Years:');
43+
print_r($sdk->years(['query' => ['make' => 'Tesla']]));
4244

43-
println('Makes:');
44-
print_r($sdk->makes(['query' => ['limit' => 1, 'page' => 0]]));
45+
println('Makes:');
46+
print_r($sdk->makes(['query' => ['limit' => 1, 'page' => 0]]));
4547

46-
println('Models:');
47-
print_r($sdk->models(['query' => ['make' => 'Tesla', 'limit' => 1]]));
48+
println('Models:');
49+
print_r($sdk->models(['query' => ['make' => 'Tesla', 'limit' => 1]]));
4850

49-
println('Trims:');
50-
$json = new JsonSearch();
51-
$json->addItem(new JsonSearchItem('make', 'like', 'Tesla'));
52-
print_r($sdk->trims(['query' => ['json' => $json, 'limit' => 1]]));
51+
println('Trims:');
52+
$json = new JsonSearch();
53+
$json->addItem(new JsonSearchItem('make', 'like', 'Tesla'));
54+
print_r($sdk->trims(['query' => ['json' => $json, 'limit' => 1]]));
5355

54-
println('Trims:');
55-
print_r($sdk->trimItem(1));
56+
println('Trims:');
57+
print_r($sdk->trimItem(1));
5658

57-
println('Bodies:');
58-
print_r($sdk->bodies(['query' => ['make' => 'Tesla', 'limit' => 1]]));
59+
println('Bodies:');
60+
print_r($sdk->bodies(['query' => ['make' => 'Tesla', 'limit' => 1]]));
5961

60-
println('Engines:');
61-
print_r($sdk->engines(['query' => ['make' => 'Tesla', 'limit' => 1]]));
62+
println('Engines:');
63+
print_r($sdk->engines(['query' => ['make' => 'Tesla', 'limit' => 1]]));
6264

63-
println('Mileages:');
64-
print_r($sdk->mileages(['query' => ['make' => 'Tesla', 'limit' => 1]]));
65+
println('Mileages:');
66+
print_r($sdk->mileages(['query' => ['make' => 'Tesla', 'limit' => 1]]));
6567

66-
println('VIN:');
67-
print_r($sdk->vin('1GTG6CEN0L1139305'));
68+
println('VIN:');
69+
print_r($sdk->vin('1GTG6CEN0L1139305'));
6870

69-
println('Interior Colors:');
70-
print_r($sdk->interiorColors(['query' => ['make' => 'Tesla', 'limit' => 1]]));
71+
println('Interior Colors:');
72+
print_r($sdk->interiorColors(['query' => ['make' => 'Tesla', 'limit' => 1]]));
7173

72-
println('Exterior Colors:');
73-
print_r($sdk->exteriorColors(['query' => ['make' => 'Tesla', 'limit' => 1]]));
74+
println('Exterior Colors:');
75+
print_r($sdk->exteriorColors(['query' => ['make' => 'Tesla', 'limit' => 1]]));
7476

75-
println('Vehicle Attributes:');
76-
print_r($sdk->vehicleAttributes('bodies.type'));
77+
println('Vehicle Attributes:');
78+
print_r($sdk->vehicleAttributes('bodies.type'));
7779

78-
println('Account Requests:');
79-
print_r($sdk->accountRequests());
80+
println('Account Requests:');
81+
print_r($sdk->accountRequests());
8082

81-
/*println('Account Requests Today:');
82-
print_r($sdk->accountRequestsToday());*/
83+
/*println('Account Requests Today:');
84+
print_r($sdk->accountRequestsToday());*/
8385

84-
println('License Plate:');
85-
print_r($sdk->licensePlate('US', 'LNP8460#TEST', 'NY'));
86+
println('License Plate:');
87+
print_r($sdk->licensePlate('US', 'LNP8460#TEST', 'NY'));
8688

87-
println('OBD Codes:');
88-
print_r($sdk->obdCodes(['query' => ['limit' => 1]]));
89+
println('OBD Codes:');
90+
print_r($sdk->obdCodes(['query' => ['limit' => 1]]));
8991

90-
println('Single OBD Code:');
91-
print_r($sdk->obdCodeItem('B1200'));
92+
println('Single OBD Code:');
93+
print_r($sdk->obdCodeItem('B1200'));
9294

93-
println('Done with Vehicles!');
95+
println('Done with Vehicles!');
9496

95-
$sdk = Powersports::build([
96-
'token' => $env['TOKEN'],
97-
'secret' => $env['SECRET'],
98-
'host' => 'http://localhost:8080',
99-
'httpVersion' => '1.1',
100-
'encoding' => ['gzip'],
101-
]);
97+
$sdk = Powersports::build([
98+
'token' => $env['TOKEN'],
99+
'secret' => $env['SECRET'],
100+
'host' => 'http://localhost:8080',
101+
'httpVersion' => '1.1',
102+
'encoding' => ['gzip'],
103+
]);
102104

103-
println('JWT:' . $sdk->authenticate());
105+
println('JWT:' . $sdk->authenticate());
104106

105-
println('Years:');
106-
print_r($sdk->years(['query' => ['make' => 'Honda', 'type' => 'street_motorcycle']]));
107+
println('Years:');
108+
print_r($sdk->years(['query' => ['make' => 'Honda', 'type' => 'street_motorcycle']]));
107109

108-
println('Makes:');
109-
print_r($sdk->makes(['query' => ['limit' => 1, 'page' => 0, 'type' => 'street_motorcycle']]));
110+
println('Makes:');
111+
print_r($sdk->makes(['query' => ['limit' => 1, 'page' => 0, 'type' => 'street_motorcycle']]));
110112

111-
println('Models:');
112-
print_r($sdk->models(['query' => ['make' => 'Honda', 'limit' => 1, 'type' => 'street_motorcycle']]));
113+
println('Models:');
114+
print_r($sdk->models(['query' => ['make' => 'Honda', 'limit' => 1, 'type' => 'street_motorcycle']]));
113115

114-
println('Done with Powersports!');
116+
println('Done with Powersports!');
117+
}

0 commit comments

Comments
 (0)