Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ BROADCAST_CONNECTION=log
FILESYSTEM_DISK=local
QUEUE_CONNECTION=database

CACHE_STORE=database
CACHE_STORE=redis
CACHE_PREFIX=

MEMCACHED_HOST=127.0.0.1
Expand Down
12 changes: 12 additions & 0 deletions app/Contracts/PetApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Contracts;

interface PetApi
{
public function searchForPets(string $query = ''): \Illuminate\Support\Collection;

public function getPet(string $id): ?\Illuminate\Support\Collection;

public function getImage(string $id): ?string;
}
40 changes: 40 additions & 0 deletions app/Http/Controllers/PetController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Http\Controllers;

use App\Contracts\PetApi;
use App\Services\CatService;

class PetController extends Controller
{
public function __construct(protected CatService $petService)
{

}

/**
* Display a listing of all the pets.
*/
public function index()
{
$pets = $this->petService->searchForPets()->map(function ($pet) {
$pet['path'] = '/pets/' . $pet['id'];
return $pet;
});

return view('dashboard', compact('pets'));
}

/**
* Display the individual pet.
*/
public function show(string $id)
{
$pet = $this->petService->getPet($id);
if (!$pet) {
abort(404);
}
$image = $this->petService->getImage($pet['reference_image_id']);
return view('show', compact('pet', 'image'));
}
}
2 changes: 2 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Providers;

use App\Contracts\PetApi;
use App\Services\CatService;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
Expand Down
94 changes: 94 additions & 0 deletions app/Services/CatService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace App\Services;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\RateLimiter;

class CatService implements \App\Contracts\PetApi
{

private function exceededRateLimit(): bool
{
if (RateLimiter::tooManyAttempts('pet-api-call', 20)) {
return true;
}

RateLimiter::increment('pet-api-call');

return false;

}
public function searchForPets(string $query = ''): \Illuminate\Support\Collection
{
if (Cache::tags(['pets'])->has($query)) {
return Cache::tags(['pets'])->get($query);
}
else {
try {
if ($this->exceededRateLimit()) {
return collect([]);
}
$pets = \Illuminate\Support\Facades\Http::withHeaders([
'Content-Type' => 'application/json',
'x-api-key' => config('app.cat_api_key'),
])->get('https://api.thecatapi.com/v1/breeds', [
'q' => $query
])->collect();

Cache::tags(['pets'])->put($query, $pets, 3600);
return $pets;
} catch (\Exception $e) {
return collect([]);
}
}
}

public function getPet(string $id): ?\Illuminate\Support\Collection
{
if (Cache::tags(['pets page'])->has($id)) {
return Cache::tags(['pets page'])->get($id);
}
else {
try {
if ($this->exceededRateLimit()) {
return null;
}
$pets = \Illuminate\Support\Facades\Http::withHeaders([
'Content-Type' => 'application/json',
'x-api-key' => config('app.cat_api_key'),
])->get('https://api.thecatapi.com/v1/breeds/' . $id, [
])->collect();

Cache::tags(['pets page'])->put($id, $pets, 3600);
return $pets;
} catch (\Exception $e) {
return collect([]);
}
}
}

public function getImage(string $id): ?string
{
if (Cache::tags(['pets image'])->has($id)) {
return Cache::tags(['pets image'])->get($id);
}
else {
try {
if ($this->exceededRateLimit()) {
return null;
}
$image = \Illuminate\Support\Facades\Http::withHeaders([
'Content-Type' => 'application/json',
'x-api-key' => config('app.cat_api_key'),
])->get('https://api.thecatapi.com/v1/images/' . $id, [
])->collect()['url'];

Cache::tags(['pets image'])->put($id, $image, 3600);
return $image;
} catch (\Exception $e) {
return null;
}
}
}
}
1 change: 1 addition & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,5 @@
'store' => env('APP_MAINTENANCE_STORE', 'database'),
],

'cat_api_key' => env('API_CAT','')
];
Loading