From 82369012f55563e4bf8edd4b3dd5319c25bf5cb4 Mon Sep 17 00:00:00 2001 From: Piotr Miazga Date: Wed, 3 Feb 2016 23:34:35 +1300 Subject: [PATCH] Fixed Importing users - fixed RequetBulder, now properly sends multipart data - fixed Jobs method names --- src/API/Helpers/RequestBuilder.php | 39 +++++++++++++++++++----------- src/API/Jobs.php | 8 +++--- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/API/Helpers/RequestBuilder.php b/src/API/Helpers/RequestBuilder.php index f0f4f99f..34a793f4 100644 --- a/src/API/Helpers/RequestBuilder.php +++ b/src/API/Helpers/RequestBuilder.php @@ -53,6 +53,7 @@ public function addPath($name, $argument = null) { public function addPathVariable($variable) { $this->path[] = $variable; + return $this; } public function getUrl() { @@ -85,18 +86,18 @@ public function dump() { return $this; } - public function addFile($file_path) { - $this->files[] = $filePath; + public function addFile($field, $file_path) { + $this->files[$field] = $file_path; + return $this; } public function addFormParam($key, $value) { $this->form_params[$key] = $value; + return $this; } public function call() { - $client = new Client(); - $method = $this->method; try { @@ -106,15 +107,8 @@ public function call() { ]; if (!empty($this->files)) { - foreach($this->files as $file) { - $data['multipart'][] = [ - 'name' => basename($file), - 'contents' => $file - ]; - } - } - - if (!empty($this->form_params)) { + $data['multipart'] = $this->buildMultiPart(); + } else if (!empty($this->form_params)) { $data['form_params'] = $this->form_params; } @@ -126,7 +120,6 @@ public function call() { } catch (RequestException $e) { throw $e; } - } public function withHeaders($headers) { @@ -170,4 +163,22 @@ public function withParams($params) { return $this; } + private function buildMultiPart() { + $multipart = array(); + + foreach($this->files as $field => $file) { + $multipart[] = [ + 'name' => $field, + 'contents' => fopen($file, 'r') + ]; + } + foreach($this->form_params as $param => $value) { + $multipart[] = [ + 'name' => $param, + 'contents' => $value + ]; + } + return $multipart; + } + } diff --git a/src/API/Jobs.php b/src/API/Jobs.php index 52d9975b..19970971 100644 --- a/src/API/Jobs.php +++ b/src/API/Jobs.php @@ -20,17 +20,17 @@ public function get($id) { ->call(); } - public function sendVerificationEmail($file_path, $connection_id) { + public function importUsers($file_path, $connection_id) { return $this->apiClient->post() ->jobs() - ->addPath('verification-email') - ->addFile($file_path) + ->addPath('users-imports') + ->addFile('users', $file_path) ->addFormParam('connection_id', $connection_id) ->call(); } - public function importUsers($user_id) { + public function sendVerificationEmail($user_id) { return $this->apiClient->post() ->jobs()