-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRequest.php
More file actions
152 lines (119 loc) · 4.17 KB
/
Copy pathRequest.php
File metadata and controls
152 lines (119 loc) · 4.17 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
namespace Scomrie\Baserow;
class Request
{
/**
* @var Baserow Instance of Baserow
*/
private $baserow;
/**
* @var resource Instance of CURL
*/
private $curl;
/**
* @var string Content type
*/
private $content_type;
/**
* @var array Request data
*/
private $data = [];
/**
* @var bool Is it a POST request?
*/
private $is_post = false;
private $response;
/**
* Create a Request to Baserow API
* @param Baserow $baserow Instance of Baserow
* @param string $content_type Content type
* @param array $data Request data
* @param bool|string $is_post Is it a POST request?
*/
public function __construct( $baserow, $content_type, $data = [], $is_post = false )
{
$this->baserow = $baserow;
$this->content_type = $content_type;
$this->data = $data;
$this->is_post = $is_post;
}
private function init()
{
$headers = array(
'Content-Type: application/json',
sprintf('Authorization: Token %s', $this->baserow->getKey())
);
$request = $this->content_type;
$request_parts = explode( '/', $request );
$request_parts = array_map( 'rawurlencode', $request_parts );
$request = join( '/', $request_parts ) . '/';
$url_encoded = false;
if( ! $this->is_post || strtolower( $this->is_post ) === 'delete' )
{
if (!empty($this->data)){
$data = http_build_query($this->data);
$request .= "?" . $data;
}
$url_encoded = true;
}
$curl = curl_init($this->baserow->getApiUrl($request));
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
if( $this->is_post )
{
$postData = [];
$postType = strtoupper( $this->is_post );
curl_setopt($curl,CURLOPT_POST, true);
if( in_array( $postType, ['PATCH', 'DELETE'] ) ) {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $postType );
}
// POST & PATCH send a payload that needs to be modified
if( $postType != 'DELETE' )
{
// make sure any straight integer values in the data array
// are reformatted using (int) which prevents json_encode() from
// wrapping them in quotes, since baserow interprets integers
// wrapped in quotes as strings, which breaks updating select
// fields by option id. this is a preferable method to using
// JSON_NUMERIC_CHECK as there are some legacy issues with the
// way that flag tests and validates numbers
$postData = $this->data;
foreach( $postData AS $key => $value ) {
if( is_numeric($value) && preg_match("/^\d+$/", $value ) ) {
$postData[$key] = (int) $value;
}
if( is_array($value) && !empty($value) ) {
$cleanValues = [];
foreach( $value AS $arrayVal ) {
$cleanValues[] =
( is_numeric($arrayVal) && preg_match("/^\d+$/", $arrayVal ) )
? (int) $arrayVal
: $arrayVal;
}
$postData[$key] = empty($cleanValues) ? $value : $cleanValues;
}
}
}
curl_setopt($curl,CURLOPT_POSTFIELDS, json_encode($postData));
}
$this->curl = $curl;
}
/**
* @return Response Get response from API
*/
public function getResponse()
{
$this->init();
$response_string = curl_exec( $this->curl );
$this->response = new Response( $this->baserow, $this, $response_string );
return $this->response;
}
public function __set( $key, $value )
{
if( ! is_array( $this->data ) )
{
$this->data = [];
}
$this->data[ $key ] = $value;
}
}