Skip to content

Commit f7edeea

Browse files
committed
restclient: added util functions
1 parent 05edaf9 commit f7edeea

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/API.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ public function login($username, $password, $uuid = null)
4545
}
4646

4747
/**
48+
* setAuthToken overrides the auth token
49+
*
50+
* @param string $token Auth-Token, when you want to override
51+
*/
52+
public function setAuthToken($token)
53+
{
54+
$this->token = $token;
55+
}
56+
/**
4857
* getFiles returns the files for the user account
4958
*
5059
* @return array files

src/RestClient.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ class RestClient
3333
*/
3434
protected $url = null;
3535
/**
36+
* headers are http headers which will be added on every request
37+
* @var array
38+
*/
39+
private $headers = [];
40+
/**
3641
* request executes a request to the SinusBot API
3742
*
3843
* @param string $path /api/v1/<path>
@@ -46,11 +51,11 @@ protected function request($path, $method = "GET", $payload = null, $encoded = f
4651
$ch = curl_init();
4752
curl_setopt_array($ch, [
4853
CURLOPT_URL => $this->url.'/api/v1'.$path,
49-
CURLOPT_HTTPHEADER => [
54+
CURLOPT_HTTPHEADER => array_merge([
5055
"Accept: application/json, text/plain, */*",
5156
"Content-Type: application/json",
5257
"Authorization: Bearer ".$this->token
53-
],
58+
], $this->headers),
5459
CURLOPT_CUSTOMREQUEST => $method,
5560
CURLOPT_RETURNTRANSFER => true,
5661
CURLOPT_SSL_VERIFYHOST => false,
@@ -76,6 +81,18 @@ protected function request($path, $method = "GET", $payload = null, $encoded = f
7681

7782

7883
/**
84+
* addHeader adds a header to every http request
85+
*
86+
* @param string $key http header key
87+
* @param string $value http header value
88+
*/
89+
public function addHeader($key, $value)
90+
{
91+
array_push($this->headers, [
92+
$key => $value,
93+
]);
94+
}
95+
/**
7996
* getError returns the string representive to the given http status code
8097
*
8198
* @param integer $code http status code

0 commit comments

Comments
 (0)