Skip to content

Commit

Permalink
Migrate to amphp
Browse files Browse the repository at this point in the history
  • Loading branch information
rdlowrey committed Sep 24, 2014
1 parent 6fb93fd commit 01882d4
Show file tree
Hide file tree
Showing 62 changed files with 286 additions and 308 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
@@ -1,7 +1,14 @@
#### v1.0.0-rc1
#### v1.0.0-beta2

- Issue #51: Fix composer dep declarations for easy install
- Issue #54: Fix bug preventing redirect if request method was not GET or HEAD
- Migrate to amphp framework

##### BC BREAKS:

- Because the library has been migrated to the amphp concurrency framework all `Artax` namespace
declarations in your code must be updated to `Amp\Artax`. Additionally, return values from both
`Client::request()` and `Client::requestMulti()` now implement `Amp\Promise` (was `After\Promise`).

#### v1.0.0-beta

Expand Down
67 changes: 36 additions & 31 deletions README.md
Expand Up @@ -7,11 +7,16 @@ there yet. Use at your own risk! The current release roadmap is as follows:

- v1.0.0 (Oct. 10, 2014 - tentative, depending on RC period length)

- v1.0.0-rc1 (Sep. 24, 2014)
- v1.0.0-rc1 (Oct. 3, 2014)

Unforeseen bugfixes only. The RC period should be short-lived. If a non-trivial number of bugs are
discovered during the RC phase there may be multiple individual RC tags.

- v1.0.0-beta2 (Sep. 24, 2014)

