Skip to content
This repository has been archived by the owner on May 10, 2020. It is now read-only.

Commit

Permalink
Give the ability to authenticate
Browse files Browse the repository at this point in the history
  • Loading branch information
jacklul committed May 20, 2018
1 parent 68258e0 commit dc53d4c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Install this package through [Composer](https://github.com/composer/composer) -

### Usage

- Initialize the object with User-Agent "My project":
- Initialize the object with user-agent:

```php
require 'vendor/autoload.php';
Expand All @@ -28,7 +28,7 @@ Install this package through [Composer](https://github.com/composer/composer) -
$api = new E621('My project');
```

- Perform request with own parameters:
- Perform request with parameters:

```php
$request = $api->postIndex(['tags' => 'falvie cat order:>score', 'limit' => 25]);
Expand All @@ -38,9 +38,9 @@ Install this package through [Composer](https://github.com/composer/composer) -

```php
if ($request->isSuccessful()) {
$results = $request->getResult(); // get the result data
$results = $request->getResult(); // Tet the result data
} else {
echo $request->getReason(); // print request failure reason
echo $request->getReason(); // Print request failure reason
}
```

Expand Down Expand Up @@ -112,12 +112,19 @@ Install this package through [Composer](https://github.com/composer/composer) -
}
```

- To authenticate using API key you can either pass `login` and `password_hash` (API key) parameters with each request or set it globally:

#### Logging in

Some actions require logging in to execute, to authenticate you can either pass `login` and `password_hash` (API key) parameters with each request or set it globally:

```php
$api->setAuth('login', 'api_key');
$api = new E621('My project');
$api->setAuth('login', 'api_key'); // Set login data
$request = $api->dmailInbox();
$api->unsetAuth(); // Remove login data
```


## License

See [LICENSE](LICENSE).
21 changes: 20 additions & 1 deletion src/E621.php
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,6 @@ public function __call($action, array $data = [])
}

if (isset($this->actions[$action]['need_login']) && $this->actions[$action]['need_login'] === true) {

if (isset($data[0]) && !isset($data[0]['login']) || !isset($data[0]['password_hash'])) {
if (isset($this->auth['login']) && isset($this->auth['password_hash'])) {
$data[0]['login'] = $this->auth['login'];
Expand Down Expand Up @@ -993,12 +992,32 @@ public function setDebugLogHandler($debug_log_handler)
}

/**
* Set auth data globally
*
* @param $login
* @param $api_key
*
* @throws \InvalidArgumentException
*/
public function setAuth($login, $api_key)
{
if (empty($login) || !is_string($login)) {
throw new \InvalidArgumentException('Argument "login" cannot be empty and must be a string!');
}

if (empty($login) || !is_string($api_key)) {
throw new \InvalidArgumentException('Argument "api_key" cannot be empty and must be a string!');
}

$this->auth['login'] = $login;
$this->auth['password_hash'] = $api_key;
}

/**
* Deletes auth data
*/
public function unsetAuth()
{
$this->auth = null;
}
}

0 comments on commit dc53d4c

Please sign in to comment.