-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
652 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/.* export-ignore | ||
/README.md export-ignore |
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,27 @@ | ||
name: Attach ZIP to GitHub Release | ||
|
||
on: | ||
release: | ||
types: | ||
- published | ||
|
||
jobs: | ||
attach-zip: | ||
name: Attach ZIP to release | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Setup PHP | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: '5.5' | ||
tools: composer:v1 | ||
coverage: none | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
- name: Create and attach ZIP | ||
uses: concrete5-community/gh-package-release-attach@main | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
with: | ||
remove-files: | | ||
composer.json |
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,46 @@ | ||
<?php | ||
|
||
use Concrete\Core\Support\Facade\Application; | ||
use CHttpClient\Client; | ||
use CHttpClient\Response; | ||
|
||
echo 'Getting app... '; | ||
$app = Application::getFacadeApplication(); | ||
echo "done.\n"; | ||
|
||
echo 'Creating client... '; | ||
$client = $app->make(Client::class); | ||
if (!$client instanceof Client) { | ||
throw new RuntimeException('Wrong class: ' . get_class($client)); | ||
} | ||
echo "done.\n"; | ||
|
||
echo 'Performing request... '; | ||
$response = $client->get('https://www.google.com'); | ||
if (!$response instanceof Response) { | ||
throw new RuntimeException('Wrong class: ' . get_class($response)); | ||
} | ||
echo "done.\n"; | ||
|
||
echo 'Checking response code... '; | ||
$code = $response->getCode(); | ||
if (!is_int($code) || $code < 1 || $code > 999) { | ||
throw new RuntimeException('Wrong code: ' . json_encode($code)); | ||
} | ||
echo "done ({$code}).\n"; | ||
|
||
echo 'Checking reason phrase... '; | ||
$reasonPhrase = $response->getReasonPhrase(); | ||
if (!is_string($reasonPhrase) || $reasonPhrase === '') { | ||
throw new RuntimeException('Wrong reason phrase: ' . json_encode($reasonPhrase)); | ||
} | ||
echo "done ({$reasonPhrase}).\n"; | ||
|
||
echo 'Checking body... '; | ||
$body = $response->getBody(); | ||
if (!is_string($body) || $body === '') { | ||
throw new RuntimeException('Wrong body: ' . json_encode($body)); | ||
} | ||
echo 'done (' . strlen($body) ." bytes).\n"; | ||
|
||
return 0; |
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,46 @@ | ||
name: Test | ||
|
||
on: | ||
pull_request: | ||
push: | ||
branches: | ||
- main | ||
tags-ignore: | ||
- "**" | ||
repository_dispatch: | ||
types: | ||
- test | ||
workflow_dispatch: | ||
|
||
jobs: | ||
test: | ||
strategy: | ||
matrix: | ||
core-version: | ||
- 8.5.2 | ||
- 8.5.12 | ||
- 9.0.1 | ||
- 9.1.3 | ||
- 9.2.3 | ||
name: Test (${{ matrix.core-version }}) | ||
runs-on: ubuntu-latest | ||
container: | ||
image: ghcr.io/concrete5-community/docker5:${{ matrix.core-version }} | ||
env: | ||
GITHUB_WORKSPACE: /app/packages/http_client_compat | ||
steps: | ||
- | ||
name: Checkout | ||
uses: actions/checkout@v4 | ||
- | ||
name: Move package | ||
run: mv -- "$GITHUB_WORKSPACE" /app/packages/ && mkdir -p -- "$GITHUB_WORKSPACE" | ||
- | ||
name: Start DB | ||
run: ccm-service start db | ||
- | ||
name: Install package | ||
run: cd /app && c5 c5:package:install http_client_compat | ||
- | ||
name: Test package | ||
run: cd /app && c5 c5:exec /app/packages/http_client_compat/.github/workflows/test.php |
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,12 @@ | ||
[![Test](https://github.com/mlocati/http_client_compat/actions/workflows/test.yml/badge.svg)](https://github.com/mlocati/http_client_compat/actions/workflows/test.yml) | ||
|
||
## A Concrete package to help performing HTTP request | ||
|
||
This package provides an easy way to perform HTTP request in various ConcreteCMS/concrete5 version. | ||
|
||
Sample code: | ||
|
||
```php | ||
$client = app(CHttpClient\Client::class); | ||
$response = $client->get('https://www.example.org/'); | ||
``` |
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,30 @@ | ||
{ | ||
"name": "concrete5-community/http_client_compat", | ||
"description": "A ConcreteCMS/concrete5 package that makes it easy to perform HTTP requests both for concrete5 v8 and ConcreteCMS v9+", | ||
"type": "concrete5-package", | ||
"keywords": [ | ||
"concrete", | ||
"concretecms", | ||
"concrete5", | ||
"ccm", | ||
"http", | ||
"client" | ||
], | ||
"homepage": "https://github.com/concrete5-community/http-client-compat", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Michele Locati", | ||
"email": "michele@locati.it", | ||
"role": "maintainer", | ||
"homepage": "https://mlocati.github.io" | ||
} | ||
], | ||
"support": { | ||
"issues": "https://github.com/concrete5-community/http-client-compat/issues", | ||
"source": "https://github.com/concrete5-community/http-client-compat" | ||
}, | ||
"require": { | ||
"concrete5/core": "^8.5 | ^9.1" | ||
} | ||
} |
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,73 @@ | ||
<?php | ||
|
||
namespace Concrete\Package\HttpClientCompat; | ||
|
||
use Concrete\Core\Database\EntityManager\Provider\ProviderInterface; | ||
use Concrete\Core\Package\Package; | ||
use CHttpClient\ServiceProvider; | ||
|
||
defined('C5_EXECUTE') or die('Access denied.'); | ||
|
||
class Controller extends Package implements ProviderInterface | ||
{ | ||
/** | ||
* The package handle. | ||
* | ||
* @var string | ||
*/ | ||
protected $pkgHandle = 'http_client_compat'; | ||
|
||
/** | ||
* The package version. | ||
* | ||
* @var string | ||
*/ | ||
protected $pkgVersion = '1.0.0'; | ||
|
||
/** | ||
* The minimum concrete5 version. | ||
* | ||
* @var string | ||
*/ | ||
protected $appVersionRequired = '8.5.0'; | ||
|
||
/** | ||
* Map folders to PHP namespaces, for automatic class autoloading. | ||
* | ||
* @var array | ||
*/ | ||
protected $pkgAutoloaderRegistries = [ | ||
'src' => 'CHttpClient', | ||
]; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getPackageName() | ||
{ | ||
return t('HTTP Client Compat'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getPackageDescription() | ||
{ | ||
return t('A package that makes it easy to perform HTTP requests both for concrete5 v8 and ConcreteCMS v9+.'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @see \Concrete\Core\Database\EntityManager\Provider\ProviderInterface::getDrivers() | ||
*/ | ||
public function getDrivers() | ||
{ | ||
return []; | ||
} | ||
|
||
public function on_start() | ||
{ | ||
$this->app->make(ServiceProvider::class)->register(); | ||
} | ||
} |
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,36 @@ | ||
<?php | ||
|
||
namespace CHttpClient; | ||
|
||
interface Client | ||
{ | ||
/** | ||
* @param string $url | ||
* | ||
* @return \CHttpClient\Response | ||
*/ | ||
public function get($url, array $headers = []); | ||
|
||
/** | ||
* @param string $url | ||
* | ||
* @return \CHttpClient\Response | ||
*/ | ||
public function delete($url, array $headers = []); | ||
|
||
/** | ||
* @param string $url | ||
* @param string $body | ||
* | ||
* @return \CHttpClient\Response | ||
*/ | ||
public function post($url, $body, array $headers = []); | ||
|
||
/** | ||
* @param string $url | ||
* @param string $body | ||
* | ||
* @return \CHttpClient\Response | ||
*/ | ||
public function put($url, $body, array $headers = []); | ||
} |
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,87 @@ | ||
<?php | ||
|
||
namespace CHttpClient\Client; | ||
|
||
use CHttpClient\Client; | ||
use CHttpClient\Response; | ||
use Concrete\Core\Http\Client\Client as CoreClient; | ||
use RuntimeException; | ||
use Zend\Http\Request; | ||
|
||
final class V8 implements Client | ||
{ | ||
private $coreClient; | ||
|
||
public function __construct(CoreClient $coreClient) | ||
{ | ||
$this->coreClient = $coreClient; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @see \CHttpClient\Client::get() | ||
*/ | ||
public function get($url, array $headers = []) | ||
{ | ||
return $this->invokeMethod('get', $url, $headers); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @see \CHttpClient\Client::delete() | ||
*/ | ||
public function delete($url, array $headers = []) | ||
{ | ||
return $this->invokeMethod('delete', $url, $headers); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @see \CHttpClient\Client::post() | ||
*/ | ||
public function post($url, $body, array $headers = []) | ||
{ | ||
return $this->invokeMethod('post', $url, $headers, ['body' => $body]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @see \CHttpClient\Client::put() | ||
*/ | ||
public function put($url, $body, array $headers = []) | ||
{ | ||
return $this->invokeMethod('put', $url, $headers, ['body' => $body]); | ||
} | ||
|
||
/** | ||
* @throws \RuntimeException | ||
*/ | ||
private function invokeMethod($method, $url, array $headers = [], array $options = []) | ||
{ | ||
$zendRequest = new Request(); | ||
$zendRequest | ||
->setMethod($method) | ||
->setUri($url) | ||
; | ||
if (isset($options['body'])) { | ||
$zendRequest->setContent($options['body']); | ||
} | ||
$zendHeaders = $zendRequest->getHeaders(); | ||
foreach ($headers as $name => $value) { | ||
$zendHeaders->addHeaderLine($name, $value); | ||
} | ||
$zendResponse = $this->coreClient->send($zendRequest); | ||
$result = new Response( | ||
$zendResponse->getStatusCode(), | ||
$zendResponse->getReasonPhrase(), | ||
$zendResponse->getHeaders()->toArray(), | ||
$zendResponse->getBody() | ||
); | ||
|
||
return $result; | ||
} | ||
} |
Oops, something went wrong.