-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 85b5920
Showing
16 changed files
with
562 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/.idea | ||
/modules | ||
.fuse_* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Ulole HTTP Client `1.0` | ||
## Installation | ||
Module | ||
#### UPPM | ||
``` | ||
uppm i interaapps/ulole-http-client | ||
``` | ||
#### Composer | ||
``` | ||
composer require interaapps/ulole-http-client | ||
``` | ||
|
||
## Getting started | ||
```php | ||
$client = new HttpClient("https://ping.intera.dev"); | ||
|
||
$authors = $client->get("/authors") | ||
->send() | ||
->json(); | ||
|
||
foreach ($authors as $author) { | ||
echo $author->name . "\n"; | ||
} | ||
|
||
$success = $client->post("/authors", [ | ||
"name" => "Author" | ||
]) | ||
->bearer("HelloWorld") | ||
->send() | ||
->ok(); | ||
|
||
if ($success) { | ||
echo "Done!"; | ||
} | ||
|
||
|
||
|
||
``` | ||
|
||
|
||
## Request-Settings | ||
On a HttpRequest or for all HttpRequests on the HttpClient you can use some methods which will change some request options. | ||
```php | ||
|
||
$request = $client->get("https://google.com"); | ||
|
||
$request->header("X-Header", "Value"); | ||
$request->bearer("ABCDE"); | ||
|
||
// Set Query Parameter | ||
$request->query("key", "value"); | ||
|
||
// Set Body | ||
$request->body("this-is-the-body=yey"); | ||
|
||
// Set Json Body | ||
$request->json(["hello" => "world"]); | ||
|
||
|
||
$request->timeout(150); | ||
|
||
$request->followRedirects(); | ||
$request->notFollowRedirects(); | ||
|
||
|
||
$request->formData([ | ||
"file" => new CURLFile("file.txt") | ||
]); | ||
|
||
$response = $request->send(); | ||
|
||
var_dump($response->json()); | ||
// From json model | ||
var_dump($response->json(User::class)); | ||
|
||
var_dump($response->header("content-type")); | ||
var_dump($response->body()); | ||
var_dump($response->status()); | ||
var_dump($response->ok()); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
return function ($dir = ".", $mod = "main") { | ||
$uppmlock = (object) []; | ||
if (file_exists("uppm.locks.json")) { | ||
$uppmlock = json_decode(file_get_contents($dir . "/" . "uppm.locks.json")); | ||
if (isset($uppmlock->namespace_bindings)) | ||
$uppmlock->namespaceBindings = $uppmlock->namespace_bindings; | ||
if (isset($uppmlock->directnamespaces)) | ||
$uppmlock->directNamespaceBindings = $uppmlock->directnamespaces; | ||
} | ||
|
||
spl_autoload_register(function($class) use ($dir, $mod, $uppmlock) { | ||
if (isset($uppmlock->directNamespaceBindings->{$class})) | ||
@include_once "$dir/".str_replace("\\","/",$uppmlock->directNamespaceBindings->{$class}); | ||
else if(file_exists("$dir/".str_replace("\\","/",$class).".php")) | ||
@include_once "$dir/".str_replace("\\","/",$class).".php"; | ||
else if(file_exists("$dir/modules/".str_replace("\\","/",$class).".php")) | ||
@include_once "$dir/modules/".str_replace("\\","/",$class).".php"; | ||
else if(file_exists("$dir/src/".str_replace("\\","/",$class).".php")) | ||
@include_once "$dir/src/".str_replace("\\","/",$class).".php"; | ||
else if(file_exists("$dir/src/$mod/".str_replace("\\","/",$class).".php")) | ||
@include_once "$dir/src/$mod/".str_replace("\\","/",$class).".php"; | ||
else if(isset($uppmlock->namespaceBindings)) { | ||
foreach ($uppmlock->namespaceBindings as $namespaceBinding => $folder){ | ||
if (substr($class, 0, strlen($namespaceBinding)) === $namespaceBinding) { | ||
$splitClass = explode($namespaceBinding."\\", $class, 2); | ||
if (isset($splitClass[1]) && $splitClass[1] != "") | ||
$class = $splitClass[1]; | ||
$classFile = $folder.'/'.str_replace("\\","/", $class).".php"; | ||
if (file_exists($dir."/".$classFile)) { | ||
@include_once $dir."/".$classFile; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
|
||
if (isset($uppmlock->initscripts)) | ||
foreach ($uppmlock->initscripts as $script) { | ||
@include_once $script; | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "interaapps/ulole-http-client", | ||
"version": "1.0.0", | ||
"type": "library", | ||
"authors": [ | ||
{ | ||
"name": "JulianFun123", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=8.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"de\\interaapps\\ulole\\module\\": "src/main/de/interaapps/ulole/httpclient" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
namespace de\interaapps\ulole\httpclient; | ||
|
||
use de\interaapps\jsonplus\JSONPlus; | ||
use de\interaapps\ulole\httpclient\http\CURLHttpAdapter; | ||
use de\interaapps\ulole\httpclient\http\HttpAdapter; | ||
use de\interaapps\ulole\httpclient\traits\HasRequestSettings; | ||
|
||
class HttpClient { | ||
use HasRequestSettings; | ||
|
||
public static HttpAdapter $adapterDefault; | ||
public static JSONPlus $jsonPlusDefault; | ||
|
||
private HttpAdapter $adapter; | ||
private string $baseUrl = ""; | ||
public JSONPlus $jsonPlus; | ||
|
||
public function __construct() { | ||
} | ||
|
||
public function request(string $method, string $url): HttpRequest { | ||
$this->adapter = HttpClient::$adapterDefault; | ||
$this->jsonPlus = HttpClient::$jsonPlusDefault; | ||
|
||
$req = new HttpRequest($this, $this->baseUrl . $method, $url); | ||
$req->headers($this->headers); | ||
$req->queries($this->queryParameters); | ||
$req->timeout($this->timeout); | ||
return $req; | ||
} | ||
|
||
public function get(string $url, array|null $query = null): HttpRequest { | ||
return $this->request("GET", $url)->queries($query); | ||
} | ||
|
||
public function delete(string $url, array|null $query = null): HttpRequest { | ||
return $this->request("DELETE", $url)->queries($query); | ||
} | ||
|
||
public function head(string $url, array|null $query = null): HttpRequest { | ||
return $this->request("HEAD", $url)->queries($query); | ||
} | ||
|
||
public function post(string $url, mixed $body = null): HttpRequest { | ||
return $this->request("POST", $url)->json($body); | ||
} | ||
|
||
public function put(string $url, mixed $body = null): HttpRequest { | ||
return $this->request("POST", $url)->json($body); | ||
} | ||
|
||
public function patch(string $url, mixed $body = null): HttpRequest { | ||
return $this->request("PATCH", $url)->json($body); | ||
} | ||
|
||
public function getAdapter(): HttpAdapter { | ||
return $this->adapter; | ||
} | ||
|
||
public function getJsonPlus(): JSONPlus { | ||
return $this->jsonPlus; | ||
} | ||
} | ||
|
||
HttpClient::$adapterDefault = new CURLHttpAdapter(); | ||
HttpClient::$jsonPlusDefault = JSONPlus::$default; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
namespace de\interaapps\ulole\httpclient; | ||
|
||
use de\interaapps\ulole\httpclient\traits\HasRequestSettings; | ||
|
||
class HttpRequest { | ||
use HasRequestSettings; | ||
|
||
private mixed $body = null; | ||
|
||
public function __construct( | ||
private HttpClient $client, | ||
private string $method, | ||
private string $url | ||
) { | ||
} | ||
|
||
/** | ||
* @throws exceptions\HttpException | ||
*/ | ||
public function send(): HttpResponse { | ||
$url = $this->url; | ||
if (count($this->queryParameters) > 0) | ||
$this->url .= '?' . http_build_query($this->queryParameters); | ||
$res = $this->client->getAdapter()->do($this->client, $this); | ||
$this->url = $url; | ||
return $res; | ||
} | ||
|
||
|
||
public function getMethod(): string { | ||
return $this->method; | ||
} | ||
|
||
public function getUrl(): string { | ||
return $this->url; | ||
} | ||
|
||
public function body(mixed $body): HttpRequest { | ||
$this->body = $body; | ||
return $this; | ||
} | ||
|
||
public function formData(array $postFields): HttpRequest { | ||
$this->body = $postFields; | ||
$this->header("Content-Type", "multipart/form-data"); | ||
return $this; | ||
} | ||
|
||
public function json(mixed $m): HttpRequest { | ||
$this->body = $this->client->getJsonPlus()->toJson($m); | ||
$this->header("Content-Type", "application/json"); | ||
return $this; | ||
} | ||
|
||
public function getBody(): mixed { | ||
return $this->body; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace de\interaapps\ulole\httpclient; | ||
|
||
class HttpResponse { | ||
|
||
public function __construct( | ||
private HttpClient $client, | ||
private int $status, | ||
private mixed $body, | ||
private array $headers | ||
) { | ||
} | ||
|
||
public function body(): mixed { | ||
return $this->body; | ||
} | ||
|
||
public function json(string|null $type = null): mixed { | ||
return $this->client->getJsonPlus()->fromJson($this->body(), $type); | ||
} | ||
|
||
public function status(): int { | ||
return $this->status; | ||
} | ||
|
||
|
||
public function ok(): bool { | ||
return $this->status >= 200 && $this->status < 400; | ||
} | ||
|
||
public function header(string $key): mixed { | ||
return $this->headers[$key]; | ||
} | ||
|
||
public function headers(): array { | ||
return $this->headers; | ||
} | ||
|
||
public function getClient(): HttpClient { | ||
return $this->client; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/de/interaapps/ulole/httpclient/exceptions/HttpException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace de\interaapps\ulole\httpclient\exceptions; | ||
|
||
use Exception; | ||
|
||
class HttpException extends Exception { | ||
|
||
} |
Oops, something went wrong.