-
Notifications
You must be signed in to change notification settings - Fork 7
/
codebasehq.inc.php
74 lines (63 loc) · 3.09 KB
/
codebasehq.inc.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
<?php
class CodebaseHQAPI {
function __construct($account, $user_name, $api_key) {
$this->account = $account;
$this->user_name = $user_name;
$this->api_key = $api_key;
$this->cache_dir = './cache'; #no slash!
}
function base_url() {
return 'http://api3.codebasehq.com';
}
function _get_request($full_path, $timeout = 300) {
$url = $this->base_url() . $full_path;
$cache_file = $this->cache_dir .'/'. sha1($url);
if (!file_exists($cache_file) || ((time() - @filemtime($cache_file)) > $timeout)) {
$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', 'Accept: application/xml'));
curl_setopt($process, CURLOPT_HEADER, 0);
curl_setopt($process, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($process, CURLOPT_USERPWD, $this->account .'/'. $this->user_name . ":" . $this->api_key);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
//curl_setopt($process, CURLOPT_POST, 1);
//curl_setopt($process, CURLOPT_POSTFIELDS, $payloadName);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($process);
$f = fopen($cache_file, 'w');
fwrite($f, $return);
fclose($f);
} else {
$return = file_get_contents($cache_file);
}
return $return;
}
function _parse_request($content) {
return simplexml_load_string($content);
}
function _perform_request($full_path, $timeout = 300) {
$content = $this->_get_request($full_path, $timeout);
return $this->_parse_request($this->_get_request($full_path));
}
function get_statuses($project) {
return $this->_perform_request(sprintf('/%s/tickets/statuses', $project), 86400);
}
function get_priorities($project) {
return $this->_perform_request(sprintf('/%s/tickets/priorities', $project), 86400);
}
function get_categories($project) {
return $this->_perform_request(sprintf('/%s/tickets/categories', $project), 86400);
}
function get_milestones($project) {
return $this->_perform_request(sprintf('/%s/milestones', $project), 86400);
}
function get_users($project) {
return $this->_perform_request(sprintf('/%s/assignments', $project), 86400);
}
function search_tickets($project, $query, $page = 1) {
$params = 'query='. urlencode($query);
if ($page > 1) {
$params .= '&page='. $page;
}
return $this->_perform_request(sprintf('/%s/tickets?%s', $project, $params));
}
}