Skip to content

Commit

Permalink
Update core. Added API explorer.
Browse files Browse the repository at this point in the history
  • Loading branch information
RTippin committed Jul 14, 2021
1 parent a902d93 commit f996d30
Show file tree
Hide file tree
Showing 26 changed files with 426 additions and 146 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ MESSENGER_SITE_NAME="Messenger"
MESSENGER_CALLING_ENABLED=false
MESSENGER_SYSTEM_MESSAGES_ENABLED=true
MESSENGER_SOCKET_ENDPOINT=http://localhost:6001
MESSENGER_MESSAGE_SIZE_LIMIT=5000
MESSENGER_PUSH_NOTIFICATIONS_ENABLED=false
MESSENGER_PROVIDER_AVATARS_ENABLED=true
MESSENGER_THREAD_AVATARS_ENABLED=true
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/storage/*.key
/vendor
/.idea

/storage/messenger-responses.json

/public/css
/public/mix-manifest.json
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ $ php artisan migrate:fresh --seed
---

## Final Steps:
**To view the API Explorer, you must download our generated responses:**
```bash
$ php artisan messenger:get:api
```
**To run locally, run 3 terminals for the following commands in your project folder:**
```bash
$ php artisan serve
Expand Down
37 changes: 37 additions & 0 deletions app/Console/Commands/RefreshApiResponsesCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class RefreshApiResponsesCommand extends Command
{
/**
* Git endpoint where the most recent generated responses are stored.
*/
const GIT_ENDPOINT = 'https://raw.githubusercontent.com/RTippin/messenger/master/docs/generated/responses.json';

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'messenger:get:api';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Download and store a fresh copy of all API responses generated from the messenger core.';

/**
* Store the responses file.
*/
public function handle()
{
file_put_contents(storage_path('messenger-responses.json'), file_get_contents(self::GIT_ENDPOINT));

$this->info("messenger-responses.json stored in public!");
}
}
46 changes: 15 additions & 31 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use ReflectionClass;
use ReflectionException;
use Symfony\Component\HttpFoundation\Response;
use Throwable;

class Handler extends ExceptionHandler
Expand All @@ -19,7 +18,7 @@ class Handler extends ExceptionHandler
* @var array
*/
protected $dontReport = [
// Exceptions not to report
// Things not to report
];

/**
Expand All @@ -39,54 +38,39 @@ class Handler extends ExceptionHandler
*/
public function register()
{
$this->reportable(function (Throwable $e) {
//
});
//
}

/**
* Render an exception into an HTTP response.
*
* @param Request $request
* @param Throwable $exception
* @return JsonResponse
* @param Throwable $e
* @return Response
*
* @throws Throwable
* @noinspection PhpMissingParamTypeInspection
*/
public function render($request, Throwable $exception)
public function render($request, Throwable $e)
{
if($exception instanceof ModelNotFoundException){
if ($e instanceof ModelNotFoundException) {
return new JsonResponse([
'message' => "Unable to locate the {$this->prettyModelNotFound($exception)} you requested."
'message' => "Unable to locate the {$this->prettyModelNotFound($e)} you requested."
], 404);
}

return parent::render($request, $exception);
return parent::render($request, $e);
}

/**
* @param Throwable $exception
* @param ModelNotFoundException $exception
* @return string
* @noinspection PhpPossiblePolymorphicInvocationInspection
*/
private function prettyModelNotFound(Throwable $exception): string
private function prettyModelNotFound(ModelNotFoundException $exception): string
{
try {
if( ! is_null($exception->getModel()))
{
return Str::lower(
ltrim(
preg_replace(
'/[A-Z]/',
' $0',
(new ReflectionClass($exception->getModel()))->getShortName()
)
)
);
}
} catch (ReflectionException $e) {
report($e);
if (! is_null($exception->getModel())) {
return Str::lower(ltrim(preg_replace('/[A-Z]/', ' $0', class_basename($exception->getModel()))));
}

return 'resource';
}
}
90 changes: 90 additions & 0 deletions app/Http/Controllers/ApiExplorerController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Route;
use Illuminate\Routing\Router;
use Illuminate\Support\Str;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class ApiExplorerController extends Controller
{
/**
* @param Router $router
* @return JsonResponse
*/
public function getRoutes(Router $router): JsonResponse
{
$routes = collect($router->getRoutes())
->map(fn ($route) => $this->getRouteInformation($route))
->filter(fn ($route) => $this->routeIsMessenger($route))
->sortBy('name')
->values()
->toArray();

return new JsonResponse([
'html' => view('explorer.routes')->with('routes', $routes)->render(),
]);
}

/**
* @param string $route
* @return JsonResponse
*/
public function getRouteResponses(string $route): JsonResponse
{
$file = storage_path('messenger-responses.json');

if (! file_exists($file)) {
throw new NotFoundHttpException("The messenger-responses.json file was not found. Please run the command 'php artisan messenger:get:api' to download it.");
}

$responses = json_decode(file_get_contents($file), true);

if (! array_key_exists($route, $responses)) {
throw new NotFoundHttpException("The route name { $route } was not found.");
}

$details = view('explorer.route-details')
->with('uri', $responses[$route]['uri'])
->with('methods', $responses[$route]['methods'])
->with('query', $responses[$route]['query'])
->with('name', $route)
->render();

$verb = explode('|', $responses[$route]['methods'])[0];

$data = view('explorer.route-responses')
->with('data', $responses[$route][$verb])
->with('verb', $verb)
->render();

return new JsonResponse([
'details' => $details,
'data' => $data,
]);
}

/**
* @param Route $route
* @return array
*/
private function getRouteInformation(Route $route): array
{
return [
'method' => implode('|', $route->methods()),
'uri' => $route->uri(),
'name' => $route->getName(),
];
}

/**
* @param array $route
* @return bool
*/
private function routeIsMessenger(array $route): bool
{
return Str::contains($route['name'], 'api.messenger');
}
}
22 changes: 7 additions & 15 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,26 @@
namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\JsonResponse;

class HomeController extends Controller
{
/**
* Show the application config.
*
* @return Renderable
*/
public function config(): Renderable
{
return view('config');
}

/**
* @return JsonResponse
*/
public function getDemoAccounts(): JsonResponse
{
$users = User::demo()->get()->shuffle()->filter(function (User $user){
return $user->getProviderOnlineStatus() === 0;
});
$users = User::demo()
->get()
->shuffle()
->filter(fn (User $user) => $user->getProviderOnlineStatus() === 0)
->take(5);

return new JsonResponse([
'html' => view('auth.demoAcc')->with('users', $users->take(5))->render()
'html' => view('auth.demoAcc')->with('users', $users)->render()
]);
}

/**
* @return JsonResponse
*/
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"laravel/tinker": "^2.5",
"laravel/ui": "^3.1",
"pusher/pusher-php-server": "^6.1.0",
"rtippin/messenger": "^0.41",
"rtippin/messenger": "^0.42",
"rtippin/messenger-bots": "^0.4",
"rtippin/messenger-ui": "^0.3"
},
Expand Down
Loading

0 comments on commit f996d30

Please sign in to comment.