Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianFun123 committed Aug 24, 2022
0 parents commit 85b5920
Show file tree
Hide file tree
Showing 16 changed files with 562 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.idea
/modules
.fuse_*
80 changes: 80 additions & 0 deletions README.md
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());
```
44 changes: 44 additions & 0 deletions autoload.php
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;
}

};
19 changes: 19 additions & 0 deletions composer.json
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"
}
}
}
67 changes: 67 additions & 0 deletions src/main/de/interaapps/ulole/httpclient/HttpClient.php
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;
60 changes: 60 additions & 0 deletions src/main/de/interaapps/ulole/httpclient/HttpRequest.php
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;
}

}
43 changes: 43 additions & 0 deletions src/main/de/interaapps/ulole/httpclient/HttpResponse.php
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;
}
}
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 {

}
Loading

0 comments on commit 85b5920

Please sign in to comment.