Skip to content

Commit 02cfc34

Browse files
Support for calling APIs based on REST standards
1. HttpConnector.request to make requests using the HttpRequest Object to get back a HttpResponse object 2. Construction of HttpRequest object using "Fluent Interface" which makes construction of requests and calling the requests easy for the developer 3. Static methods defined in such a way that request method is never invalid and the construstion of object is done statically, which makes it possible to write one liners for making an api call.
1 parent 55e7711 commit 02cfc34

File tree

5 files changed

+210
-0
lines changed

5 files changed

+210
-0
lines changed

composer.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "encryptorcode/php-http-client",
3+
"description": "Yet another http client made to support fluent interface for all REST APIs...",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Abhay Jatin Doshi",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {},
13+
"autoload": {
14+
"psr-4": {
15+
"encryptorcode\\httpclient\\": "src/"
16+
}
17+
}
18+
}

src/HttpConnector.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
namespace encryptorcode\httpclient;
3+
4+
class HttpConnector{
5+
public static function request(HttpRequest $request) : HttpResponse{
6+
$fullUrl = $request->getUrl();
7+
$queryParams = $request->getParams();
8+
if(isset($queryParams)){
9+
$firstParam = true;
10+
foreach ($queryParams as $key => $value) {
11+
if($firstParam){
12+
$firstParam = false;
13+
$fullUrl .= '?';
14+
} else {
15+
$fullUrl .= '&';
16+
}
17+
$fullUrl .= urlencode($key) . "=" . urlencode($value);
18+
}
19+
}
20+
21+
$ch = curl_init();
22+
curl_setopt($ch, CURLOPT_URL, $fullUrl);
23+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
24+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request->getMethod());
25+
26+
$requestBodyType = $request->getRequestBodyType();
27+
if(isset($requestBodyType)){
28+
curl_setopt($ch, CURLOPT_POSTFIELDS, $request->getBody());
29+
}
30+
31+
$headersArray = array();
32+
$headers = $request->getHeaders();
33+
if(isset($headers)){
34+
foreach ($headers as $key => $value) {
35+
$headersArray[] = $key . ": " . $value;
36+
}
37+
}
38+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headersArray);
39+
40+
$responseBody = curl_exec($ch);
41+
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
42+
curl_close($ch);
43+
44+
$response = new HttpResponse($responseCode, $responseBody);
45+
return $response;
46+
}
47+
}

src/HttpException.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
namespace encryptorcode\httpclient;
3+
4+
class HttpException extends Exception{
5+
6+
}

src/HttpRequest.php

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
namespace encryptorcode\httpclient;
3+
4+
class HttpRequest{
5+
private $method;
6+
private $url;
7+
private $requestBodyType;
8+
private $params;
9+
private $headers;
10+
private $body;
11+
12+
public static function get($url) : HttpRequest{
13+
return new HttpRequest("GET",$url);
14+
}
15+
16+
public static function post($url) : HttpRequest{
17+
return new HttpRequest("POST",$url);
18+
}
19+
20+
public static function put($url) : HttpRequest{
21+
return new HttpRequest("PUT",$url);
22+
}
23+
24+
public static function patch($url) : HttpRequest{
25+
return new HttpRequest("PATCH",$url);
26+
}
27+
28+
public static function delete($url) : HttpRequest{
29+
return new HttpRequest("PATCH",$url);
30+
}
31+
32+
private function __construct($method, $url){
33+
$this->method = $method;
34+
$this->url = $url;
35+
}
36+
37+
public function param($key, $value) : HttpRequest{
38+
$this->params[$key] = $value;
39+
return $this;
40+
}
41+
42+
public function header($key, $value) : HttpRequest{
43+
$this->headers[$key] = $value;
44+
return $this;
45+
}
46+
47+
public function formParam($key, $value) : HttpRequest{
48+
if($this->method == "GET"){
49+
throw new HttpException("FORM_DATA not supported for method ".$this->method);
50+
}
51+
52+
if(isset($this->requestBodyType)){
53+
if($this->requestBodyType != "FORM_DATA"){
54+
throw new HttpException("Request already has a body of type ".$this->requestBodyType);
55+
}
56+
} else {
57+
$this->requestBodyType = "FORM_DATA";
58+
$this->body = array();
59+
}
60+
61+
$this->body[$key] = $value;
62+
return $this;
63+
}
64+
65+
public function jsonData($data) : HttpRequest{
66+
if($this->method == "GET"){
67+
throw new HttpException("JSON_BODY not supported for method ".$this->method);
68+
}
69+
70+
if(isset($this->requestBodyType)){
71+
if($this->requestBodyType != "JSON_BODY"){
72+
throw new HttpException("Request already has a body of type ".$this->requestBodyType);
73+
}
74+
}
75+
76+
if(gettype($data) !== "string"){
77+
$this->body = json_encode($data);
78+
} else {
79+
$this->body = $data;
80+
}
81+
82+
$this->requestBodyType = "JSON_BODY";
83+
$this->headers["Content-Type"] = "application/json";
84+
return $this;
85+
}
86+
87+
public function getResponse() : HttpResponse{
88+
return HttpConnector::request($this);
89+
}
90+
91+
public function getParams() : ?array{
92+
return $this->params;
93+
}
94+
95+
public function getHeaders() : ?array{
96+
return $this->headers;
97+
}
98+
99+
public function getMethod() : string{
100+
return $this->method;
101+
}
102+
103+
public function getUrl() : string{
104+
return $this->url;
105+
}
106+
107+
public function getRequestBodyType() : ?string{
108+
return $this->requestBodyType;
109+
}
110+
111+
public function getBody(){
112+
return $this->body;
113+
}
114+
115+
}

src/HttpResponse.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
namespace encryptorcode\httpclient;
3+
4+
class HttpResponse{
5+
private $status;
6+
private $body;
7+
8+
public function __construct(int $status, string $body){
9+
$this->status = $status;
10+
$this->body = $body;
11+
}
12+
13+
public function getStatus() : int {
14+
return $this->status;
15+
}
16+
17+
public function getBody() : string {
18+
return $this->body;
19+
}
20+
21+
public function getJsonBody() : array {
22+
return json_decode($this->body);
23+
}
24+
}

0 commit comments

Comments
 (0)