改进 请求方式

This commit is contained in:
iVampireSP.com 2022-11-30 15:57:31 +08:00
parent 1f24eabf1c
commit 5eb6f8b5cd
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
7 changed files with 22 additions and 32 deletions

View File

@ -41,8 +41,7 @@ public function handle()
continue;
}
$http = Http::module($host->module->api_token, $host->module->url);
$response = $http->get('hosts/' . $host->id);
$response = $host->module->http()->get('hosts/' . $host->id);
if ($response->status() === 404) {
Log::warning($host->module->name . ' ' . $host->name . ' ' . $host->id . ' 不存在,删除。');

View File

@ -42,19 +42,17 @@ public function handle()
$host = $this->host;
$host->load(['module']);
$http = Http::module($host->module->api_token, $host->module->url);
switch ($this->type) {
case 'patch':
$response = $http->patch('hosts/' . $host->id, $host->toArray());
$response = $host->module->http()->patch('hosts/' . $host->id, $host->toArray());
break;
case 'post':
$response = $http->post('hosts', $host->toArray());
$response = $host->module->http()->post('hosts', $host->toArray());
break;
case 'delete':
$response = $http->delete('hosts/' . $host->id);
$response = $host->module->http()->delete('hosts/' . $host->id);
// if successful
if ($response->successful() || $response->status() === 404) {

View File

@ -35,10 +35,9 @@ public function handle()
//
Host::whereIn('status', ['pending', 'error'])->with(['module', 'user'])->chunk(100, function ($hosts) {
foreach ($hosts as $host) {
$http = Http::module($host->module->api_token, $host->module->url);
$host->status = 'running';
$response = $http->post('hosts', $host->toArray());
$response = $host->module->http()->post('hosts', $host->toArray());
if (!$response->successful()) {
$host->status = 'error';

View File

@ -53,10 +53,9 @@ public function handle()
}
}
$http = Http::module($workOrder->module->api_token, $workOrder->module->url);
$workOrder->status = 'open';
$response = $http->post('work-orders', $workOrder->toArray());
$response = $workOrder->module->http()->post('work-orders', $workOrder->toArray());
if (!$response->successful()) {
Log::error('推送工单失败', [

View File

@ -40,11 +40,9 @@ public function handle()
$this->reply->load(['workOrder', 'user']);
$this->reply->workOrder->load(['module']);
$http = Http::module($this->reply->workOrder->module->api_token, $this->reply->workOrder->module->url);
$reply = $this->reply->toArray();
$response = $http->post('work-orders/' . $this->reply->workOrder->id . '/replies', $reply);
$response = $this->reply->workOrder->module->http()->post('work-orders/' . $this->reply->workOrder->id . '/replies', $reply);
if ($response->successful()) {
$this->reply->update([

View File

@ -39,17 +39,16 @@ public function handle()
{
$this->workOrder->load(['module']);
$http = Http::module($this->workOrder->module->api_token, $this->workOrder->module->url);
if ($this->type == 'put') {
$response = $http->put('work-orders/' . $this->workOrder->id, $this->workOrder->toArray());
$response = $this->workOrder->module->http()->put('work-orders/' . $this->workOrder->id, $this->workOrder->toArray());
} else if ($this->type == 'delete') {
$response = $http->delete('work-orders/' . $this->workOrder->id);
$response = $this->workOrder->module->http()->delete('work-orders/' . $this->workOrder->id);
if ($response->successful()) {
$this->workOrder->delete();
}
} else {
$response = $http->post('work-orders', $this->workOrder->toArray());
$response = $this->workOrder->module->http()->post('work-orders', $this->workOrder->toArray());
}
if (!$response->successful()) {

View File

@ -5,6 +5,7 @@
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use GuzzleHttp\Exception\ConnectException;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
@ -94,8 +95,7 @@ protected static function boot()
// post, get, patch, delete 等请求
public function remote($func, $requests): array
{
$http = Http::module($this->api_token, $this->url);
$response = $http->post('functions/' . $func, $requests);
$response = $this->http()->post('functions/' . $func, $requests);
return $this->getResponse($response);
}
@ -109,10 +109,8 @@ public function baseRequest($method, $path, $requests): array
{
$user = auth('sanctum')->user();
$http = Http::module($this->api_token, $this->url);
if ($user) {
$http->withHeaders([
$this->http()->withHeaders([
'X-User-id' => $user->id,
]);
$requests['user_id'] = $user->id;
@ -122,7 +120,7 @@ public function baseRequest($method, $path, $requests): array
}
}
$response = $http->{$method}($path, $requests);
$response = $this->http()->{$method}($path, $requests);
return $this->getResponse($response);
}
@ -131,16 +129,13 @@ public function moduleRequest($method, $path, $requests): array
{
$module_id = auth('module')->id();
$http = Http::module($this->api_token, $this->url)
->accept('application/json');
$http->withHeaders([
$this->http()->withHeaders([
'X-Module' => $module_id
]);
$requests['module_id'] = $module_id;
$response = $http->{$method}("exports/{$path}", $requests);
$response = $this->http()->{$method}("exports/{$path}", $requests);
return $this->getResponse($response);
}
@ -165,12 +160,10 @@ public function check($module_id = null): bool
$module = $this;
}
$http = Http::module($module->api_token, $module->url);
$success = 0;
try {
$response = $http->get('remote');
$response = $this->http()->get('remote');
if ($response->status() == 200) {
$success = 1;
@ -182,6 +175,11 @@ public function check($module_id = null): bool
return $success;
}
public function http(): PendingRequest
{
return Http::module($this->api_token, $this->url)->acceptJson();
}
#[ArrayShape(['transactions' => "array"])]
public function calculate(): array
{