Skip to content

Commit

Permalink
Merge pull request #1 from pn-neutrino/0.1
Browse files Browse the repository at this point in the history
0.1-RC1
  • Loading branch information
Ark4ne committed Apr 28, 2017
2 parents d9bdc1a + 09a0898 commit a6e9848
Show file tree
Hide file tree
Showing 45 changed files with 4,438 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/.idea
/vendor
composer.phar
composer.lock
.php_cs.cache
.DS_Store
Thumbs.db
26 changes: 26 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
language: php

sudo: required

php:
- 5.6
- 7.0
- 7.1

install:
- composer self-update
- travis_retry composer install --no-interaction --prefer-source --dev

before_script:
- composer dump-autoload

script:
- php -S 127.0.0.1:8000 tests/server.php > /dev/null 2>&1 &
- mkdir -p build/logs
- vendor/bin/phpunit --bootstrap ./tests/bootstrap.php --configuration ./phpunit.xml --coverage-clover build/logs/clover.xml

after_script:
- php vendor/bin/coveralls -v

after_success:
- coveralls
247 changes: 245 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,245 @@
# http
Neutrino Http Client
Neutrino : Http Client component
==============================================
[![Build Status](https://travis-ci.org/pn-neutrino/http.svg?branch=master)](https://travis-ci.org/pn-neutrino/http) [![Coverage Status](https://coveralls.io/repos/github/pn-neutrino/http/badge.svg?branch=master)](https://coveralls.io/github/pn-neutrino/http)

Http Client library using Curl or HttpStream.

# Provider

## Curl
require curl extension.

#### How use :
```php
use \Neutrino\Http\Provider\Curl as HttpCurl;
use \Neutrino\Http\Method;

$curl = new HttpCurl;

$response = $curl
->get('http://www.google.com', ['foo' => 'bar'], ['Accept' => 'text/plain'])
->send();

$response->code; // HTTP Status Code
```

### Curl\Streaming
Curl\Stream allows you to work with large queries, by recovers content part by part.

#### How use :
```php
use \Neutrino\Http\Provider\Curl\Streaming as HttpCurlStream;
use \Neutrino\Http\Method;

$curl = new HttpCurlStream;

$response = $curl
->get('http://www.google.com')
->on(HttpCurlStream::EVENT_START, function (HttpCurlStream $curl) {
// Start to download response body
// Header are fully loaded when the event are raised
})
->on(HttpCurlStream::EVENT_PROGRESS, function (HttpCurlStream $curl, $content) {
// Download progress
// $content contain the response part
})
->send();
```

Transfer huge data, without overloading the php memory :
```php
$curl
->get('http://www.google.com')
->on(HttpCurlStream::EVENT_START, function (HttpCurlStream $curl) {
if ($curl->response->header->has('Content-Length')) {
header('Content-Length: ' . $curl->response->header->get('Content-Length'));
}
})
->on(HttpCurlStream::EVENT_PROGRESS, function (HttpCurlStream $curl, $content) {
echo $content;
ob_flush();
flush();
// => Direct echo contents & flush the output (free memory)
})
->send();
```

Download huge file, without overloading the php memory :
```php
$resource = fopen($path, 'w');

$curl
->get('http://www.google.com')
->on(HttpCurlStream::EVENT_PROGRESS, function (HttpCurlStream $curl, $content) use ($resource) {
fwrite($resource, $content, strlen($content));
})
->send();

fclose($resource);
```

## StreamContext
StreamContext make HTTP call via the php wrapper.

This require you have "allow_url_fopen" configuration value set to '1'.

#### How use :
```php
use \Neutrino\Http\Provider\StreamContext as HttpStreamCtx;
use \Neutrino\Http\Method;

$streamCtx = new HttpStreamCtx;

$response = $streamCtx
->get('http://www.google.com', ['foo' => 'bar'], ['Accept' => 'text/plain'])
->send();

$response->code; // HTTP Status Code
```

### StreamContext\Streaming
Such as Curl\Streaming, StreamContext\Streaming allows you to work with large queries, by recovers content part by part.

#### How use :
```php
use \Neutrino\Http\Provider\StreamContext\Streaming as HttpStreamCtxStreaming;
use \Neutrino\Http\Method;

$streamCtx = new HttpStreamCtxStreaming;

$response = $streamCtx
->get('http://www.google.com')
->on(HttpStreamCtxStreaming::EVENT_START, function (HttpStreamCtxStreaming $streamCtx) {
// Start to download response body
// Header are fully loaded when the event are raised
})
->on(HttpStreamCtxStreaming::EVENT_PROGRESS, function (HttpStreamCtxStreaming $streamCtx, $content) {
// Download progress
// $content contain the response part
})
->send();
```

# Auth
Authentication is a request component.

## Auth\Basic
Auth\Basic provides the elements to configure a call with an Basic Authorization.

#### How use :
```php
use \Neutrino\Http\Auth\Basic as AuthBasic;
use \Neutrino\Http\Provider\StreamContext as HttpStreamCtx;
use \Neutrino\Http\Method;

$streamCtx = new HttpStreamCtx;

$response = $streamCtx
->get('http://www.google.com')
->setAuth(new AuthBasic('user', 'pass'))
->send();
```

## Auth\Curl
Specific for Curl provider.

Auth\Curl provides the elements to build a call with Curl Auth.

#### How use :
```php
use \Neutrino\Http\Auth\Curl as AuthCurl;
use \Neutrino\Http\Provider\Curl as HttpCurl;
use \Neutrino\Http\Method;

$curl = new HttpCurl;

$response = $curl
->get('http://www.google.com')
->setAuth(new AuthCurl(CURLAUTH_BASIC | CURLAUTH_DIGEST, 'user', 'pass'))
->send();
```

## Custom Auth Component
You can easily make your own Auth Component :

```php
namespace MyLib\Http\Auth;

use Neutrino\Http\Request;
use Neutrino\Http\Contract\Request\Component;

class Hmac implements Component
{
private $id;
private $value;

public function __construct($id, $value)
{
$this->id = $id;
$this->value = $value;
}

public function build(Request $request)
{
$date = date('D, d M Y H:i:s', time());
$signature = urlencode(base64_encode(hash_hmac('sha1', "date: $date", $this->value, true)));

$request
->setHeader('Date', $date)
->setHeader('Authorization', 'Signature keyId="' . $this->id . '",algorithm="hmac-sha1",signature="' . $signature . '"');
}
}
```
```php
use \MyLib\Http\Auth\Hmac as AuthHmac;
use \Neutrino\Http\Provider\Curl as HttpCurl;
use \Neutrino\Http\Method;

$curl = new HttpCurl;

$response = $curl
->get('http://www.google.com')
->setAuth(new AuthHmac('key_id', 'key_value'))
->send();
```

# Response
## Basic
```php
$response->code; // HTTP Status Code
$response->status; // HTTP Status Message
$response->header; // Response Headers
$response->body; // Response Body
```
## Provider Info
```php
$response->errorCode; // Provider Error Code
$response->error; // Provider Error Message
$response->providerDatas; // All Provider Information (if available)
```

## Parse

```php
use \Neutrino\Http\Parser;

// Json Body => Object
$jsonObject = $response->parse(Parser\Json::class)->data;

// Xml Body => SimpleXMLElement
$xmlElement = $response->parse(Parser\Xml::class)->data;

// Xml Body => array
$xmlArray = $response->parse(Parser\XmlArray::class)->data;

// Other exemple : (PHP7)
$response->parse(new class implements Parser\Parserize
{
public function parse($body)
{
return unserialize($body);
}
});

$response->data; // Unserialized body
```
51 changes: 51 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "neutrino/http",
"description": "The Neutrino Http package.",
"keywords": [
"neutrino",
"nucleon",
"http",
"http client",
"api",
"json",
"requests",
"rest",
"restful",
"web service",
"xml",
"curl",
"stream",
"stream context",
"streaming"
],
"license": "MIT",
"homepage": "https://phalcon-nucleon.github.io",
"support": {
"issues": "https://github.com/pn-neutrino/http/issues",
"source": "https://github.com/pn-neutrino/http"
},
"authors": [
{
"name": "Ark4ne (Guillaume Allegret)",
"email": "gui.allegret@gmail.com"
}
],
"require": {
"php": ">=5.6"
},
"require-dev": {
"phpunit/phpunit": "~5.7",
"satooshi/php-coveralls": "~1.0"
},
"autoload": {
"psr-4": {
"Neutrino\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Test\\": "tests/Test"
}
},
"minimum-stability": "dev"
}
21 changes: 21 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./tests/bootstrap.php"
backupGlobals="false"
backupStaticAttributes="false"
verbose="true"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="true">
<testsuite name="Neutrino\Http - Testsuite">
<directory>./tests/</directory>
</testsuite>
<filter>
<whitelist processUncoveredFilesFromWhitelist="false">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
37 changes: 37 additions & 0 deletions src/Http/Auth/Basic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Neutrino\Http\Auth;

use Neutrino\Http\Contract\Request\Component;
use Neutrino\Http\Request;

/**
* Class Basic
*
* @package Neutrino\Http\Auth
*/
class Basic implements Component
{
/** @var string */
protected $user;

/** @var string */
protected $pass;

/**
* Basic constructor.
*
* @param string $user
* @param string $pass
*/
public function __construct($user, $pass)
{
$this->user = $user;
$this->pass = $pass;
}

public function build(Request $request)
{
$request->setHeader('Authorization', 'Basic ' . base64_encode($this->user . ':' . $this->pass));
}
}

0 comments on commit a6e9848

Please sign in to comment.