Skip to content

Commit 187c34b

Browse files
committed
feat: improve http client
1 parent 0725de2 commit 187c34b

1 file changed

Lines changed: 41 additions & 22 deletions

File tree

src/Http/Client.php

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ public function get(string $route, array $header = null)
4242
try {
4343
return json_decode(self::$http->request('GET', $route, ['headers' => $header])->getBody()->getContents());
4444
} catch (GuzzleException $e) {
45-
if ($e->getCode() == 401) {
46-
throw new ExpiredJwtException();
47-
} else if ($e->getCode() == 404) {
48-
throw new NotFoundException();
45+
if ($e->getCode() === 401) {
46+
throw new ExpiredJwtException($e->getMessage());
47+
} else if ($e->getCode() === 404) {
48+
throw new NotFoundException($e->getMessage());
4949
}
50-
throw new GenericException($e);
50+
throw new GenericException($e->getMessage());
5151
}
5252

5353
}
@@ -66,38 +66,42 @@ public function patch(string $route, array $header, $json)
6666
try {
6767
return self::$http->patch($route, ['headers' => $header, 'body' => $json])->getStatusCode();
6868
} catch (GuzzleException $e) {
69-
if ($e->getCode() == 401)
70-
throw new ExpiredJwtException();
71-
else if ($e->getCode() == 404)
69+
if ($e->getCode() === 401) {
70+
throw new ExpiredJwtException($e->getMessage());
71+
}
72+
73+
if ($e->getCode() === 404) {
7274
return null;
73-
throw new GenericException($e);
75+
}
76+
77+
throw new GenericException($e->getMessage());
7478
}
7579
}
7680

7781
/**
78-
* Generic function to send HTTP PATCH requests
82+
* Generic function to send HTTP POST requests
7983
*
8084
* @param string $route
8185
* @param array $header
8286
* @param $json
8387
* @return mixed|null
8488
* @throws \Exception
8589
*/
86-
public function post(string $route, array $header, $json)
90+
public function post(string $route, array $json, array $header = ['X-Requested-With' => 'XMLHttpRequest', 'Content-Type' => 'application/json'])
8791
{
8892
try {
89-
return self::$http->patch($route, ['headers' => $header, 'body' => $json])->getStatusCode();
93+
return json_decode(self::$http->request('POST', $route, ['headers' => $header, 'json' => $json])->getBody()->getContents());
9094
} catch (GuzzleException $e) {
91-
if ($e->getCode() == 401)
92-
throw new ExpiredJwtException();
93-
else if ($e->getCode() == 404)
94-
return null;
95-
throw new GenericException($e);
95+
if ($e->getCode() === 401) {
96+
throw new ExpiredJwtException($e->getMessage());
97+
}
98+
99+
throw new GenericException($e->getMessage());
96100
}
97101
}
98102

99103
/**
100-
* Generic function to send HTTP PATCH requests
104+
* Generic function to send HTTP DELETE requests
101105
*
102106
* @param String $route
103107
* @param array $header
@@ -109,11 +113,26 @@ public function delete(String $route, array $header)
109113
try {
110114
return self::$http->delete($route, ['headers' => $header])->getStatusCode();
111115
} catch (GuzzleException $e) {
112-
if ($e->getCode() == 401)
113-
throw new ExpiredJwtException();
114-
else if ($e->getCode() == 404)
116+
if ($e->getCode() === 401) {
117+
throw new ExpiredJwtException($e->getMessage());
118+
}
119+
120+
if ($e->getCode() === 404) {
115121
return null;
116-
throw new GenericException($e);
122+
}
123+
throw new GenericException($e->getMessage());
117124
}
118125
}
126+
127+
/**
128+
* Guzzle Legacy POST
129+
*
130+
* @param $route
131+
* @param $args
132+
* @return \Psr\Http\Message\ResponseInterface
133+
*/
134+
public function legacyPost($route, $args)
135+
{
136+
return self::$http->post($route, $args);
137+
}
119138
}

0 commit comments

Comments
 (0)