From c51f27803fe10765b7ca026f961b78a157a5763c Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 29 Sep 2021 18:21:46 +1000 Subject: [PATCH] Remove Unirest dependency --- app/Jobs/HostedMigration.php | 47 +++++++++++++---- app/Services/Migration/AuthService.php | 53 +++++++++++++++---- app/Services/Migration/CompanyService.php | 2 - app/Services/Migration/CompleteService.php | 61 +++++++++++++++++++--- composer.json | 1 - tests/_bootstrap.php.default | 2 +- 6 files changed, 134 insertions(+), 32 deletions(-) diff --git a/app/Jobs/HostedMigration.php b/app/Jobs/HostedMigration.php index d74ae1193087..b96e059b4069 100644 --- a/app/Jobs/HostedMigration.php +++ b/app/Jobs/HostedMigration.php @@ -8,8 +8,9 @@ use App\Models\Account; use App\Models\User; use App\Services\Migration\CompleteService; use App\Traits\GenerateMigrationResources; +use GuzzleHttp\RequestOptions; use Illuminate\Support\Facades\Storage; -use Unirest\Request; +// use Unirest\Request; class HostedMigration extends Job { @@ -78,21 +79,45 @@ class HostedMigration extends Job 'password' => '', ]; - $body = \Unirest\Request\Body::json($body); + $client = new \GuzzleHttp\Client([ + 'headers' => $headers, + ]); - $response = Request::post($url, $headers, $body); + $response = $client->post($url,[ + RequestOptions::JSON => $body, + RequestOptions::ALLOW_REDIRECTS => false + ]); - if (in_array($response->code, [200])) { - - $data = $response->body; - info(print_r($data,1)); - $this->migration_token = $data->token; + if($response->getStatusCode() == 401){ + info($response->getBody()); + + } elseif ($response->getStatusCode() == 200) { + + $message_body = json_decode($response->getBody(), true); + + $this->migration_token = $message_body['token']; } else { - info("getting token failed"); - info($response->raw_body); + info(json_decode($response->getBody()->getContents())); - } + } + + + // $body = \Unirest\Request\Body::json($body); + + // $response = Request::post($url, $headers, $body); + + // if (in_array($response->code, [200])) { + + // $data = $response->body; + // info(print_r($data,1)); + // $this->migration_token = $data->token; + + // } else { + // info("getting token failed"); + // info($response->raw_body); + + // } return $this; } diff --git a/app/Services/Migration/AuthService.php b/app/Services/Migration/AuthService.php index 3952e81d5070..0218362013be 100644 --- a/app/Services/Migration/AuthService.php +++ b/app/Services/Migration/AuthService.php @@ -13,8 +13,9 @@ namespace App\Services\Migration; -use Unirest\Request; -use Unirest\Request\Body; +use GuzzleHttp\RequestOptions; +// use Unirest\Request; +// use Unirest\Request\Body; class AuthService { @@ -51,25 +52,57 @@ class AuthService 'password' => $this->password, ]; - $body = Body::json($data); + $client = new \GuzzleHttp\Client([ + 'headers' => $this->getHeaders(), + ]); - $response = Request::post($this->getUrl(), $this->getHeaders(), $body); + $response = $client->post($this->getUrl(),[ + RequestOptions::JSON => $data, + RequestOptions::ALLOW_REDIRECTS => false + ]); - if (in_array($response->code, [401])) { - info($response->raw_body); + if($response->getStatusCode() == 401){ + info($response->getBody()); $this->isSuccessful = false; - $this->processErrors($response->body->message); - } elseif (in_array($response->code, [200])) { + $this->processErrors($response->getBody()); + } elseif ($response->getStatusCode() == 200) { + + $message_body = json_decode($response->getBody(), true); + + info(print_r($message_body,1)); + $this->isSuccessful = true; - $this->token = $response->body->data[0]->token->token; + $this->token = $message_body['data'][0]['token']['token']; } else { - info($response->raw_body); + info(json_decode($response->getBody()->getContents())); $this->isSuccessful = false; $this->errors = [trans('texts.migration_went_wrong')]; } + + //return $response->getBody(); + + // $body = Body::json($data); + + // $response = Request::post($this->getUrl(), $this->getHeaders(), $body); + + // if (in_array($response->code, [401])) { + // info($response->raw_body); + + // $this->isSuccessful = false; + // $this->processErrors($response->body->message); + // } elseif (in_array($response->code, [200])) { + // $this->isSuccessful = true; + // $this->token = $response->body->data[0]->token->token; + // } else { + // info($response->raw_body); + + // $this->isSuccessful = false; + // $this->errors = [trans('texts.migration_went_wrong')]; + // } + return $this; } diff --git a/app/Services/Migration/CompanyService.php b/app/Services/Migration/CompanyService.php index 1709a3342c83..5ad036247bdd 100644 --- a/app/Services/Migration/CompanyService.php +++ b/app/Services/Migration/CompanyService.php @@ -3,8 +3,6 @@ namespace App\Services\Migration; use App\Models\Account; -use Unirest\Request; -use Unirest\Request\Body; class CompanyService { diff --git a/app/Services/Migration/CompleteService.php b/app/Services/Migration/CompleteService.php index 171576e66458..c7ce22b5bdbd 100644 --- a/app/Services/Migration/CompleteService.php +++ b/app/Services/Migration/CompleteService.php @@ -2,8 +2,9 @@ namespace App\Services\Migration; +use GuzzleHttp\RequestOptions; use Illuminate\Support\Facades\Storage; -use Unirest\Request; +// use Unirest\Request; class CompleteService { @@ -45,22 +46,48 @@ class CompleteService foreach ($this->data as $companyKey => $companyData) { - $data[] = [ + $data = [ 'company_index' => $companyKey, 'company_key' => $companyData['data']['company']['company_key'], 'force' => $companyData['force'], + 'contents' => 'name', + 'name' => $companyKey, ]; - $files[$companyKey] = $companyData['file']; + $payload[$companyKey] = [ + 'contents' => json_encode($data), + 'name' => $companyData['data']['company']['company_key'], + ]; + + $files[] = [ + 'name' => $companyKey, + 'company_index' => $companyKey, + 'company_key' => $companyData['data']['company']['company_key'], + 'force' => $companyData['force'], + 'contents' => file_get_contents($companyData['file']), + 'filename' => basename($companyData['file']), + 'Content-Type' => 'application/zip' + ]; } - $body = \Unirest\Request\Body::multipart(['companies' => json_encode($data)], $files); + $client = new \GuzzleHttp\Client( + [ + 'headers' => $this->getHeaders(), + ]); - $response = Request::post($this->getUrl(), $this->getHeaders(), $body); + + $payload_data = [ + 'multipart'=> array_merge($files, $payload), + ]; + + info(print_r($payload_data,1)); + $response = $client->request("POST", $this->getUrl(),$payload_data); + + if($response->getStatusCode() == 200){ - if (in_array($response->code, [200])) { $this->isSuccessful = true; - } else { + return json_decode($response->getBody(),true); + }else { info($response->raw_body); $this->isSuccessful = false; @@ -70,6 +97,26 @@ class CompleteService } return $this; + + + + + // $body = \Unirest\Request\Body::multipart(['companies' => json_encode($data)], $files); + + // $response = Request::post($this->getUrl(), $this->getHeaders(), $body); + + // if (in_array($response->code, [200])) { + // $this->isSuccessful = true; + // } else { + // info($response->raw_body); + + // $this->isSuccessful = false; + // $this->errors = [ + // 'Oops, something went wrong. Migration can\'t be processed at the moment. Please checks the logs.', + // ]; + // } + + return $this; } public function isSuccessful() diff --git a/composer.json b/composer.json index 5999169f24a0..c695823efd9c 100644 --- a/composer.json +++ b/composer.json @@ -68,7 +68,6 @@ "league/fractal": "0.13.*", "lokielse/omnipay-alipay": "~1.4", "maatwebsite/excel": "dev-carbon#8b17952", - "mashape/unirest-php": "^3.0.4", "meebio/omnipay-creditcall": "dev-master", "meebio/omnipay-secure-trading": "dev-master", "mfauveau/omnipay-pacnet": "~2.0", diff --git a/tests/_bootstrap.php.default b/tests/_bootstrap.php.default index f532c7d310f5..29467eecf401 100644 --- a/tests/_bootstrap.php.default +++ b/tests/_bootstrap.php.default @@ -2,7 +2,7 @@ // This is global bootstrap for autoloading use Codeception\Util\Fixtures; -Fixtures::add('url', 'http://www.ninja.test:8000'); +Fixtures::add('url', 'http://ninja-master.test:8000'); Fixtures::add('username', 'user@example.com'); Fixtures::add('permissions_username', 'permissions@example.com'); Fixtures::add('password', 'password');