diff --git a/include/class-ClickUpAPI.php b/include/class-ClickUpAPI.php index d7c6fb6..502cbb6 100644 --- a/include/class-ClickUpAPI.php +++ b/include/class-ClickUpAPI.php @@ -1,141 +1,149 @@ ClickUp bot # Copyright (C) 2022 Valerio Bozzolan, contributors # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . /** * Utility to run HTTP queries against ClickUp */ class ClickUpAPI { /** * https://clickup.com/api/clickupreference/operation/GetSpaces/ * * @return array */ public static function getSpaces() { - return self::querySpaces()->spaces; + return self::querySpaces()->spaces ?? []; } /** * https://clickup.com/api/clickupreference/operation/GetSpaces/ * * @return array */ public static function getSpaceFolders( $space_id ) { - return self::querySpaceFolders( $space_id )->folders; + return self::querySpaceFolders( $space_id )->folders ?? []; } /** * https://clickup.com/api/clickupreference/operation/GetTask/ */ public static function queryTaskData( $task_id ) { // append the Team ID since it does not work otherwise $task_details = self::requestGET( "/task/$task_id", self::query_team() ); // no Task no party if( !$task_details ) { throw new Exception( "missing ClickUp Task $task_id" ); } // save some stuff in the cache ClickUpPhabricatorCache::instance() ->newlock() ->importClickupTaskData( $task_id, $task_details ) ->save(); return $task_details; } /** * https://clickup.com/api/clickupreference/operation/UpdateTask/ */ public static function putTaskData( $task_id, $data = [] ) { - $task_details = self::requestPUT( "/task/$task_id", self::query_team() ); + $task_details = self::requestPUT( "/task/$task_id", $data ); ClickUpPhabricatorCache::instance() ->newlock() ->importClickupTaskData( $task_id, $task_details ) ->save(); return $task_details; } /** * https://clickup.com/api/clickupreference/operation/GetSpaces/ * * @return mixed */ private static function querySpaces() { $team_id = CLICKUP_TEAM_ID; - $query = [ - 'archived' => false, - ]; - return self::requestGET( "/team/$team_id/spaces", $query ); + return self::requestGET( "/team/$team_id/space" ); } /** * https://clickup.com/api/clickupreference/operation/GetFolders/ */ private static function querySpaceFolders( $space_id ) { $team_id = CLICKUP_TEAM_ID; - $query = [ - 'archived' => false, - ]; - return self::requestGET( "/space/$space_id/folders", $query ); + return self::requestGET( "/space/$space_id/folder" ); } private static function request( $method, $path, $query = [] ) { - $url = "https://api.clickup.com/api/v2" . $path; $API_TOKEN = CLICKUP_API_TOKEN; + + $url = "https://api.clickup.com/api/v2" . $path; + $full_url = $url . '?' . http_build_query( $query ); + $curl_opts = [ CURLOPT_HTTPHEADER => [ "Authorization: $API_TOKEN", "Content-Type: application/json" ], - CURLOPT_URL => $url . '?' . http_build_query( $query ), + CURLOPT_URL => $full_url, CURLOPT_CUSTOMREQUEST => $method, CURLOPT_RETURNTRANSFER => true, ]; - $curl = curl_init(); curl_setopt_array( $curl, $curl_opts ); $response = curl_exec( $curl ); $error = curl_error( $curl ); curl_close( $curl ); - if( $error ) { throw new Exception( "cURL Error ClickUp #:" . $error ); } - return json_decode( $response ); + $data = json_decode( $response ); + if( $data === false ) { + throw new Exception( "cannot parse JSON of ClickUp response" ); + } + + // formal error from ClickUp + if( isset( $data->err ) ) { + throw new Exception( sprintf( + "ClickUp API error: %s", + json_encode( $data ) + ) ); + } + + return $data; } public static function requestGET( $path, $query = [] ) { return self::request( "GET", $path, $query ); } public static function requestPUT( $path, $query = [] ) { return self::request( "PUT", $path, $query ); } private static function query_team( $query = [] ) { $query[ 'team_id' ] = CLICKUP_TEAM_ID; return $query; } }