+ Fix bugs while upload files

This commit is contained in:
Djery-Tom 2020-06-20 16:09:37 +01:00
parent 9878189ee9
commit b921c059eb
4 changed files with 51 additions and 24 deletions

View File

@ -6,7 +6,6 @@ namespace App\Http\Controllers;
use App\Services\WalletService;
use App\Traits\ApiResponser;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class WalletServiceController extends Controller
{
@ -40,6 +39,13 @@ class WalletServiceController extends Controller
));
}
public function postWithFiles(Request $request)
{
return $this->successResponse($this->walletService->postFiles(
substr($request->getRequestUri(),strlen(env('WALLET_SERVICE_NAME'))+1), $request->all(),$request->header()
));
}
public function put(Request $request)
{
return $this->successResponse($this->walletService->put(

View File

@ -29,6 +29,11 @@ class WalletService
return $this->perfomRequest('POST',$uri,$data,$headers);
}
public function postFiles($uri , $data, $headers)
{
return $this->perfomRequestWithFiles('POST',$uri,$data,$headers);
}
public function get($uri , $data, $headers)
{
return $this->perfomRequest('GET',$uri,$data,$headers);

View File

@ -1,7 +1,10 @@
<?php
namespace App\Traits;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\MultipartStream;
use Illuminate\Http\UploadedFile;
trait ConsumesExternalService
@ -21,37 +24,50 @@ trait ConsumesExternalService
'base_uri' => $this->baseUri,
]);
if(isset($this->key)){
if (isset($this->key)) {
unset($headers['authorization']);
$headers['Authorization'] = $this->key;
}
$contentType = $headers['content-type'][0];
if(strpos($contentType, 'multipart/form-data') !== false){
// $headers['content-type'] = 'multipart/form-data';
// dd($body);
$response = $client->request($method ,$requestUrl , ['multipart' => $this->bodyToMultipart($body),'headers' => $headers]);
}else{
$response = $client->request($method , $requestUrl , ['json'=> $body ,'form_params' => $formParams , 'headers' => $headers]);
}
$response = $client->request($method, $requestUrl, ['json' => $body, 'form_params' => $formParams, 'headers' => $headers]);
return $response->getBody()->getContents();
}
private function bodyToMultipart(array $body){
public function perfomRequestWithFiles($method, $requestUrl, $body = [], $headers = [], $formParams = [])
{
if (isset($this->key)) {
unset($headers['authorization']);
$headers['Authorization'] = $this->key;
}
$client = new Client([
'base_uri' => $this->baseUri,
'headers' => $headers
]);
$request = new Request($method, $requestUrl, [], new MultipartStream($this->bodyToMultipart($body)));
$response = $client->send($request);
return $response->getBody()->getContents();
}
private function bodyToMultipart(array $body)
{
$multipart = [];
foreach ($body as $key => $value){
if( $value instanceof UploadedFile){
array_push($multipart , [
foreach ($body as $key => $value) {
if ($value instanceof UploadedFile) {
array_push($multipart, [
'name' => $key,
'contents' => file_get_contents($value->getRealPath()),
'mine-type'=>$value->getMimeType(),
'mine-type' => $value->getMimeType(),
'filename' => $value->getClientOriginalName(),
// 'contents' => fopen( './test.txt', 'r' ),
'headers' => [ 'Content-type' => 'multipart/form-data']
// 'headers' => [ 'Content-type' => 'application/octet-stream']
// 'Content-type' => 'multipart/form-data',
]);
}else{
array_push($multipart , [
} else {
array_push($multipart, [
'name' => $key,
'contents' => $value,
]);

View File

@ -93,7 +93,7 @@
// Idendification routes
$router->group(['prefix' => '/identifications'] , function () use ($router){
$router->post('','WalletServiceController@post');
$router->post('{id_identification}','WalletServiceController@post');
$router->post('{id_identification}','WalletServiceController@postWithFiles');
$router->get('{id_user}','WalletServiceController@get');
$router->get('verify/{user_phone}','WalletServiceController@get');
});