Skip to content

Commit

Permalink
Merge branch 'cloudflare:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Mello21century authored Dec 17, 2024
2 parents ccaece6 + 20da7aa commit 43e7200
Show file tree
Hide file tree
Showing 34 changed files with 1,293 additions and 205 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Lint

on:
push:
branches: [ '*' ]
pull_request:
branches: [ '*' ]

jobs:
build:

runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4']

steps:
- uses: actions/checkout@v2

- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}

- name: Cache Composer dependencies
uses: actions/cache@v2
with:
path: /tmp/composer-cache
key: ${{ runner.os }}-php${{ matrix.php-versions }}-${{ hashFiles('**/composer.lock') }}

- name: Install dependencies
uses: php-actions/composer@v5
with:
command: install
args: --prefer-dist --no-progress --no-suggest --verbose
php_version: ${{ matrix.php-versions }}
version: 1

- name: Run lint
run: make lint
25 changes: 25 additions & 0 deletions .github/workflows/semgrep.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

on:
pull_request: {}
workflow_dispatch: {}
push:
branches:
- main
- master
schedule:
- cron: '0 0 * * *'
name: Semgrep config
jobs:
semgrep:
name: semgrep/ci
runs-on: ubuntu-20.04
env:
SEMGREP_APP_TOKEN: ${{ secrets.SEMGREP_APP_TOKEN }}
SEMGREP_URL: https://cloudflare.semgrep.dev
SEMGREP_APP_URL: https://cloudflare.semgrep.dev
SEMGREP_VERSION_CHECK_URL: https://cloudflare.semgrep.dev/api/check-version
container:
image: returntocorp/semgrep
steps:
- uses: actions/checkout@v3
- run: semgrep ci
40 changes: 40 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Test

on:
push:
branches: [ '*' ]
pull_request:
branches: [ '*' ]

jobs:
build:

runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4']

steps:
- uses: actions/checkout@v2

- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}

- name: Cache Composer dependencies
uses: actions/cache@v2
with:
path: /tmp/composer-cache
key: ${{ runner.os }}-php${{ matrix.php-versions }}-${{ hashFiles('**/composer.lock') }}

- name: Install dependencies
uses: php-actions/composer@v5
with:
command: install
args: --prefer-dist --no-progress --no-suggest --verbose
php_version: ${{ matrix.php-versions }}
version: 1

- name: Run tests
run: make test
24 changes: 0 additions & 24 deletions .travis.yml

This file was deleted.

13 changes: 5 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
THIS := $(realpath $(lastword $(MAKEFILE_LIST)))
HERE := $(shell dirname $(THIS))

.PHONY: all fix lint test

all: lint test

fix:
php $(HERE)/vendor/bin/php-cs-fixer fix --config=$(HERE)/.php_cs
php vendor/bin/php-cs-fixer fix --config=.php_cs

lint:
php $(HERE)/vendor/bin/php-cs-fixer fix --config=$(HERE)/.php_cs --dry-run
php $(HERE)/vendor/bin/phpmd src/ text cleancode,codesize,controversial,design,naming,unusedcode
php $(HERE)/vendor/bin/phpmd tests/ text cleancode,codesize,controversial,design,naming,unusedcode
php vendor/bin/php-cs-fixer fix --config=.php_cs --dry-run
php vendor/bin/phpmd src/ text cleancode,codesize,controversial,design,naming,unusedcode
php vendor/bin/phpmd tests/ text cleancode,codesize,controversial,design,naming,unusedcode

test:
php $(HERE)/vendor/bin/phpunit --configuration $(HERE)/phpunit.xml
php vendor/bin/phpunit --configuration phpunit.xml
Binary file removed phpcbf.phar
Binary file not shown.
Binary file removed phpcs.phar
Binary file not shown.
40 changes: 12 additions & 28 deletions src/Adapter/Guzzle.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
<?php
/**
* User: junade
* Date: 13/01/2017
* Time: 18:26
*/

namespace Cloudflare\API\Adapter;

use Cloudflare\API\Auth\Auth;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\ResponseInterface;

class Guzzle implements Adapter
Expand Down Expand Up @@ -74,36 +70,24 @@ public function delete(string $uri, array $data = [], array $headers = []): Resp
return $this->request('delete', $uri, $data, $headers);
}

