|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LaravelLoqate\APIs; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Client\PendingRequest; |
|
6
|
|
|
use Illuminate\Http\Client\Response; |
|
7
|
|
|
use Illuminate\Support\Facades\Http; |
|
8
|
|
|
use LaravelLoqate\Responses\AbstractResponse; |
|
9
|
|
|
use LaravelLoqate\Responses\LoqateResponse; |
|
10
|
|
|
|
|
11
|
|
|
abstract class AbstractAPI |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Http client |
|
16
|
|
|
* @var PendingRequest|null |
|
17
|
|
|
*/ |
|
18
|
|
|
protected ?PendingRequest $http = null; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Api key |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected string $key; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Request Fields |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected array $requestParams = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* LoqateApi constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $key |
|
36
|
|
|
*/ |
|
37
|
2 |
|
public function __construct(string $key) |
|
38
|
|
|
{ |
|
39
|
2 |
|
$this->key = $key; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Get api base path for uri |
|
44
|
|
|
* @return string |
|
45
|
|
|
* @example "Capture/Interactive/Find" |
|
46
|
|
|
*/ |
|
47
|
|
|
abstract public function basePath(): string; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get locator for api uri |
|
51
|
|
|
* @return string |
|
52
|
|
|
*/ |
|
53
|
2 |
|
public function url(): string |
|
54
|
|
|
{ |
|
55
|
2 |
|
return rtrim(config('services.loqate.url', 'https://api.addressy.com/'), '/') . '/'; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get json endpoint for api uri |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
2 |
|
public function jsonEndpoint(): string |
|
63
|
|
|
{ |
|
64
|
2 |
|
return '/' . ltrim(config('services.loqate.jsonEndpoint', '/json3.ws'), '/'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get api uri |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
2 |
|
public function uri(): string |
|
72
|
|
|
{ |
|
73
|
2 |
|
return $this->url() . trim($this->basePath(), '/') . $this->jsonEndpoint(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return PendingRequest |
|
78
|
|
|
*/ |
|
79
|
2 |
|
protected function http(): PendingRequest |
|
80
|
|
|
{ |
|
81
|
2 |
|
if (!$this->http) { |
|
82
|
2 |
|
$this->http = Http::acceptJson(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
2 |
|
return $this->http; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Get Response class |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function responseClass(): string |
|
93
|
|
|
{ |
|
94
|
|
|
return LoqateResponse::class; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Dynamically proxy other methods to the underlying response. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $method |
|
101
|
|
|
* @param array $parameters |
|
102
|
|
|
* |
|
103
|
|
|
* @return mixed |
|
104
|
|
|
*/ |
|
105
|
|
|
public function __call($method, $parameters) |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->http()->{$method}(...$parameters); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param string $key |
|
112
|
|
|
* @param $value |
|
113
|
|
|
* |
|
114
|
|
|
* @return $this |
|
115
|
|
|
*/ |
|
116
|
2 |
|
public function setRequestField(string $key, $value): self |
|
117
|
|
|
{ |
|
118
|
2 |
|
$this->requestParams[ $key ] = $value; |
|
119
|
|
|
|
|
120
|
2 |
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return AbstractResponse |
|
125
|
|
|
*/ |
|
126
|
2 |
|
public function call(): AbstractResponse |
|
127
|
|
|
{ |
|
128
|
2 |
|
$class = $this->responseClass(); |
|
129
|
|
|
|
|
130
|
2 |
|
return new $class($this->rawCall()); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @return Response |
|
135
|
|
|
*/ |
|
136
|
2 |
|
public function rawCall(): Response |
|
137
|
|
|
{ |
|
138
|
2 |
|
return $this->http()->get( |
|
139
|
2 |
|
$this->uri(), |
|
140
|
2 |
|
array_merge($this->requestParams, [ |
|
141
|
2 |
|
'Key' => $this->key, |
|
142
|
2 |
|
]) |
|
143
|
2 |
|
); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|