From c912123262387cb74c8eb483f6f80ce74ab031bd Mon Sep 17 00:00:00 2001 From: technowhizz <7688823+technowhizz@users.noreply.github.com> Date: Fri, 7 Apr 2023 18:03:57 +0000 Subject: [PATCH] Add delete link API functionality Adds delete link functionality to the API through the use of a GET request to admin/delete_link --- app/Http/Controllers/Api/ApiLinkController.php | 15 +++++++++++++++ app/Http/routes.php | 3 +++ docs/developer-guide/api.md | 14 ++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/app/Http/Controllers/Api/ApiLinkController.php b/app/Http/Controllers/Api/ApiLinkController.php index b1f377c3c..0320ca3a9 100644 --- a/app/Http/Controllers/Api/ApiLinkController.php +++ b/app/Http/Controllers/Api/ApiLinkController.php @@ -137,4 +137,19 @@ public function lookupLink(Request $request) { throw new ApiException('NOT_FOUND', 'Link not found.', 404, $response_type); } } + + public function deleteLink(Request $request) { + + $response_type = $request->input('response_type'); + $url_ending = $request->input('url_ending'); + $link = LinkHelper::linkExists($url_ending); + + if (!$link) { + throw new ApiException('DELETION ERROR','The Short Link to delete was not found', 404, $response_type); + } + + $link->delete(); + return "OK"; + } + } diff --git a/app/Http/routes.php b/app/Http/routes.php index e936cb10f..aa663ea70 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -75,4 +75,7 @@ /* API data endpoints */ $app->get('data/link', ['as' => 'api_link_analytics', 'uses' => 'ApiAnalyticsController@lookupLinkStats']); $app->post('data/link', ['as' => 'api_link_analytics', 'uses' => 'ApiAnalyticsController@lookupLinkStats']); + + /*API delete endpoints */ + $app->get('admin/delete_link', ['as' => 'api_delete_link', 'uses' => 'ApiLinkController@deleteLink']); }); diff --git a/docs/developer-guide/api.md b/docs/developer-guide/api.md index bcd192716..a14975e04 100644 --- a/docs/developer-guide/api.md +++ b/docs/developer-guide/api.md @@ -231,6 +231,20 @@ Response: } ``` +### /api/v2/admin/delete_link +Arguments: + + - `url_ending`: the link ending for the URL to delete. (e.g `5ga`) + +An API key granted to a regular user can only be used to delete their own links. +Admins can delete any link. + +Response: An OK if the link was deleted, unauthorised if not authorised to delete or 404 if the link does not exist. + +Example: GET `http://example.com/api/v2/admin/delete_link?key=API_KEY_HERE&url_ending=5gk` + +Response: `OK` + ## HTTP Error Codes The API will return an error code if your request was malformed or another error occured while processing your request.