/**
* @SuppressWarnings(PHPMD.StaticAccess)
*/
public function request(string $method, string $uri, array $data = [], array $headers = [])
{
if (!in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) {
throw new \InvalidArgumentException('Request method must be get, post, put, patch, or delete');
}

$response = $this->client->$method($uri, [
'headers' => $headers,
($method === 'get' ? 'query' : 'json') => $data,
]);

$this->checkError($response);

return $response;
}

private function checkError(ResponseInterface $response)
{
$json = json_decode($response->getBody());

if (json_last_error() !== JSON_ERROR_NONE) {
throw new JSONException();
try {
$response = $this->client->$method($uri, [
'headers' => $headers,
($method === 'get' ? 'query' : 'json') => $data,
]);
} catch (RequestException $err) {
throw ResponseException::fromRequestException($err);
}

if (isset($json->errors) && count($json->errors) >= 1) {
throw new ResponseException($json->errors[0]->message, $json->errors[0]->code);
}

if (isset($json->success) && !$json->success) {
throw new ResponseException('Request was unsuccessful.');
}
return $response;
}
}
31 changes: 31 additions & 0 deletions src/Adapter/ResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,37 @@

namespace Cloudflare\API\Adapter;

use GuzzleHttp\Exception\RequestException;

class ResponseException extends \Exception
{
/**
* Generates a ResponseException from a Guzzle RequestException.
*
* @param RequestException $err The client request exception (typicall 4xx or 5xx response).
* @return ResponseException
*/
public static function fromRequestException(RequestException $err): self
{
if (!$err->hasResponse()) {
return new ResponseException($err->getMessage(), 0, $err);
}

$response = $err->getResponse();
$contentType = $response->getHeaderLine('Content-Type');

// Attempt to derive detailed error from standard JSON response.
if (strpos($contentType, 'application/json') !== false) {
$json = json_decode($response->getBody());
if (json_last_error() !== JSON_ERROR_NONE) {
return new ResponseException($err->getMessage(), 0, new JSONException(json_last_error_msg(), 0, $err));
}

if (isset($json->errors) && count($json->errors) >= 1) {
return new ResponseException($json->errors[0]->message, $json->errors[0]->code, $err);
}
}

return new ResponseException($err->getMessage(), 0, $err);
}
}
50 changes: 50 additions & 0 deletions src/Endpoints/AccountMembers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Cloudflare\API\Endpoints;

use Cloudflare\API\Adapter\Adapter;
use Cloudflare\API\Traits\BodyAccessorTrait;

class AccountMembers implements API
{
use BodyAccessorTrait;

/**
* @var Adapter
*/
private $adapter;

public function __construct(Adapter $adapter)
{
$this->adapter = $adapter;
}

public function addAccountMember(string $accountId, string $email, array $roles): \stdClass
{
$options = [
'email' => $email,
'roles' => $roles,
];

$account = $this->adapter->post('accounts/' . $accountId . '/members', $options);
$this->body = json_decode($account->getBody());

return $this->body->result;
}

public function listAccountMembers(string $accountId, int $page = 1, int $perPage = 20): \stdClass
{
$query = [
'page' => $page,
'per_page' => $perPage,
];

$zone = $this->adapter->get('accounts/' . $accountId . '/members', $query);
$this->body = json_decode($zone->getBody());

return (object)[
'result' => $this->body->result,
'result_info' => $this->body->result_info,
];
}
}
33 changes: 33 additions & 0 deletions src/Endpoints/AccountRoles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Cloudflare\API\Endpoints;

use Cloudflare\API\Adapter\Adapter;
use Cloudflare\API\Traits\BodyAccessorTrait;
use stdClass;

class AccountRoles implements API
{
use BodyAccessorTrait;

/**
* @var Adapter
*/
private $adapter;

public function __construct(Adapter $adapter)
{
$this->adapter = $adapter;
}

public function listAccountRoles(string $accountId): stdClass
{
$roles = $this->adapter->get('accounts/' . $accountId . '/roles');
$this->body = json_decode($roles->getBody());

return (object)[
'result' => $this->body->result,
'result_info' => $this->body->result_info,
];
}
}
13 changes: 13 additions & 0 deletions src/Endpoints/Accounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ public function __construct(Adapter $adapter)
$this->adapter = $adapter;
}

public function addAccount(string $name, string $type = 'standard'): \stdClass
{
$options = [
'name' => $name,
'type' => $type,
];

$account = $this->adapter->post('accounts', $options);
$this->body = json_decode($account->getBody());

return $this->body->result;
}

public function listAccounts(
int $page = 1,
int $perPage = 20,
Expand Down
Loading

0 comments on commit 43e7200

Please sign in to comment.