Skip to content

Commit

Permalink
test: add bad access toekn
Browse files Browse the repository at this point in the history
  • Loading branch information
KEINOS committed Jul 22, 2020
1 parent 2855df3 commit 4046647
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 9 deletions.
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,66 @@
[![](https://img.shields.io/packagist/php-v/keinos/mastodon-streaming-api-config)](https://github.com/KEINOS/Mastodon_Streaming_API_Config/blob/master/.travis.yml "View version support in Packagist")

# Configuration Settings Class

This PHP class simply holds Mastodon server information from ["instance" API method](https://docs.joinmastodon.org/methods/instance/).

Use this class to get the URI of [Mastodon Streaming API](https://docs.joinmastodon.org/methods/timelines/streaming/) for receiving messages from `server-sent events` or `WebSocket`.

- Note: This class is aimed to be used in other [Mastodon StreamingAPI tools](https://github.com/search?q=user%3AKEINOS+Mastodon_StreamingAPI_).

## Install

```bash
composer require keinos/mastodon-streaming-api-config
```

## Usage

```php
<?php

namespace KEINOS\Sample;

require_once __DIR__ . '/../vendor/autoload.php';

$conf = new \KEINOS\MSTDN_TOOLS\Config\Config([
'url_host' => 'https://qiitadon.com/', // Your server/instance URL
]);

$info_instance = $conf->getInfoInstance();
$uri_websocket = $conf->getUriStreamingApi();

```

If the server/instance is in [`WHITELIST_MODE`](https://docs.joinmastodon.org/admin/config/#basic) then you will need an access token to get the server information.

```php
<?php

namespace KEINOS\Sample;

require_once __DIR__ . '/../vendor/autoload.php';

use KEINOS\MSTDN_TOOLS\Config\Config;

$conf = new Config([
'url_host' => 'https://qiitadon.com/',
'access_token' => 'YOUR ACCESS TOKEN HERE',
]);

$info_instance = $conf->getInfoInstance();
$uri_websocket = $conf->getUriStreamingApi();
$access_token = $cong->getAccessToken();

```

- For other methods see the [interface of the Config class](https://github.com/KEINOS/Mastodon_StreamingAPI_Config/blob/master/src/ConfigInterface.php).
- [./src/ConfigInterface.php](./src/ConfigInterface.php)

## Package Information

- Packagist: https://packagist.org/packages/keinos/mastodon-streaming-api-config
- Source: https://github.com/KEINOS/Mastodon_StreamingAPI_Config
- Issues: https://github.com/KEINOS/Mastodon_StreamingAPI_Config/issues
- License: [MIT](https://github.com/KEINOS/Mastodon_StreamingAPI_Config/blob/master/LICENSE)
- Authors: [KEINOS and the contributors](https://github.com/KEINOS/Mastodon_StreamingAPI_Config/graphs/contributors)
6 changes: 5 additions & 1 deletion samples/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

use KEINOS\MSTDN_TOOLS\Config\Config;

$obj = new Config([
$config = new Config([
'url_host' => 'https://qiitadon.com/',
]);

$info_instance = $config->getInfoInstance();

print_r($info_instance);
11 changes: 11 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ protected function generateUrlApiInstance(): string
return $url_api_instance;
}

/**
* Get the WebSocket URI address.
* @return string
*/
public function getUriStreamingApi(): string
{
$info_instance = $this->getInfoInstance();

return $info_instance['urls']['streaming_api'];
}

/**
* @return null|mixed
*/
Expand Down
59 changes: 51 additions & 8 deletions src/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,59 @@ interface ConfigInterface
* Checks if the URL of the host given is valid/alive, then fetches the
* server information from the Mastodon API "instance" method.
*
* This "instance" information will be cached for an hour by default to
* lower the quota usage of the API.
*
* To change the life time, set the "ttl_cache" key in the settings arg.
* To disable caching set the "use_cache"
*
*
* @param array<string,string> $settings
* Set information of the target server/instance.
* Must keys
* - "url_host":
* Optional keys
* - "access_token": Access token of the target server. This is required
* if the server is in "whitelist" mode.
* - "endpoint_api_instance": This will be auto-set.
* - "ttl_cache": Life time of the cache in seconds. Default is 1 hour.
* - "use_cache": "true" or "false" string. Default is "true".
* Must "keys"
* - "url_host" (string)
*
* Optional "key name" => (value type)
* [
* "access_token" => (string),
* "endpoint_api_instance" => (string),
* "flag_use_cache" => (bool),
* "prefix_cache" => (string),
* "id_hash_self" => (string),
* "ttl_cache" => (int),
* ];
*/
public function __construct(array $settings);

/**
* Property setters。
*
* Setting properties won't take effect the fetched information from the
* "instance" API method. Though you can change them after the instantiation.
*/
public function setAccessToken(string $access_token): void;
public function setEndpointApiInstance(string $path): void;
public function setFlagUseCache(bool $flag): void;
public function setIdHashSelf(string $id_hash_self): void;
/** @param array<mixed,mixed> $info_instance */
public function setInfoInstance(array $info_instance): void;
public function setPrefixCache(string $prefix_cache): void;
public function setTtlCache(int $ttl): void;
public function setUrlHost(string $url_host): void;
public function setUrlApiInstance(string $url_api_instance): void;

/**
* Property getters.
*/
public function getAccessToken(): string;
public function getEndpointApiInstance(): string;
public function getFlagUseCache(): bool;
public function getIdHashSelf(): string;
/** @return array<mixed,mixed> */
public function getInfoInstance(): array;
public function getPrefixCache(): string;
public function getTtlCache(): int;
public function getUrlHost(): string;
public function getUrlApiInstance(): string;
public function getUriStreamingApi(): string;
}
13 changes: 13 additions & 0 deletions tests/ConstructTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ public function testEmptyInstanceInfoWhenCache()
$method->invokeArgs($object, [ null ]);
}

public function testGetUriStreamingApi()
{
$settings = [
'url_host' => 'https://qiitadon.com/',
];
$conf = new \KEINOS\MSTDN_TOOLS\Config\Config($settings);

$expect = 'wss://streaming.qiitadon.com:4000';
$actual = $conf->getUriStreamingApi();

$this->assertSame($expect, $actual);
}

public function testNonCaching()
{
$settings = [
Expand Down

0 comments on commit 4046647

Please sign in to comment.