44 * An unofficial PHP client library for accessing the official Todoist REST API.
55 *
66 * @author Fabian Beiner <[email protected] > 7- * @license MIT
7+ * @license https://opensource.org/licenses/MIT MIT
8+ * @version 0.6.0 <2018-03-06>
89 * @link https://github.com/FabianBeiner/Todoist-PHP-API-Library
9- * @version 0.5.0 <2018-03-02>
1010 */
1111
1212namespace FabianBeiner \Todoist ;
@@ -40,11 +40,6 @@ class Todoist
4040 */
4141 private $ client ;
4242
43- /**
44- * @var string Default URL query.
45- */
46- private $ tokenQuery ;
47-
4843 /**
4944 * Todoist constructor.
5045 *
@@ -60,23 +55,30 @@ public function __construct($apiToken)
6055 }
6156 $ this ->apiToken = trim ($ apiToken );
6257
63- // Create a default query for the token.
64- $ this ->tokenQuery = http_build_query ([
65- 'token ' => $ this ->apiToken
66- ],
67- null ,
68- '& ' ,
69- PHP_QUERY_RFC3986 );
70-
7158 // Create a Guzzle client.
7259 $ this ->client = new Client ([
7360 'base_uri ' => $ this ->restApiUrl ,
74- 'headers ' => [ ' X-Request-Id ' => $ this ->generateV4GUID ()] ,
61+ 'headers ' => $ this ->createHeaders () ,
7562 'http_errors ' => false ,
7663 'timeout ' => 5
7764 ]);
7865 }
7966
67+ /**
68+ * Helper function (introduced by @balazscsaba2006), to easily define
69+ * Guzzle (cURL) headers.
70+ *
71+ * @return array Guzzle headers.
72+ */
73+ protected function createHeaders (): array
74+ {
75+ return [
76+ 'Authorization ' => sprintf ('Bearer %s ' , $ this ->apiToken ),
77+ 'X-Request-Id ' => $ this ->generateV4GUID (),
78+ 'Accept-Encoding ' => 'gzip '
79+ ];
80+ }
81+
8082 /**
8183 * Generate a v4 GUID string.
8284 *
@@ -85,7 +87,7 @@ public function __construct($apiToken)
8587 *
8688 * @return string A v4 GUID.
8789 */
88- private function generateV4GUID ()
90+ private function generateV4GUID (): string
8991 {
9092 if (function_exists ('com_create_guid ' ) === true ) {
9193 return trim (com_create_guid (), '{} ' );
0 commit comments