diff --git a/lib/thirdparty/Asana.class.php b/lib/thirdparty/Asana.class.php index 218e55f6..ed042adf 100644 --- a/lib/thirdparty/Asana.class.php +++ b/lib/thirdparty/Asana.class.php @@ -2,7 +2,7 @@ class Asana { - protected static $curlOptions = ['json_response' => true, 'cache' => true]; + protected static $curlOptions = ['json_response' => true, 'cache' => true, 'timeout' => 10]; public static function listRoadmapTasks($cache = true) { @@ -16,7 +16,6 @@ class Asana 158602294500249 => ['Documentation', null], ]; - $groupCategories = ['ongoing', 'upcoming']; $tasks = []; $tags = [ @@ -25,30 +24,30 @@ class Asana 192699565737948 => 'Future' ]; - foreach($tags as $tagId => $tagLabel) + foreach ($tags as $tagId => $tagLabel) { $taggedTasks = static::get('/tags/' . $tagId . '/tasks', ['completed_since' => 'now'], $cache); foreach ($taggedTasks as $task) { - $fullTask = static::get('/tasks/' . $task['id']); + $fullTask = static::get('/tasks/' . $task['id']); $projectId = $fullTask['memberships'][0]['project']['id'] ?? null; if ($fullTask['name'] && $projectId && isset($projects[$projectId])) { list($projectName, $projectUrl) = $projects[$projectId]; $tasks[$tagLabel][] = array_intersect_key($fullTask, ['name' => null]) + [ 'project_label' => $projectName, - 'badge' => $projectName, - 'date' => $fullTask['due_on'] ?? null, - 'body' => $fullTask['notes'], - 'url' => $projectUrl, - 'group' => $tagLabel, - 'assignee' => $fullTask['assignee'] ? ucwords($fullTask['assignee']['name']) : '' - ]; + 'badge' => $projectName, + 'date' => $fullTask['due_on'] ?? null, + 'body' => $fullTask['notes'], + 'url' => $projectUrl, + 'group' => $tagLabel, + 'assignee' => $fullTask['assignee'] ? ucwords($fullTask['assignee']['name']) : '' + ]; } } } - foreach($tasks as &$groupTasks) + foreach ($tasks as &$groupTasks) { usort($groupTasks, function ($tA, $tB) { @@ -72,12 +71,12 @@ class Asana $apiKey = Config::get('asana_key'); $options = [ - 'headers' => ['Authorization: Bearer ' . $apiKey], - 'cache' => $cache - ] + static::$curlOptions; + 'headers' => ['Authorization: Bearer ' . $apiKey], + 'cache' => $cache + ] + static::$curlOptions; - $responseData = Curl::get('https://app.asana.com/api/1.0' . $endpoint, $data, $options); - return isset($responseData['data']) ? $responseData['data'] : []; + $responseData = CurlWithCache::get('https://app.asana.com/api/1.0' . $endpoint, $data, $options); + return $responseData['data'] ?? []; } } diff --git a/lib/thirdparty/Github.class.php b/lib/thirdparty/Github.class.php index 19ce072f..228fdab7 100644 --- a/lib/thirdparty/Github.class.php +++ b/lib/thirdparty/Github.class.php @@ -33,7 +33,7 @@ class Github public static function get($endpoint, $cache = true) { - return Curl::get('https://api.github.com' . $endpoint, [], ['user_agent' => 'LBRY', 'json_response' => true, 'cache' => $cache]); + return CurlWithCache::get('https://api.github.com' . $endpoint, [], ['user_agent' => 'LBRY', 'json_response' => true, 'cache' => $cache]); } public static function listRoadmapChangesets($cache = true) diff --git a/lib/tools/Curl.class.php b/lib/tools/Curl.class.php index 443dd7b6..7f6d17ef 100644 --- a/lib/tools/Curl.class.php +++ b/lib/tools/Curl.class.php @@ -2,10 +2,12 @@ class Curl { - const GET = 'GET', - POST = 'POST', - PUT = 'PUT', - DEFAULT_CACHE = 600000; + const + GET = 'GET', + POST = 'POST', + PUT = 'PUT', + DELETE = 'DELETE'; + public static function get($url, $params = [], $options = []) { @@ -25,13 +27,18 @@ class Curl return $body; } + public static function delete($url, $params = [], $options = []) + { + list ($status, $headers, $body) = static::doCurl(static::DELETE, $url, $params, $options); + return $body; + } + public static function doCurl($method, $url, $params = [], $options = []) { $defaults = [ - 'cache' => false, 'headers' => [], 'verify' => true, - 'timeout' => 10, //5 was timing out on /roadmap for Asana API + 'timeout' => 5, 'follow_redirects' => true, 'user_agent' => null, 'proxy' => null, @@ -39,7 +46,7 @@ class Curl 'cookie' => null, 'json_data' => false, 'json_response' => false, - 'retry' => false + 'retry' => false, ]; $invalid = array_diff_key($options, $defaults); @@ -55,21 +62,6 @@ class Curl $options = array_merge($defaults, $options); - if (!Apc::isEnabled()) - { - $options['cache'] = false; - } - - if ($options['cache']) - { - $cacheKey = md5('z' . $url . $method . serialize($options) . serialize($params)); - $cachedData = apc_fetch($cacheKey); - if ($cachedData) - { - return $cachedData; - } - } - if ($options['headers'] && $options['headers'] !== array_values($options['headers'])) // associative array { throw new DomainException('Headers must not be an associative array. Its a simple array with values of the form "Header: value"'); @@ -180,14 +172,7 @@ class Curl curl_close($ch); - $response = [$statusCode, $headers, $responseContent]; - - if ($options['cache']) - { - apc_store($cacheKey, $response, is_numeric($options['cache']) ? $options['cache'] : static::DEFAULT_CACHE); - } - - return $response; + return [$statusCode, $headers, $responseContent]; } } diff --git a/lib/tools/CurlWithCache.class.php b/lib/tools/CurlWithCache.class.php new file mode 100644 index 00000000..47295e34 --- /dev/null +++ b/lib/tools/CurlWithCache.class.php @@ -0,0 +1,32 @@ +