Skip to content

Commit

Permalink
Major refactor of entire API client (InfinityFreeHosting#61)
Browse files Browse the repository at this point in the history
* Big refactor of the entire API client

- Remove the separate Request classes, the Client just returns the Responses themselves.
- The Client functions now take their input parameters as function arguments, instead of using an array.
- Use custom exception classes for Guzzle errors and custom logic errors.
- Add isAvailable method to AvailabilityResponse, isSuccessful also returns true if a domain is not found but no error was returned.
- Add pre-commit with various linters.
- Completely rewrite the test suite to use Guzzle mocks.

* Remove Pint for PHP 7 support

* Downgrade php-cs-fixer for old PHP versions

* Enhance documentation

* Fix timeout settings
  • Loading branch information
Grendel7 authored Mar 2, 2023
1 parent 3904216 commit 4bd072e
Show file tree
Hide file tree
Showing 45 changed files with 1,214 additions and 2,000 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/phpunit.yaml → .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ jobs:
php_version: "${{ matrix.php_version }}"
- uses: php-actions/phpunit@v3
with:
configuration: "phpunit.xml.dist"
configuration: "phpunit.xml"
php_version: "${{ matrix.php_version }}"
version: "9"

pre-commit:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: php-actions/composer@v6
- uses: actions/setup-python@v3
- uses: pre-commit/[email protected]
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ composer.phar
/vendor/
/coverage/
/.phpunit.result.cache
/.phpunit.cache/
/.php-cs-fixer.cache

# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
composer.lock
8 changes: 8 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

$config = new \PhpCsFixer\Config();
$config->setRules([
'braces' => false,
]);

return $config;
31 changes: 31 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-added-large-files
- id: check-case-conflict
- id: check-executables-have-shebangs
- id: check-json
- id: check-merge-conflict
- id: check-shebang-scripts-are-executable
- id: check-symlinks
- id: check-toml
- id: check-xml
- id: check-yaml
- id: destroyed-symlinks
- id: detect-private-key
- id: end-of-file-fixer
- id: fix-byte-order-marker
- id: mixed-line-ending
- id: trailing-whitespace

- repo: https://github.com/InfinityFreeHosting/pre-commit-php
rev: 1.3.1
hooks:
- id: php-lint
# - id: php-cs
# - id: php-cbf
- id: php-cs-fixer
args:
- --config=.php-cs-fixer.php
# - id: pint
53 changes: 26 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# MyOwnFreeHost API Client

An API client to use the free hosting system from [MyOwnFreeHost](https://myownfreehost.net).

**IMPORTANT: THIS LIBRARY IS AIMED AT EXPERIENCED PHP DEVELOPERS. Experience with object-oriented PHP and Composer is required. If you can't use oo-PHP and Composer, don't bother with this library.**
Expand All @@ -12,29 +13,33 @@ composer require infinityfree/mofh-client
```

## Usage

Before you can get started, you need to get the API credentials from MyOwnFreeHost. Login to the [reseller panel](https://panel.myownfreehost.net), go to API -> Setup WHM API -> select the domain you want to configure. Copy the API Username and API password and set your own IP address as the Allowed IP Address (the IP address of your computer, server, or wherever you want to use this API client).

### Available Methods

The MyOwnFreeHost API exposes the following methods. The available parameters are listed below.
- createAccount

- createAccount: Create a new hosting account.
- username: A unique, 8 character identifier of the account.
- password: A password to login to the control panel, FTP and databases.
- domain: A domain name to create the account. Can be a subdomain or a custom domain.
- email: The email address of the user.
- plan: The name of the hosting plan to create the account on. Requires a hosting package to be configured through MyOwnFreeHost.
- suspend
- suspend: Suspend a hosting account.
- username: The unique, 8 character identifier of the account.
- reason: A string with information about why you are suspending the account.
- linked: If true, related accounts will be suspended as well.
- unsuspend
- unsuspend: Reactivate a hosting account.
- username: The unique, 8 character identifier of the account.
- password
- password: Change the password of a hosting account.
- username: The unique, 8 character identifier of the account.
- password: The new password to set for the account.
- availability
- availability: Check if a given domain name is available to be added to an account.
- domain: The domain name or subdomain to check.
- getUserDomains
- getUserDomains: Get the domain names linked to a given account.
- username: The vistaPanel login username (e.g. abcd_12345678).
- getDomainUser
- getDomainUser: Get the information of a particular hosting domain name, including the account it's hosted on and the document root.
- domain: The domain name to search for.

### Example
Expand All @@ -43,35 +48,29 @@ The MyOwnFreeHost API exposes the following methods. The available parameters ar
use \InfinityFree\MofhClient\Client;

// Create a new API client with your API credentials.
$client = Client::create([
'apiUsername' => 'your_api_username',
'apiPassword' => 'your_api_password',
'plan' => 'my_plan', // Optional, you can define it here or define it with the createAccount call.
]);

// Create a request object to create the request.
$request = $client->createAccount([
'username' => 'abcdefgh', // A unique, 8 character identifier of the account.
'password' => 'password123', // A password to login to the control panel, FTP and databases.
'domain' => 'userdomain.example.com', // Can be a subdomain or a custom domain.
'email' => '[email protected]', // The email address of the user.
'plan' => 'my_plan', // Optional, you can submit a hosting plan here or with the Client instantiation.
]);

// Send the API request and keep the response.
$response = $request->send();
$client = new Client("<MOFH API username>", "<MOFH API password>");

// Create a new hosting account.
$createResponse = $client->createAccount(
'abcd1234', // A unique, 8 character identifier of the account. Primarily used as internal identifier.
'password123', // A password to login to the control panel, FTP and databases.
'[email protected]', // The email address of the user.
'userdomain.example.com', // Initial domain of the account. Can be a subdomain or a custom domain.
'my_plan', // The hosting plan name at MyOwnFreeHost.
);

// Check whether the request was successful.
if ($response->isSuccessful()) {
echo 'You can login as: ' . $response->getVpUsername();
if ($createResponse->isSuccessful()) {
echo "Created account with username: ".$createResponse->getVpUsername();
} else {
echo 'Failed to create account: ' . $response->getMessage();
die();
}
```

## License

Copyright 2020 Hans Adema
Copyright 2023 InfinityFree

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
12 changes: 8 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@
"license": "Apache-2.0",
"authors": [
{
"name": "Hans Adema",
"email": "hans@infinityfree.net"
"name": "InfinityFree",
"email": "hello@infinityfree.net"
}
],
"require": {
"php": ">=7.1",
"ext-json": "*",
"ext-simplexml": "*",
"guzzlehttp/guzzle": "~6.0|~7.0"
},
"require-dev": {
"phpunit/phpunit": "^7.0|^8.0|^9.0|^10.0",
"fakerphp/faker": "^1.7",
"friendsofphp/php-cs-fixer": "^3.4",
"mockery/mockery": "^1.3",
"fakerphp/faker": "^1.7"
"phpunit/phpunit": "^7.0|^8.0|^9.0"
},
"autoload": {
"psr-4": {
Expand Down
16 changes: 16 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
colors="true"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd">
<coverage>
<include>
<directory>./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Tests">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
17 changes: 0 additions & 17 deletions phpunit.xml.dist

This file was deleted.

Loading

0 comments on commit 4bd072e

Please sign in to comment.