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:
Ümit Tunç
2025-01-21 18:28:39 +03:00
parent e2684ef05a
commit 30dd0c5b8d
8 changed files with 74 additions and 27 deletions
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+8 -3
View File
@@ -6,6 +6,8 @@ use App\Jobs\FetchCurrencyRates;
use App\Jobs\FetchGoldRates;
use App\Jobs\MergeCurrencyAndGoldRates;
use Illuminate\Support\Facades\Storage;
use App\Http\Controllers\Controller;
/**
* Class CurrencyController
@@ -14,8 +16,11 @@ use Illuminate\Support\Facades\Storage;
*/
class CurrencyController extends Controller
{
/**
* ⚙️ Dispatches all jobs to fetch currency and gold rates synchronously.
* Rate limited to 2 requests per 30 seconds.
*
* @return \Illuminate\Http\JsonResponse
*/
@@ -28,13 +33,13 @@ class CurrencyController extends Controller
return response()->json([
'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) {
return response()->json([
'status' => 'error',
'message' => 'An error occurred while updating rates: ' . $e->getMessage()
], 500);
'message' => 'Kurlar güncellenirken bir hata oluştu: ' . $e->getMessage()
], 429);
}
}
+1
View File
@@ -36,6 +36,7 @@ class Kernel extends HttpKernel
'password.confirm' => \Illuminate\Auth\Middleware\EnsurePasswordIsConfirmed::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::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);
}
}
+5 -2
View File
@@ -32,10 +32,13 @@
.sl-text-base {
font-size: 16px;
}
h4.sl-text-paragraph.sl-leading-snug.sl-font-prose.sl-font-semibold.sl-text-heading {
display: none;
}
</style>
</head>
<body>
asdadas
<elements-api
apiDescriptionUrl="{{ route("scribe.openapi") }}"
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');
if (linkElement) {
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
</script>
+5 -1
View File
@@ -32,6 +32,10 @@
.sl-text-base {
font-size: 16px;
}
h4.sl-text-paragraph.sl-leading-snug.sl-font-prose.sl-font-semibold.sl-text-heading {
display: none;
}
</style>
</head>
<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');
if (linkElement) {
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
</script>
-1
View File
@@ -10,7 +10,6 @@ use App\Http\Controllers\TimeDateController;
| API Routes
|--------------------------------------------------------------------------
*/
Route::get('/run-all-fetchs', [CurrencyController::class, 'runAllFetchs']);
Route::get('/today.json', [CurrencyController::class, 'getAllRates']);