Skip to content
This repository has been archived by the owner on Dec 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #31 from giansalex/feature/migrate-react-http
Browse files Browse the repository at this point in the history
Migrate React/http
  • Loading branch information
giansalex authored Oct 8, 2020
2 parents 36ac813 + ef8f8c5 commit 0115fe2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
custom: "https://yape.giansalex.dev/"
custom: ["https://yape.giansalex.dev/", "https://paypal.me/giansalex"]
18 changes: 8 additions & 10 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ jobs:
extensions: mbstring
- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest
- name: Install Coveralls
run: composer require php-coveralls/php-coveralls
- name: Install React HTTP
run: composer require react/http
- name: Create test directory
run: mkdir -p test-results/behat
shell: bash
Expand All @@ -35,11 +35,9 @@ jobs:
run: vendor/bin/behat --config behat.yml -f pretty -o std -f junit -o test-results/behat/
shell: bash
- name: Report code coverage
run: vendor/bin/php-coveralls
shell: bash
env:
CI_NAME: Github Actions
CI_BRANCH: ${GITHUB_REF}
CI_PULL_REQUEST: $([ "${GITHUB_EVENT_NAME}" == "pull_request" ] && echo "true" || echo "false")
COVERALLS_REPO_TOKEN: ${{ secrets.coverallsToken }}
if: matrix.operating-system == 'ubuntu-latest'
uses: codecov/codecov-action@v1
with:
file: ./test-results/phpunit/clover.xml
flags: unittests
name: codecov-peruconsult
if: matrix.operating-system == 'ubuntu-latest'
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Peru Consultas
[![Github Actions](https://github.com/giansalex/peru-consult/workflows/CI/badge.svg)](https://github.com/giansalex/peru-consult/actions)
[![Coverage Status](https://coveralls.io/repos/github/giansalex/peru-consult/badge.svg?branch=master)](https://coveralls.io/github/giansalex/peru-consult?branch=master)
[![codecov](https://codecov.io/gh/giansalex/peru-consult/branch/master/graph/badge.svg)](https://codecov.io/gh/giansalex/peru-consult)
[![Maintainability](https://api.codeclimate.com/v1/badges/c307caea39f1101cbc5d/maintainability)](https://codeclimate.com/github/giansalex/peru-consult/maintainability)
[![Project Stats](https://www.openhub.net/p/peru-consult/widgets/project_thin_badge.gif)](https://www.openhub.net/p/peru-consult)
[![Packagist](https://img.shields.io/packagist/v/giansalex/peru-consult.svg?style=flat-square)](https://packagist.org/packages/giansalex/peru-consult)
Expand All @@ -23,7 +23,7 @@ composer require giansalex/peru-consult
### Servicios Disponibles
- Consulta de DNI
- Consulta de RUC
- Validez de Usuario SOL
- Validez de Usuario SOL (extras)

### API REST :zap:
### API :cloud:
Visitar [peru-consult-api](https://github.com/giansalex/peru-consult-api).
18 changes: 14 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
{
"name": "giansalex/peru-consult",
"description": "Consultas gratuita de RUC y DNI en Perú",
"keywords": ["sunat", "reniec", "dni", "ruc", "clave-sol", "peru"],
"description": "Consulta gratuita de DNI, RUC en Perú",
"keywords": [
"sunat",
"reniec",
"dni",
"ruc",
"consulta-dni",
"consulta-ruc",
"peru"
],
"license": "MIT",
"authors": [
{
Expand All @@ -15,14 +23,16 @@
"php": "^7.1",
"ext-json": "*",
"ext-dom": "*",
"ext-libxml": "*",
"react/http-client": "^0.5.9"
"ext-libxml": "*"
},
"require-dev": {
"phpunit/phpunit": "^7",
"behat/behat": "^3.5",
"clue/block-react": "^1.3",
"phpstan/phpstan": "^0.11.15"
},
"suggest": {
"react/http": "Allow Dni, Ruc async requests"
},
"autoload": {
"psr-4": {
Expand Down
43 changes: 14 additions & 29 deletions src/Peru/Http/Async/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@

namespace Peru\Http\Async;

use React\HttpClient\Client;
use React\HttpClient\Response;
use React\Promise\Deferred;
use Psr\Http\Message\ResponseInterface;
use React\Http\Browser;
use React\Promise\PromiseInterface;

/**
* Class HttpClient.
*
* HttpClient based on ReactPHP
*/
class HttpClient extends Client implements ClientInterface
class HttpClient extends Browser implements ClientInterface
{
/**
* @var array
Expand All @@ -29,7 +28,13 @@ class HttpClient extends Client implements ClientInterface
*/
public function getAsync(string $url, array $headers = []): PromiseInterface
{
return $this->requestAsync('GET', $url, null, $headers);
$headers = $this->buildHeaders($headers);
return $this->get($url, $headers)
->then(function (ResponseInterface $response) {
$this->saveCookies($response->getHeaders());

return (string)$response->getBody();
});
}

/**
Expand All @@ -43,33 +48,13 @@ public function getAsync(string $url, array $headers = []): PromiseInterface
*/
public function postAsync(string $url, $data, array $headers = []): PromiseInterface
{
return $this->requestAsync('POST', $url, $data, $headers);
}

private function requestAsync($method, $url, $data, $headers)
{
$deferred = new Deferred();
$headers = $this->buildHeaders($headers);
return $this->post($url, $headers, $data)
->then(function (ResponseInterface $response) {
$this->saveCookies($response->getHeaders());

$request = $this->request($method, $url, $headers);
$request->on('response', function (Response $response) use ($deferred) {
$this->saveCookies($response->getHeaders());

$result = '';
$response->on('data', function ($data) use (&$result) {
$result .= $data;
});

$response->on('end', function () use (&$result, $deferred) {
$deferred->resolve($result);
return (string)$response->getBody();
});
});
$request->on('error', function ($e) use ($deferred) {
$deferred->reject($e);
});
$request->end($data);

return $deferred->promise();
}

private function saveCookies(array $headers)
Expand Down

0 comments on commit 0115fe2

Please sign in to comment.