-
Notifications
You must be signed in to change notification settings - Fork 0
/
habitrpg_lib.php
213 lines (184 loc) · 6.44 KB
/
habitrpg_lib.php
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
/*
* @author Rex Finn <[email protected]>
* @version 1.4
* @link http://github.com/rexfinn/HabitRPG_PHP
* @package HabitRPG_PHP
*/
class HabitRPG {
public $userId;
public $apiToken;
public $apiURL;
/**
* Creates a new HabitRPG instance
*/
public function __construct ($userId, $apiToken) {
$this->userId = $userId;
$this->apiToken = $apiToken;
$this->apiURL = "https://habitrpg.com/api/v1/user";
if(!extension_loaded("cURL")) {
throw new Exception("This HabitRPG PHP API class requires cURL in order to work.");
}
}
/**
* Creates a new task for the userId and apiToken HabitRPG is initiated with
* @param array $newTaskParams required keys: type, title and text
* @param array $newTaskParams optional keys: value and note
*/
public function newTask($newTaskParams) {
if(is_array($newTaskParams)) {
if(!empty($newTaskParams['type']) && !empty($newTaskParams['title']) && !empty($newTaskParams['text'])) {
$newTaskParamsEndpoint=$this->apiURL."/task";
$newTaskPostBody=array();
$newTaskPostBody['type'] = $newTaskParams['type'];
$newTaskPostBody['title'] = $newTaskParams['title'];
$newTaskPostBody['text'] = $newTaskParams['text'];
if(!empty($newTaskParams['value'])) {
$newTaskPostBody['value']=$newTaskParams['value'];
}
if(!empty($newTaskParams['note'])) {
$newTaskPostBody['note']=$newTaskParams['note'];
}
$newTaskPostBody=json_encode($newTaskPostBody);
return $this->curl($newTaskParamsEndpoint,"POST",$newTaskPostBody);
}
else {
throw new Exception("Required keys of $newTaskParams are null.");
}
}
else {
throw new Exception("newTask takes an array as it's parameter.");
}
}
/**
* Up votes or down votes a task by taskId using apiToken and userId
* @param array $scoringParams required keys: taskId and direction
* @param array $scoringParams optional keys: title, service and icon
*/
public function taskScoring($scoringParams) {
if(is_array($scoringParams)) {
if(!empty($scoringParams['taskId']) && !empty($scoringParams['direction'])) {
$scoringEndpoint="https://habitrpg.com/api/v1/user/task/".$scoringParams['taskId']."/".$scoringParams['direction'];
$scoringPostBody=array();
if(!empty($scoringParams['title'])) {
$scoringPostBody['title']=$scoringParams['title'];
}
if(!empty($scoringParams['service'])) {
$scoringPostBody['service']=$scoringParams['service'];
}
if(!empty($scoringParams['icon'])) {
$scoringPostBody['icon']=$scoringParams['icon'];
}
$scoringPostBody=json_encode($scoringPostBody);
return $this->curl($scoringEndpoint,"POST",$scoringPostBody);
}
else {
throw new Exception("Required keys of $scoringParams are null.");
}
}
else {
throw new Exception("taskScoring takes an array as it's parameter.");
}
}
/**
* Grabs all a user's info using the apiToken and userId
* @function userStats() no parameter's required, uses userId and apiToken
*/
public function userStats() {
return $this->curl($this->apiURL,"GET",NULL);
}
/**
* Gets a JSON feed of all of a users task using apiToken and userId
* @param string $userTasksType ex. habit,todo,daily (optional null value)
* @param string $userTasksType allows to output only certain type of task
*/
public function userTasks($userTasksType=NULL) {
$userTasksEndpoint=$this->apiURL."/tasks";
if($userTasksType != NULL) {
$userTasksEndpoint=$this->apiURL."/tasks?type=".$userTasksType;
}
return $this->curl($userTasksEndpoint,"GET",NULL);
}
/**
* Get's info for a certain task only for the apiToken and userId passed
* @param string $taskId taskId for user task, which can be grabbed from userTasks()
*/
public function userGetTask($taskId) {
if(!empty($taskId)) {
$userGetTaskEndpoint=$this->apiURL."/task/".$taskId;
return $this->curl($userGetTaskEndpoint,"GET");
}
else {
throw new Exception("userGetTask needs a value as it's parameter.");
}
}
/**
* Updates a task's for a userId and apiToken combo and a taskId
* @param array $updateParams required keys: taskId and text
*/
public function updateTask($updateParams) {
if(is_array($updateParams)) {
if(!empty($updateParams['taskId']) && !empty($updateParams['text'])) {
$updateParamsEndpoint=$this->apiURL."/task/".$updateParams['taskId'];
$updateTaskPostBody=array();
$updateTaskPostBody['text'] = $updateParams['text'];
$updateTaskPostBody=json_encode($updateTaskPostBody);
return $this->curl($updateParamsEndpoint,"PUT",$updateTaskPostBody);
}
else {
throw new Exception("Required keys of $updateParams are null.");
}
}
else {
throw new Exception("updateTask takes an array as it's parameter.");
}
}
/**
* Performs all cURLs that are initated in each function, private function
* @param string $endpoint is the URL of the cURL
* @param string $curlType is the type of the cURL for the switch, e.g. PUT, POST, GET, etc.
* @param array $postBody is the data that is posted to $endpoint in JSON
*/
private function curl($endpoint,$curlType,$postBody) {
$curl = curl_init();
$curlArray = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_ENCODING => "gzip",
CURLOPT_HTTPHEADER => array(
"Content-type: application/json",
"x-api-user:".$this->userId,
"x-api-key:".$this->apiToken),
CURLOPT_URL => $endpoint);
switch($curlType) {
case "POST":
$curlArray[CURLOPT_POSTFIELDS] = $postBody;
$curlArray[CURLOPT_POST] = true;
curl_setopt_array($curl, $curlArray);
break;
case "GET":
curl_setopt_array($curl, $curlArray);
break;
case "PUT":
$curlArray[CURLOPT_CUSTOMREQUEST] = "PUT";
$curlArray[CURLOPT_POSTFIELDS] = $postBody;
curl_setopt_array($curl, $curlArray);
break;
case "DELETE":
break;
default:
throw new Exception("Please use a valid method as the cURL type.");
}
$habitRPGResponse = curl_exec($curl);
$habitRPGHTTPCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($habitRPGHTTPCode == 200) {
return array("result"=>true,"habitRPGData"=>json_decode($habitRPGResponse,true));
}
else {
$habitRPGResponse = json_decode($habitRPGResponse,true);
return array("error"=>$habitRPGResponse['err'],"details"=>array("httpCode"=>$habitRPGHTTPCode,"endpoint"=>$endpoint,"dataSent"=>json_decode($postBody,true)));
}
}
}
?>