Skip to content

Commit a833ac8

Browse files
Joshua de GierJoshua de Gier
Joshua de Gier
authored and
Joshua de Gier
committed
Change in setup of the class and added a first draft for Wunderlist Files
I'm not sure yet if there will be an abilty to get the Wunderlist Files that are added to tasks using the API. I've set up a quick draft (api.files.class.php) that has only one function. An example can be found in /examples/getFiles.php - for the moment the results are still empty so it's not funtional yet! I've removed the "Call()" function from both classes, adding this to base.class.php, this gave me the abiltiy to use the same function for both classes (Wunderlist and Wunderfiles) as they extend from this class and carry their own protected variables. Also the class variables have been made protected instead of private (otherwise they won't be accesible by the Wunderbase class) and the authtoken variable has been declared public in order to pass it to the Wunderfiles class.
1 parent 838c9da commit a833ac8

File tree

5 files changed

+218
-117
lines changed

5 files changed

+218
-117
lines changed

api.class.php

+9-117
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,22 @@
55
* api.class.php
66
* Purpose: communicate with Wunderlist2 API
77
*
8+
* @requires base.class.php
89
* @author Joshua de Gier
910
* @version 1.01 18/07/2013
1011
*/
1112

12-
class Wunderlist
13+
require_once('base.class.php');
14+
15+
class Wunderlist extends Wunderbase
1316
{
1417

15-
private $authtoken = false;
16-
private $api_url = 'http://api.wunderlist.com';
18+
public $authtoken = false;
19+
protected $api_url = 'https://api.wunderlist.com';
1720

18-
public $tasks = false;
19-
public $lists = false;
20-
public $listTasks = false;
21+
protected $tasks = false;
22+
protected $lists = false;
23+
protected $listTasks = false;
2124

2225
/**
2326
* construct the Wunderlist API and save the authentication token for later use
@@ -461,117 +464,6 @@ public function deleteList($list_id)
461464
}
462465
}
463466

464-
/**
465-
* performs the call to the Wunderlist API
466-
*
467-
* @param string $action
468-
* @param string $method
469-
* @param array $data
470-
*
471-
* @throws Exception if the expected HTTP response does not match the result HTTP response
472-
* @return array Returns an array containing the response
473-
*/
474-
private function call($action, $method, $data)
475-
{
476-
// Check action parameter
477-
if( $action == "" )
478-
{
479-
throw new Exception( "No API action given", 0001 );
480-
}
481-
482-
// Check method parameter
483-
if( $action == "" )
484-
{
485-
throw new Exception( "No API method given", 0002 );
486-
}
487-
488-
// Expected response is 200 OK
489-
$expectedResponse = 200;
490-
491-
// Start with an empty set of headers
492-
$headers = array();
493-
494-
// Init cURL
495-
$ch = curl_init($this->api_url.$action);
496-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
497-
498-
// Pass data?
499-
if( is_array($data) && count($data) > 0 )
500-
{
501-
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data) );
502-
}
503-
504-
// Set request type for POST
505-
if( strtolower($method) == 'post' )
506-
{
507-
if( $action != "/login" )
508-
{
509-
// For post-request the expected response is 201 Created
510-
$expectedResponse = 201;
511-
}
512-
curl_setopt($ch, CURLOPT_POST, true );
513-
}
514-
515-
// Set request type for PUT
516-
if( strtolower($method) == 'put' )
517-
{
518-
// Set custom request to put
519-
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
520-
521-
// Data needs to be send as json
522-
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data) );
523-
524-
// Set header content-type to application/json
525-
$headers[] = 'content-type: application/json';
526-
527-
// Set header Content-Lenght to the json encoded length
528-
$headers[] = 'Content-Length: '.strlen(json_encode($data));
529-
}
530-
531-
// Set request type for DELETE
532-
if( strtolower($method) == 'delete' )
533-
{
534-
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
535-
}
536-
537-
// Send authtoken if set
538-
if( $this->authtoken != false )
539-
{
540-
$headers[] = 'authorization: Bearer '.$this->authtoken;
541-
}
542-
543-
// Send headers with the request
544-
if( count($headers) > 0 )
545-
{
546-
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
547-
}
548-
549-
$output = curl_exec($ch);
550-
551-
// Get HTTP response
552-
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
553-
554-
// Close CURL
555-
curl_close($ch);
556-
557-
// get / put / delete requests should have HTTP Code 200 OK
558-
// only exception is the login method, which returns HTTP Code 200 OK
559-
if($httpCode == 200 && (strtolower($method) != 'post' || $action == '/login'))
560-
{
561-
return json_decode($output, true);
562-
}
563-
// all non-login post requests should have HTTP Code 201 Created
564-
elseif($httpCode == 201 && strtolower($method) == 'post')
565-
{
566-
return json_decode($output, true);
567-
}
568-
// If the HTTP code did not match, than the request failed
569-
else
570-
{
571-
throw new Exception( "API Call failed - Method: $method - Action: $action - HTTP Response: $httpCode (Expected $expectedResponse)", 0000 );
572-
}
573-
}
574-
575467
}
576468

577469
?>

