-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpRequest.php
More file actions
115 lines (91 loc) · 2.45 KB
/
Copy pathHttpRequest.php
File metadata and controls
115 lines (91 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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;
}
}