* Bugfixes
* Migrate to [amphp](https://github.com/amphp) framework

- v1.0.0-beta (Sep. 17, 2014)

* bagder cert package no longer needed in composer.json
Expand Down Expand Up @@ -63,12 +68,12 @@ dependency on PHP's `curl_*` API and requires no non-standard PHP extensions.
##### Installation

```bash
$ git clone https://github.com/rdlowrey/Artax.git
$ cd Artax
$ git clone https://github.com/amphp/artax.git
$ cd artax
$ composer.phar install
```

The relevant composer/packagist lib is `rdlowrey/artax`.
The relevant composer/packagist lib is `amphp/artax`.



Expand All @@ -86,7 +91,7 @@ as the request parameter:
<?php

try {
$response = (new Artax\Client)->request('http://www.google.com')->wait();
$response = (new Amp\Artax\Client)->request('http://www.google.com')->wait();
printf(
"\nHTTP/%s %d %s\n",
$response->getProtocol(),
Expand All @@ -105,13 +110,13 @@ For non-trivial requests Artax allows you to construct messages piece-by-piece.
sets the request method to POST and assigns an entity body. HTTP veterans will notice that
we don't bother to set a `Content-Length` or `Host` header. Artax will automatically add/normalize
missing headers for us so we don't need to worry about it. The only property that _MUST_ be assigned
when sending an `Artax\Request` is the absolute *http://* or *https://* request URI:
when sending an `Amp\Artax\Request` is the absolute *http://* or *https://* request URI:

```php
<?php

try {
$request = (new Artax\Request)
$request = (new Amp\Artax\Request)
->setUri('http://httpbin.org/post')
->setProtocol('1.1')
->setMethod('POST')
Expand All @@ -122,7 +127,7 @@ try {
])
;

$response = (new Artax\Client)->request($request)->wait();
$response = (new Amp\Artax\Client)->request($request)->wait();

} catch (Exception $error) {
echo $error;
Expand All @@ -146,24 +151,24 @@ Assume `httpbin.org/post` contains the following HTML form:
</form>
```

We can easily submit this form using the `Artax\FormBody` API:
We can easily submit this form using the `Amp\Artax\FormBody` API:

```php
<?php

$body = (new Artax\FormBody)
$body = (new Amp\Artax\FormBody)
->addField('name', 'Zoroaster')
->addFileField('file1', '/hard/path/to/some/file1')
->addFileField('file2', '/hard/path/to/some/file2')
;

$request = (new Artax\Request)
$request = (new Amp\Artax\Request)
->setUri('http://httpbin.org/post')
->setMethod('POST')
->setBody($body)
;

$response = (new Artax\Client)->request($request)->wait();
$response = (new Amp\Artax\Client)->request($request)->wait();
```

##### Parallel Requests
Expand All @@ -174,7 +179,7 @@ same thing:

```php
<?php
$client = new Artax\Client;
$client = new Amp\Artax\Client;

// Here we pass dispatch two requests at the same time
$arrayOfPromises = $client->requestMulti([
Expand All @@ -191,14 +196,14 @@ $comboPromise = After\all([$googlePromise, $bingPromise]);
list($googleResponse, $bingResponse) = $comboPromise->wait();
```

Remember that `Artax\Client::request()` *always* returns a promise instance. So if we want to
Remember that `Amp\Artax\Client::request()` *always* returns a promise instance. So if we want to
specify individualized callbacks for progress events on those promises we're perfectly able to
do so. In the below example we use the `Promise::when()` method (which accepts an error-first
callback) to react to the completion of an individual response:

```php
<?php
$client = new Artax\Client;
$client = new Amp\Artax\Client;

list($googlePromise, $bingPromise) = $client->request([
'http://www.google.com',
Expand Down Expand Up @@ -233,7 +238,7 @@ combinator function. Consider:
```php
<?php

$arrayOfPromises = (new Artax\Client)->request([
$arrayOfPromises = (new Amp\Artax\Client)->request([
'google' => 'http://www.google.com',
'news' => 'http://news.google.com',
'bing' => 'http://www.bing.com',
Expand All @@ -258,7 +263,7 @@ foreach ($responses as $key => $response) {

##### Progress Events

Once again we note that `Artax\Client::request()` always returns a promise instance. This means
Once again we note that `Amp\Artax\Client::request()` always returns a promise instance. This means
we can use the `Promise::watch()` method to observe updates/events for a particular request:

```php
Expand All @@ -271,31 +276,31 @@ we can use the `Promise::watch()` method to observe updates/events for a particu
function myNotifyCallback(array $notifyData) {
$event = array_shift($notifyData);
switch ($event) {
case Artax\Notify::SOCK_PROCURED:
case Amp\Artax\Notify::SOCK_PROCURED:
echo "SOCK_PROCURED\n";
break;
case Artax\Notify::SOCK_DATA_IN:
case Amp\Artax\Notify::SOCK_DATA_IN:
echo "SOCK_DATA_IN\n";
break;
case Artax\Notify::SOCK_DATA_OUT:
case Amp\Artax\Notify::SOCK_DATA_OUT:
echo "SOCK_DATA_OUT\n";
break;
case Artax\Notify::REQUEST_SENT:
case Amp\Artax\Notify::REQUEST_SENT:
echo "REQUEST_SENT\n";
break;
case Artax\Notify::RESPONSE_HEADERS:
case Amp\Artax\Notify::RESPONSE_HEADERS:
echo "RESPONSE_HEADERS\n";
break;
case Artax\Notify::RESPONSE_BODY_DATA:
case Amp\Artax\Notify::RESPONSE_BODY_DATA:
echo "RESPONSE_BODY_DATA\n";
break;
case Artax\Notify::RESPONSE:
case Amp\Artax\Notify::RESPONSE:
echo "RESPONSE\n";
break;
case Artax\Notify::REDIRECT:
case Amp\Artax\Notify::REDIRECT:
echo "REDIRECT\n";
break;
case Artax\Notify::ERROR:
case Amp\Artax\Notify::ERROR:
echo "ERROR\n";
break;
}
Expand All @@ -306,7 +311,7 @@ function myNotifyCallback(array $notifyData) {
* notifications simply pass a callback to Promise::watch() as demonstrated
* below.
*/
$promise = (new Artax\Client)->request('http://www.google.com');
$promise = (new Amp\Artax\Client)->request('http://www.google.com');
$promise->watch('myNotifyCallback');
$response = $promise->wait();

Expand All @@ -329,17 +334,17 @@ printf(
##### Progress Bars

Generating a progress bar depends on a few details from the HTTP spec regarding message size. To
make this easier for end users Artax exposes the `Artax\Progress` object which makes generating
make this easier for end users Artax exposes the `Amp\Artax\Progress` object which makes generating
a usable progress bar on a per-request basis trivial. Consider:

```php
<?php
$promise = (new Artax\Client)->request($request);
$promise->watch(new Artax\Progress(function($update) {
$promise = (new Amp\Artax\Client)->request($request);
$promise->watch(new Amp\Artax\Progress(function($update) {
printf(
"\r%s %s%%\r",
$update['bar'],
round($update['percent_complete'] * 100)
round($update['fraction_complete'] * 100)
);
}));
$response = $promise->wait();
Expand Down
25 changes: 0 additions & 25 deletions TODO.md

This file was deleted.

22 changes: 13 additions & 9 deletions composer.json
@@ -1,5 +1,5 @@
{
"name": "rdlowrey/artax",
"name": "amphp/artax",
"homepage": "https://github.com/rdlowrey/Artax",
"description": "Non-blocking HTTP/1.1 client",
"keywords": [
Expand All @@ -19,28 +19,32 @@
}
],
"require": {
"php": ">=5.4.0",
"rdlowrey/alert": "~0.10",
"rdlowrey/after": "~0.2",
"rdlowrey/acesync": "~0.2"
"php": ">=5.5.0",
"amphp/amp": "~0.11",
"rdlowrey/nbsock": "~0.4"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/rdlowrey/Alert.git"
"url": "https://github.com/amphp/amp.git"
},
{
"type": "vcs",
"url": "https://github.com/rdlowrey/After.git"
"url": "https://github.com/amphp/dns.git"
},
{
"type": "vcs",
"url": "https://github.com/rdlowrey/Acesync.git"
"url": "https://github.com/rdlowrey/nbsock.git"
}
],
"autoload": {
"psr-4": {
"Artax\\": "lib/"
"Amp\\Artax\\": "lib/"
}
},
"autoload-dev": {
"psr-4": {
"Amp\\Test\\Artax\\": "test/"
}
},
"extra": {
Expand Down
4 changes: 2 additions & 2 deletions examples/001_get_request.php
Expand Up @@ -4,7 +4,7 @@

try {
// Instantiate the HTTP client
$client = new Artax\Client;
$client = new Amp\Artax\Client;

// Make an asynchronous HTTP request
$promise = $client->request('http://httpbin.org/user-agent');
Expand All @@ -22,7 +22,7 @@
$response->getReason()
);

} catch (Artax\ClientException $error) {
} catch (Amp\Artax\ClientException $error) {
// If something goes wrong the Promise::wait() call will throw the relevant
// exception. The Client::request() method itself will never throw.
echo $error;
Expand Down
6 changes: 3 additions & 3 deletions examples/002_custom_request.php
Expand Up @@ -4,10 +4,10 @@

try {
// Instantiate the HTTP client
$client = new Artax\Client;
$client = new Amp\Artax\Client;

// Let's build up a custom Request object
$request = (new Artax\Request)
$request = (new Amp\Artax\Request)
->setMethod('GET') // <-- defaults to GET if not assigned, so this isn't strictly necessary
->setUri('http://httpbin.org/user-agent')
->setHeader('X-My-Header', 'some-value')
Expand All @@ -29,7 +29,7 @@
$response->getReason()
);

} catch (Artax\ClientException $e) {
} catch (Amp\Artax\ClientException $e) {
// If something goes wrong the Promise::wait() call will throw the relevant
// exception. The Client::request() method itself will never throw.
echo $e;
Expand Down
7 changes: 4 additions & 3 deletions examples/003_post_request.php
Expand Up @@ -4,10 +4,11 @@

try {
// Instantiate the HTTP client
$client = new Artax\Client;
$client = new Amp\Artax\Client;
$client->setOption(Amp\Artax\Client::OP_VERBOSITY, Amp\Artax\Client::VERBOSE_ALL);

// Let's build up a custom Request object
$request = (new Artax\Request)
$request = (new Amp\Artax\Request)
->setMethod('POST')
->setUri('http://httpbin.org/post')
->setBody('zanzibar!')
Expand All @@ -30,7 +31,7 @@
$response->getBody()
);

} catch (Artax\ClientException $e) {
} catch (Amp\Artax\ClientException $e) {
// If something goes wrong the Promise::wait() call will throw the relevant
// exception. The Client::request() method itself will never throw.
echo $e;
Expand Down
6 changes: 3 additions & 3 deletions examples/004_cookies.php
Expand Up @@ -4,11 +4,11 @@

try {
// Instantiate the HTTP client
$client = new Artax\Client;
$client = new Amp\Artax\Client;

// Enable verbose sends so we can see our raw request messages in the console
// as they're sent to the server.
$client->setOption(Artax\Client::OP_VERBOSITY, Artax\Client::VERBOSE_SEND);
$client->setOption(Amp\Artax\Client::OP_VERBOSITY, Amp\Artax\Client::VERBOSE_SEND);

// This request will receive and store google's Set-Cookie headers.
$response = $client->request('http://www.google.com/')->wait();
Expand All @@ -17,7 +17,7 @@
// In your console you'll see that this second request contains a Cookie header.
$response = $client->request('http://www.google.com/')->wait();

} catch (Artax\ClientException $e) {
} catch (Amp\Artax\ClientException $e) {
// If something goes wrong the Promise::wait() call will throw the relevant
// exception. The Client::request() method itself will never throw.
echo $e;
Expand Down

0 comments on commit 01882d4

Please sign in to comment.