Enhance currency and gold rates API with custom throttling and improved responses
- Implemented a custom throttle middleware to limit requests to 2 per 30 seconds, enhancing API stability. - Updated API responses to include Turkish messages for successful updates and error handling. - Refactored the CurrencyController to integrate the new throttling logic and improve response clarity. - Enhanced the Scribe documentation to reflect changes in API behavior and response formats. - Updated views to improve branding and user experience, including dynamic content adjustments. These changes collectively improve the API's performance, usability, and branding for the Truncgil Finance application.
This commit is contained in:
File diff suppressed because one or more lines are too long
+10
-10
File diff suppressed because one or more lines are too long
@@ -6,6 +6,8 @@ use App\Jobs\FetchCurrencyRates;
|
|||||||
use App\Jobs\FetchGoldRates;
|
use App\Jobs\FetchGoldRates;
|
||||||
use App\Jobs\MergeCurrencyAndGoldRates;
|
use App\Jobs\MergeCurrencyAndGoldRates;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class CurrencyController
|
* Class CurrencyController
|
||||||
@@ -14,8 +16,11 @@ use Illuminate\Support\Facades\Storage;
|
|||||||
*/
|
*/
|
||||||
class CurrencyController extends Controller
|
class CurrencyController extends Controller
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ⚙️ Dispatches all jobs to fetch currency and gold rates synchronously.
|
* ⚙️ Dispatches all jobs to fetch currency and gold rates synchronously.
|
||||||
|
* Rate limited to 2 requests per 30 seconds.
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Http\JsonResponse
|
* @return \Illuminate\Http\JsonResponse
|
||||||
*/
|
*/
|
||||||
@@ -28,13 +33,13 @@ class CurrencyController extends Controller
|
|||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status' => 'success',
|
'status' => 'success',
|
||||||
'message' => 'All currency and gold rates have been successfully updated'
|
'message' => 'Tüm döviz ve altın kurları başarıyla güncellendi'
|
||||||
]);
|
]);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status' => 'error',
|
'status' => 'error',
|
||||||
'message' => 'An error occurred while updating rates: ' . $e->getMessage()
|
'message' => 'Kurlar güncellenirken bir hata oluştu: ' . $e->getMessage()
|
||||||
], 500);
|
], 429);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ class Kernel extends HttpKernel
|
|||||||
'password.confirm' => \Illuminate\Auth\Middleware\EnsurePasswordIsConfirmed::class,
|
'password.confirm' => \Illuminate\Auth\Middleware\EnsurePasswordIsConfirmed::class,
|
||||||
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
|
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
|
||||||
'api' => \App\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
|
'api' => \App\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
|
||||||
|
'custom.throttle' => \App\Http\Middleware\CustomThrottleRequests::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Middleware;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Cache\RateLimiter;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
|
||||||
|
class CustomThrottleRequests
|
||||||
|
{
|
||||||
|
protected $limiter;
|
||||||
|
|
||||||
|
public function __construct(RateLimiter $limiter)
|
||||||
|
{
|
||||||
|
$this->limiter = $limiter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handle(Request $request, Closure $next, $maxAttempts = 60, $decayMinutes = 1): Response
|
||||||
|
{
|
||||||
|
$key = $request->ip();
|
||||||
|
|
||||||
|
if ($this->limiter->tooManyAttempts($key, $maxAttempts)) {
|
||||||
|
return response()->json([
|
||||||
|
'error' => 'Rate limit aşıldı',
|
||||||
|
'message' => 'Bu endpoint 30 dakikada bir kez çağrılabilir.',
|
||||||
|
'retry_after' => $this->limiter->availableIn($key)
|
||||||
|
], 429);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->limiter->hit($key, $decayMinutes * 60);
|
||||||
|
|
||||||
|
return $next($request);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -32,10 +32,13 @@
|
|||||||
.sl-text-base {
|
.sl-text-base {
|
||||||
font-size: 16px;
|
font-size: 16px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
h4.sl-text-paragraph.sl-leading-snug.sl-font-prose.sl-font-semibold.sl-text-heading {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
asdadas
|
|
||||||
<elements-api
|
<elements-api
|
||||||
apiDescriptionUrl="{{ route("scribe.openapi") }}"
|
apiDescriptionUrl="{{ route("scribe.openapi") }}"
|
||||||
router="hash"
|
router="hash"
|
||||||
@@ -50,7 +53,7 @@ setTimeout(function() {
|
|||||||
const linkElement = document.querySelector('a.sl-flex.sl-items-center.sl-px-4.sl-py-3.sl-border-t');
|
const linkElement = document.querySelector('a.sl-flex.sl-items-center.sl-px-4.sl-py-3.sl-border-t');
|
||||||
if (linkElement) {
|
if (linkElement) {
|
||||||
linkElement.href = "https://truncgil.com"; // Yeni URL'yi buraya ekleyin
|
linkElement.href = "https://truncgil.com"; // Yeni URL'yi buraya ekleyin
|
||||||
// linkElement.innerHTML = '<img src="https://truncgil.com.tr/yatay.svg" alt="Truncgil Teknoloji">'; // Resim URL'sini ve alternatif metni buraya ekleyin
|
linkElement.innerHTML = '<img src="https://truncgil.com.tr/yatay.svg" width="100" alt="Truncgil Teknoloji">'; // Resim URL'sini ve alternatif metni buraya ekleyin
|
||||||
}
|
}
|
||||||
}, 1000); // 1 saniye gecikme
|
}, 1000); // 1 saniye gecikme
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -32,6 +32,10 @@
|
|||||||
.sl-text-base {
|
.sl-text-base {
|
||||||
font-size: 16px;
|
font-size: 16px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
h4.sl-text-paragraph.sl-leading-snug.sl-font-prose.sl-font-semibold.sl-text-heading {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
@@ -55,7 +59,7 @@ setTimeout(function() {
|
|||||||
const linkElement = document.querySelector('a.sl-flex.sl-items-center.sl-px-4.sl-py-3.sl-border-t');
|
const linkElement = document.querySelector('a.sl-flex.sl-items-center.sl-px-4.sl-py-3.sl-border-t');
|
||||||
if (linkElement) {
|
if (linkElement) {
|
||||||
linkElement.href = "https://truncgil.com"; // Yeni URL'yi buraya ekleyin
|
linkElement.href = "https://truncgil.com"; // Yeni URL'yi buraya ekleyin
|
||||||
// linkElement.innerHTML = '<img src="https://truncgil.com.tr/yatay.svg" alt="Truncgil Teknoloji">'; // Resim URL'sini ve alternatif metni buraya ekleyin
|
linkElement.innerHTML = '<img src="https://truncgil.com.tr/yatay.svg" width="100" alt="Truncgil Teknoloji">'; // Resim URL'sini ve alternatif metni buraya ekleyin
|
||||||
}
|
}
|
||||||
}, 1000); // 1 saniye gecikme
|
}, 1000); // 1 saniye gecikme
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ use App\Http\Controllers\TimeDateController;
|
|||||||
| API Routes
|
| API Routes
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Route::get('/run-all-fetchs', [CurrencyController::class, 'runAllFetchs']);
|
Route::get('/run-all-fetchs', [CurrencyController::class, 'runAllFetchs']);
|
||||||
|
|
||||||
Route::get('/today.json', [CurrencyController::class, 'getAllRates']);
|
Route::get('/today.json', [CurrencyController::class, 'getAllRates']);
|
||||||
|
|||||||
Reference in New Issue
Block a user