Skip to content

Commit

Permalink
Support for calling APIs based on REST standards
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
abhayjatindoshi committed May 4, 2019
1 parent 55e7711 commit 02cfc34
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 0 deletions.
18 changes: 18 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "encryptorcode/php-http-client",
"description": "Yet another http client made to support fluent interface for all REST APIs...",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Abhay Jatin Doshi",
"email": "ajd1996@hotmail.com"
}
],
"require": {},
"autoload": {
"psr-4": {
"encryptorcode\\httpclient\\": "src/"
}
}
}
47 changes: 47 additions & 0 deletions src/HttpConnector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
namespace encryptorcode\httpclient;

class HttpConnector{
public static function request(HttpRequest $request) : HttpResponse{
$fullUrl = $request->getUrl();
$queryParams = $request->getParams();
if(isset($queryParams)){
$firstParam = true;
foreach ($queryParams as $key => $value) {
if($firstParam){
$firstParam = false;
$fullUrl .= '?';
} else {
$fullUrl .= '&';
}
$fullUrl .= urlencode($key) . "=" . urlencode($value);
}
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $fullUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request->getMethod());

$requestBodyType = $request->getRequestBodyType();
if(isset($requestBodyType)){
curl_setopt($ch, CURLOPT_POSTFIELDS, $request->getBody());
}

$headersArray = array();
$headers = $request->getHeaders();
if(isset($headers)){
foreach ($headers as $key => $value) {
$headersArray[] = $key . ": " . $value;
}
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headersArray);

$responseBody = curl_exec($ch);
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

$response = new HttpResponse($responseCode, $responseBody);
return $response;
}
}
6 changes: 6 additions & 0 deletions src/HttpException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace encryptorcode\httpclient;

class HttpException extends Exception{

}
115 changes: 115 additions & 0 deletions src/HttpRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
namespace encryptorcode\httpclient;

class HttpRequest{
private $method;
private $url;
private $requestBodyType;
private $params;
private $headers;
private $body;

public static function get($url) : HttpRequest{
return new HttpRequest("GET",$url);
}

public static function post($url) : HttpRequest{
return new HttpRequest("POST",$url);
}

public static function put($url) : HttpRequest{
return new HttpRequest("PUT",$url);
}

public static function patch($url) : HttpRequest{
return new HttpRequest("PATCH",$url);
}

public static function delete($url) : HttpRequest{
return new HttpRequest("PATCH",$url);
}

private function __construct($method, $url){
$this->method = $method;
$this->url = $url;
}

public function param($key, $value) : HttpRequest{
$this->params[$key] = $value;
return $this;
}

public function header($key, $value) : HttpRequest{
$this->headers[$key] = $value;
return $this;
}

public function formParam($key, $value) : HttpRequest{
if($this->method == "GET"){
throw new HttpException("FORM_DATA not supported for method ".$this->method);
}

if(isset($this->requestBodyType)){
if($this->requestBodyType != "FORM_DATA"){
throw new HttpException("Request already has a body of type ".$this->requestBodyType);
}
} else {
$this->requestBodyType = "FORM_DATA";
$this->body = array();
}

$this->body[$key] = $value;
return $this;
}

public function jsonData($data) : HttpRequest{
if($this->method == "GET"){
throw new HttpException("JSON_BODY not supported for method ".$this->method);
}

if(isset($this->requestBodyType)){
if($this->requestBodyType != "JSON_BODY"){
throw new HttpException("Request already has a body of type ".$this->requestBodyType);
}
}

if(gettype($data) !== "string"){
$this->body = json_encode($data);
} else {
$this->body = $data;
}

$this->requestBodyType = "JSON_BODY";
$this->headers["Content-Type"] = "application/json";
return $this;
}

public function getResponse() : HttpResponse{
return HttpConnector::request($this);
}

public function getParams() : ?array{
return $this->params;
}

public function getHeaders() : ?array{
return $this->headers;
}

public function getMethod() : string{
return $this->method;
}

public function getUrl() : string{
return $this->url;
}

public function getRequestBodyType() : ?string{
return $this->requestBodyType;
}

public function getBody(){
return $this->body;
}

}
24 changes: 24 additions & 0 deletions src/HttpResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace encryptorcode\httpclient;

class HttpResponse{
private $status;
private $body;

public function __construct(int $status, string $body){
$this->status = $status;
$this->body = $body;
}

public function getStatus() : int {
return $this->status;
}

public function getBody() : string {
return $this->body;
}

public function getJsonBody() : array {
return json_decode($this->body);
}
}

0 comments on commit 02cfc34

Please sign in to comment.