-
-
Notifications
You must be signed in to change notification settings - Fork 1
base and register #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
07781cc
base and register
rdeutz fde98c0
fix cs and phpstan
rdeutz 023714b
remove package
rdeutz f96e574
type check
rdeutz 464f231
Fix cs
SniperSister 3a09bb2
deduplicate calls
SniperSister a4ed09c
move base dto
SniperSister 2b23f2e
save site
rdeutz b4331a6
stan fix
rdeutz 33ba905
types
rdeutz c8f54ac
Merge branch 'main' into feature/api-v1
SniperSister d241969
fix nullable error on write
SniperSister 4d5d54e
push last_seen
SniperSister File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| <?php | ||
|
|
||
| namespace App\Http\Controllers\Api\V1; | ||
|
|
||
| use App\Http\Controllers\Controller; | ||
| use App\Jobs\CheckSiteHealth; | ||
| use App\Models\Site; | ||
| use App\RemoteSite\Connection; | ||
| use App\Traits\ApiResponse; | ||
| use Carbon\Carbon; | ||
| use GuzzleHttp\Exception\ClientException; | ||
| use GuzzleHttp\Exception\ServerException; | ||
| use Illuminate\Http\JsonResponse; | ||
| use Illuminate\Http\Request; | ||
|
|
||
| /** | ||
| * Class SiteController | ||
| * @package App\Http\Controllers\Api\V1 | ||
| * @since 1.0 | ||
| */ | ||
| class SiteController extends Controller | ||
| { | ||
| use ApiResponse; | ||
|
|
||
| /** | ||
| * @param Request $request | ||
| * | ||
| * @return JsonResponse | ||
| * @throws \Exception | ||
| */ | ||
| public function register(Request $request): JsonResponse | ||
| { | ||
| $url = $request->string('url'); | ||
| $key = $request->string('key'); | ||
|
|
||
| if ($url->isEmpty() && $key->isEmpty()) { | ||
| return $this->error('BadRequest'); | ||
| } | ||
|
|
||
| $connectionService = new Connection($url, $key); | ||
|
|
||
| // Do a health check | ||
| try { | ||
| $healthResponse = $connectionService->checkHealth(); | ||
| } catch (ServerException $e) { | ||
| return $this->error($e->getMessage(), 500); | ||
| } catch (ClientException|\Exception $e) { | ||
| return $this->error($e->getMessage()); | ||
| } | ||
|
|
||
| // If successful save site | ||
| $site = new Site(); | ||
|
|
||
| $site->key = $key; | ||
| $site->url = $url; | ||
| $site->last_seen = Carbon::now(); | ||
|
|
||
| // Fill with site info | ||
| $site->fill($healthResponse->toArray()); | ||
|
|
||
| $site->save(); | ||
|
|
||
| CheckSiteHealth::dispatch($site); | ||
|
|
||
| return $this->ok(); | ||
| } | ||
|
|
||
| /** | ||
| * @param Request $request | ||
| * | ||
| * @return JsonResponse | ||
| */ | ||
| public function check(Request $request): JsonResponse | ||
| { | ||
| return response()->json(['check']); | ||
| } | ||
|
|
||
| /** | ||
| * @param Request $request | ||
| * | ||
| * @return JsonResponse | ||
| */ | ||
| public function delete(Request $request): JsonResponse | ||
| { | ||
| return response()->json(['delete']); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| <?php | ||
|
|
||
| namespace App\Traits; | ||
|
|
||
| use Illuminate\Http\JsonResponse; | ||
|
|
||
| /** | ||
| * Api Response | ||
| */ | ||
| trait ApiResponse | ||
| { | ||
| /** | ||
| * @param string $message | ||
| * | ||
| * @return JsonResponse | ||
| */ | ||
| protected function ok(string $message = 'OK'): JsonResponse | ||
| { | ||
| return $this->success($message, 200); | ||
| } | ||
|
|
||
| /** | ||
| * @param string $message | ||
| * @param int $statusCode | ||
| * | ||
| * @return JsonResponse | ||
| */ | ||
| protected function success(string $message, int $statusCode = 200): JsonResponse | ||
| { | ||
| return $this->response($message, $statusCode); | ||
| } | ||
|
|
||
| /** | ||
| * @param string $message | ||
| * @param int $statusCode | ||
| * | ||
| * @return JsonResponse | ||
| */ | ||
| protected function error(string $message, int $statusCode = 400): JsonResponse | ||
| { | ||
| return $this->response($message, $statusCode); | ||
| } | ||
|
|
||
| /** | ||
| * @param string $message | ||
| * @param int $statusCode | ||
| * | ||
| * @return JsonResponse | ||
| */ | ||
| protected function response(string $message, int $statusCode = 200): JsonResponse | ||
| { | ||
| return response()->json([ | ||
| 'message' => $message, | ||
| 'status' => $statusCode | ||
| ], $statusCode); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <?php | ||
|
|
||
| use App\Http\Controllers\Api\V1\SiteController; | ||
| use Illuminate\Support\Facades\Route; | ||
|
|
||
| Route::prefix('v1')->group(function () { | ||
| Route::controller(SiteController::class)->group(function () { | ||
| Route::get('register', 'register'); | ||
| Route::get('check/{hase}', 'check'); | ||
| Route::delete('delete/{hash}', 'delete'); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.