Skip to content

Commit

Permalink
Added File upload support
Browse files Browse the repository at this point in the history
  • Loading branch information
IsraelOrtuno committed Dec 29, 2015
1 parent 386942e commit e8800a4
Showing 1 changed file with 40 additions and 9 deletions.
49 changes: 40 additions & 9 deletions src/Http/PipedriveClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ public function get($url, $parameters = [])
{
$options = $this->getClient()
->getConfig();
array_set(
$options,
'query',
array_merge($parameters, $options['query'])
);
array_set($options, 'query', array_merge($parameters, $options['query']));

// For this particular case we have to include the parameters into the
// URL query. Merging the request default query configuration to the
Expand All @@ -59,15 +55,50 @@ public function get($url, $parameters = [])
/**
* Perform a POST request.
*
* @param $url
* @param $url
* @param array $parameters
* @return Response
*/
public function post($url, $parameters = [])
{
$request = new GuzzleRequest('POST', $url);
$form = 'form_params';

// If any file key is found, we will assume we have to convert the data
// into the multipart array structure. Otherwise, we will perform the
// request as usual using the form_params with the given parameters.
if (isset($parameters['file'])) {
$form = 'multipart';
$parameters = $this->multipart($parameters);
}

return $this->execute($request, ['form_params' => $parameters]);
return $this->execute($request, [$form => $parameters]);
}

/**
* Convert the parameters into a multipart structure.
*
* @param array $parameters
* @return array
*/
protected function multipart(array $parameters)
{
if (! ($file = $parameters['file']) instanceof \SplFileInfo) {
throw new \InvalidArgumentException('File must be an instance of \SplFileInfo.');
}

$result = [];
$content = file_get_contents($file->getPathname());

foreach (array_except($parameters, 'file') as $key => $value) {
$result[] = ['name' => $key, 'contents' => (string) $value];
}
// Will convert every element of the array into a format accepted by the
// multipart encoding standards. It will also add a special item which
// includes the file key name, the content of the file and its name.
$result[] = ['name' => 'file', 'contents' => $content, 'filename' => $file->getFilename()];

return $result;
}

/**
Expand Down Expand Up @@ -102,8 +133,8 @@ public function delete($url, $parameters = [])
* Execute the request and returns the Response object.
*
* @param GuzzleRequest $request
* @param array $options
* @param null $client
* @param array $options
* @param null $client
* @return Response
*/
protected function execute(GuzzleRequest $request, array $options = [], $client = null)
Expand Down

0 comments on commit e8800a4

Please sign in to comment.