-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new MultipartRequest class to handle extra parameters
- Loading branch information
Showing
8 changed files
with
177 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
================== | ||
Multipart requests | ||
================== | ||
|
||
The MultipartRequest class can be used if you need a FluentRequest that has more parameters to be set on individual parts of a multipart request. | ||
|
||
The name is a slight misnomer, because either of the other two Request classes (SimpleRequest and FluentRequest) | ||
will also end up being multipart requests if you pass any parameters of type Resource_. | ||
|
||
.. _Resource: http://php.net/manual/en/resource.php | ||
|
||
To use a MultipartRequest you must first set the main parameters, and then you can add additional "multipart parameters" to any of the parameters you've set. | ||
(You will get an Exception if you try to set a multipart parameter for a main parameter that doesn't exist yet.) | ||
|
||
For example, to add a ``Content-Disposition`` header to a parameter named ``param1``:: | ||
|
||
$contentDisposition = 'form-data; name="param1"; filename="a_filename.png"'; | ||
$request = MultipartRequest::factory() | ||
->setParams( [ 'param1' => 'Lorem ipsum' ] ) | ||
->setAction( 'actionname' ) | ||
->setMultipartParams( [ | ||
'param1' => [ | ||
'headers' => [ 'Content-Disposition' => $contentDisposition ], | ||
], | ||
] ); | ||
$response = $api->postRequest( $request ); | ||
|
||
(For details of creating the ``$api`` object in this example, see :ref:`quickstart`.) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.. _quickstart: | ||
|
||
========== | ||
Quickstart | ||
========== | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace Mediawiki\Api; | ||
|
||
use Exception; | ||
|
||
/** | ||
* A MultipartRequest is the same as a FluentRequest with additional support for setting request | ||
* parameters (both normal parameters and headers) on multipart requests. | ||
* | ||
* @link http://docs.guzzlephp.org/en/stable/request-options.html#multipart | ||
* | ||
* @since 2.4.0 | ||
*/ | ||
class MultipartRequest extends FluentRequest { | ||
|
||
/** @var mixed[] */ | ||
protected $multipartParams = []; | ||
|
||
/** | ||
* Check the structure of a multipart parameter array. | ||
* | ||
* @param mixed[] $params The multipart parameters to check. | ||
* | ||
* @throws Exception | ||
*/ | ||
protected function checkMultipartParams( $params ) { | ||
foreach ( $params as $key => $val ) { | ||
if ( !is_array( $val ) ) { | ||
throw new Exception( "Parameter '$key' must be an array." ); | ||
} | ||
if ( !in_array( $key, array_keys( $this->getParams() ) ) ) { | ||
throw new Exception( "Parameter '$key' is not already set on this request." ); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Set all multipart parameters, replacing all existing ones. | ||
* | ||
* Each key of the array passed in here must be the name of a parameter already set on this | ||
* request object. | ||
* | ||
* @param mixed[] $params The multipart parameters to use. | ||
* @return $this | ||
*/ | ||
public function setMultipartParams( $params ) { | ||
$this->checkMultipartParams( $params ); | ||
$this->multipartParams = $params; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Add extra multipart parameters. | ||
* | ||
* Each key of the array passed in here must be the name of a parameter already set on this | ||
* request object. | ||
* | ||
* @param mixed[] $params The multipart parameters to add to any already present. | ||
* | ||
* @return $this | ||
*/ | ||
public function addMultipartParams( $params ) { | ||
$this->checkMultipartParams( $params ); | ||
$this->multipartParams = array_merge( $this->multipartParams, $params ); | ||
return $this; | ||
} | ||
|
||
/** | ||
* Get all multipart request parameters. | ||
* | ||
* @return mixed[] | ||
*/ | ||
public function getMultipartParams() { | ||
return $this->multipartParams; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Mediawiki\Api\Test\Unit; | ||
|
||
use Exception; | ||
use Mediawiki\Api\MultipartRequest; | ||
use PHPUnit_Framework_TestCase; | ||
|
||
class MultipartRequestTest extends PHPUnit_Framework_TestCase { | ||
|
||
public function testBasics() { | ||
$request = new MultipartRequest(); | ||
$this->assertEquals( [], $request->getMultipartParams() ); | ||
|
||
// One parameter. | ||
$request->setParam( 'testparam', 'value' ); | ||
$request->addMultipartParams( [ 'testparam' => [ 'lorem' => 'ipsum' ] ] ); | ||
$this->assertEquals( | ||
[ 'testparam' => [ 'lorem' => 'ipsum' ] ], | ||
$request->getMultipartParams() | ||
); | ||
|
||
// Another parameter. | ||
$request->setParam( 'testparam2', 'value' ); | ||
$request->addMultipartParams( [ 'testparam2' => [ 'lorem2' => 'ipsum2' ] ] ); | ||
$this->assertEquals( | ||
[ | ||
'testparam' => [ 'lorem' => 'ipsum' ], | ||
'testparam2' => [ 'lorem2' => 'ipsum2' ], | ||
], | ||
$request->getMultipartParams() | ||
); | ||
} | ||
|
||
/** | ||
* You are not allowed to set multipart parameters on a parameter that doesn't exist. | ||
* @expectedException Exception | ||
* @expectedExceptionMessage Parameter 'testparam' is not already set on this request. | ||
*/ | ||
public function testParamNotYetSet() { | ||
$request = new MultipartRequest(); | ||
$request->addMultipartParams( [ 'testparam' => [] ] ); | ||
} | ||
} |