api.files.class.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/**
4+
* Wrapper for Wunderlist2 API - Files (PRO accounts only)
5+
* api.class.php
6+
* Purpose: communicate with Wunderlist2 API
7+
*
8+
* @requires base.class.php
9+
* @author Joshua de Gier
10+
* @version 1.01 18/07/2013
11+
*/
12+
13+
require_once('base.class.php');
14+
15+
class Wunderfiles extends Wunderbase
16+
{
17+
18+
protected $authtoken = false;
19+
protected $api_url = 'https://files.wunderlist.com';
20+
21+
protected $files = false;
22+
23+
/**
24+
* construct the Wunderlist Files API and save the authentication token for later use
25+
*
26+
* @param string $token
27+
*
28+
* @throws Exception if any of the parameters are empty or authentication fails
29+
* @return void
30+
*/
31+
public function __construct($token)
32+
{
33+
// Check for email
34+
if( $token == "" )
35+
{
36+
throw new Exception( "Token parameter empty", 1001 );
37+
}
38+
$this->authtoken = $token;
39+
}
40+
41+
/**
42+
* get all the files of the currently logged in user
43+
*
44+
* @return array Returns an array with the files of the user
45+
*/
46+
public function filelist()
47+
{
48+
$return = $this->call('/files', 'get', array());
49+
return $return;
50+
}
51+
52+
}
53+
54+
?>

base.class.php

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
/**
4+
* Wrapper for Wunderlist2 API
5+
* api.class.php
6+
* Purpose: communicate with Wunderlist2 API
7+
*
8+
* @author Joshua de Gier
9+
* @version 1.01 18/07/2013
10+
*/
11+
12+
class Wunderbase
13+
{
14+
15+
protected function call($action, $method, $data)
16+
{
17+
// Check action parameter
18+
if( $action == "" )
19+
{
20+
throw new Exception( "No API action given", 0001 );
21+
}
22+
23+
// Check method parameter
24+
if( $action == "" )
25+
{
26+
throw new Exception( "No API method given", 0002 );
27+
}
28+
29+
// Expected response is 200 OK
30+
$expectedResponse = 200;
31+
32+
// Start with an empty set of headers
33+
$headers = array();
34+
35+
// Init cURL
36+
$ch = curl_init($this->api_url.$action);
37+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
38+
39+
// Pass data?
40+
if( is_array($data) && count($data) > 0 )
41+
{
42+
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data) );
43+
}
44+
45+
// Set request type for POST
46+
if( strtolower($method) == 'post' )
47+
{
48+
if( $action != "/login" )
49+
{
50+
// For post-request the expected response is 201 Created
51+
$expectedResponse = 201;
52+
}
53+
curl_setopt($ch, CURLOPT_POST, true );
54+
}
55+
56+
// Set request type for PUT
57+
if( strtolower($method) == 'put' )
58+
{
59+
// Set custom request to put
60+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
61+
62+
// Data needs to be send as json
63+
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data) );
64+
65+
// Set header content-type to application/json
66+
$headers[] = 'content-type: application/json';
67+
68+
// Set header Content-Lenght to the json encoded length
69+
$headers[] = 'Content-Length: '.strlen(json_encode($data));
70+
}
71+
72+
// Set request type for DELETE
73+
if( strtolower($method) == 'delete' )
74+
{
75+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
76+
}
77+
78+
// Send authtoken if set
79+
if( $this->authtoken != false )
80+
{
81+
$headers[] = 'authorization: Bearer '.$this->authtoken;
82+
}
83+
84+
// Files
85+
if( strpos($action, "/files") )
86+
{
87+
$headers[] = "Accept: application/json";
88+
$headers[] = "Accept-Encoding: gzip, deflate";
89+
$headers[] = "Accept-Language: nl,en-us;q=0.7,en,q=0.3";
90+
$headers[] = "Content-Type: application/json; charset=utf-8";
91+
$headers[] = "Host: files.wunderlist.com";
92+
$headers[] = "Origin: https://www.wunderlist.com";
93+
$headers[] = "Referer: https://www.wunderlist.com";
94+
$headers[] = "X-6W-Platform: web";
95+
$headers[] = "X-6W-Product: wunderlist";
96+
$headers[] = "X-6W-System: MacIntel";
97+
}
98+
99+
// Send headers with the request
100+
if( count($headers) > 0 )
101+
{
102+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
103+
}
104+
105+
$output = curl_exec($ch);
106+
107+
// Get HTTP response
108+
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
109+
110+
// Close CURL
111+
curl_close($ch);
112+
113+
// get / put / delete requests should have HTTP Code 200 OK
114+
// only exception is the login method, which returns HTTP Code 200 OK
115+
if($httpCode == 200 && (strtolower($method) != 'post' || $action == '/login'))
116+
{
117+
return json_decode($output, true);
118+
}
119+
// all non-login post requests should have HTTP Code 201 Created
120+
elseif($httpCode == 201 && strtolower($method) == 'post')
121+
{
122+
return json_decode($output, true);
123+
}
124+
// If the HTTP code did not match, than the request failed
125+
else
126+
{
127+
throw new Exception( "API Call failed - Method: $method - Action: $action - HTTP Response: $httpCode (Expected $expectedResponse)", 0000 );
128+
}
129+
}
130+
131+
}

examples/getFiles.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
// Include the class & constructor
4+
include_once('init.php');
5+
6+
// Try & Catch
7+
try
8+
{
9+
// Construct the Wunderfiles class and pass our token
10+
$wunderFiles = new Wunderfiles( $wunderlist->authtoken );
11+
12+
// Get all available files
13+
$files = $wunderFiles->filelist();
14+
15+
// View lists
16+
echo '<pre>';
17+
var_dump($files);
18+
}
19+
catch(Exception $e)
20+
{
21+
echo $e->getMessage();
22+
// $e->getCode() contains the error code
23+
}

examples/init.php

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
// Require the API class
88
require_once('../api.class.php');
9+
require_once('../api.files.class.php');
910

1011
// construct the Wunderlist class using user Wunderlist e-mailaddress and password
1112
try

0 commit comments

Comments
 (0)