Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mlocati committed Jan 15, 2024
1 parent 359810f commit cccfb94
Show file tree
Hide file tree
Showing 12 changed files with 652 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.* export-ignore
/README.md export-ignore
27 changes: 27 additions & 0 deletions .github/workflows/create-release-attachment.yml
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
46 changes: 46 additions & 0 deletions .github/workflows/test.php
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;
46 changes: 46 additions & 0 deletions .github/workflows/test.yml
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
12 changes: 12 additions & 0 deletions README.md
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/');
```
30 changes: 30 additions & 0 deletions composer.json
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"
}
}
73 changes: 73 additions & 0 deletions controller.php
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();
}
}
36 changes: 36 additions & 0 deletions src/Client.php
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 = []);
}
87 changes: 87 additions & 0 deletions src/Client/V8.php
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;
}
}
Loading

0 comments on commit cccfb94

Please sign in to comment.