Skip to content

Commit

Permalink
Update document
Browse files Browse the repository at this point in the history
  • Loading branch information
parsilver committed Dec 24, 2023
1 parent 06fd92b commit f97a587
Show file tree
Hide file tree
Showing 11 changed files with 335 additions and 80 deletions.
318 changes: 258 additions & 60 deletions README.md

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions example/live-order-book.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
use Farzai\Bitkub\WebSocket\Message;
use Farzai\Bitkub\WebSocketClient;

$client = ClientBuilder::create()
->setCredentials('YOUR_API_KEY', 'YOUR_SECRET')
->build();
$websocket = new LiveOrderBookEndpoint(
new WebSocketClient(
ClientBuilder::create()
->setCredentials('YOUR_API_KEY', 'YOUR_SECRET')
->build()
)
);

$websocket = new LiveOrderBookEndpoint(new WebSocketClient($client));

$websocket->listen('thb_btc', function (Message $message) {
$websocket->listen('THB_ADA', function (Message $message) {
echo $message->json('event').PHP_EOL;
});

Expand Down
19 changes: 8 additions & 11 deletions example/stream-trade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@

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

use Farzai\Bitkub\ClientBuilder;
use Farzai\Bitkub\WebSocket\Endpoints\MarketEndpoint;
use Farzai\Bitkub\WebSocket\Message;
use Farzai\Bitkub\WebSocketClient;
$websocket = new \Farzai\Bitkub\WebSocket\Endpoints\MarketEndpoint(
new \Farzai\Bitkub\WebSocketClient(
\Farzai\Bitkub\ClientBuilder::create()
->setCredentials('YOUR_API_KEY', 'YOUR_SECRET')
->build()
)
);

$client = ClientBuilder::create()
->setCredentials('YOUR_API_KEY', 'YOUR_SECRET')
->build();

$websocket = new MarketEndpoint(new WebSocketClient($client));

$websocket->listen('trade.thb_ada', function (Message $message) {
$websocket->listen('trade.thb_ada', function (Farzai\Bitkub\WebSocket\Message $message) {
echo $message->json('sym').PHP_EOL;
});

Expand Down
2 changes: 1 addition & 1 deletion src/Endpoints/UserEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function tradingCredits(): ResponseInterface
* }
* }
*/
public function userLimits(): ResponseInterface
public function limits(): ResponseInterface
{
$config = $this->client->getConfig();

Expand Down
5 changes: 5 additions & 0 deletions src/WebSocket/Endpoints/AbstractEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ public function run()
{
$this->websocket->run();

Check warning on line 21 in src/WebSocket/Endpoints/AbstractEndpoint.php

View check run for this annotation

Codecov / codecov/patch

src/WebSocket/Endpoints/AbstractEndpoint.php#L21

Added line #L21 was not covered by tests
}

protected function getLogger()

Check warning on line 24 in src/WebSocket/Endpoints/AbstractEndpoint.php

View check run for this annotation

Codecov / codecov/patch

src/WebSocket/Endpoints/AbstractEndpoint.php#L24

Added line #L24 was not covered by tests
{
return $this->websocket->getLogger();

Check warning on line 26 in src/WebSocket/Endpoints/AbstractEndpoint.php

View check run for this annotation

Codecov / codecov/patch

src/WebSocket/Endpoints/AbstractEndpoint.php#L26

Added line #L26 was not covered by tests
}
}
29 changes: 28 additions & 1 deletion src/WebSocket/Endpoints/LiveOrderBookEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Farzai\Bitkub\WebSocket\Endpoints;

use Farzai\Bitkub\Endpoints as RestApiEndpoints;

class LiveOrderBookEndpoint extends AbstractEndpoint
{
/**
Expand All @@ -11,10 +13,35 @@ class LiveOrderBookEndpoint extends AbstractEndpoint
* echo $message->json('sym').PHP_EOL;
* });
*
* @param string|int $symbol Symbol name or id.
* @param callable|array<callable> $listeners
*/
public function listen(string $symbol, $listeners)
public function listen($symbol, $listeners)

Check warning on line 19 in src/WebSocket/Endpoints/LiveOrderBookEndpoint.php

View check run for this annotation

Codecov / codecov/patch

src/WebSocket/Endpoints/LiveOrderBookEndpoint.php#L19

Added line #L19 was not covered by tests
{
// Check if symbol is numeric.
if (! is_numeric($symbol)) {

Check warning on line 22 in src/WebSocket/Endpoints/LiveOrderBookEndpoint.php

View check run for this annotation

Codecov / codecov/patch

src/WebSocket/Endpoints/LiveOrderBookEndpoint.php#L22

Added line #L22 was not covered by tests

$this->getLogger()->debug('Find symbol id by name: '.$symbol);

Check warning on line 24 in src/WebSocket/Endpoints/LiveOrderBookEndpoint.php

View check run for this annotation

Codecov / codecov/patch

src/WebSocket/Endpoints/LiveOrderBookEndpoint.php#L24

Added line #L24 was not covered by tests

// Find symbol id by name.
$market = new RestApiEndpoints\MarketEndpoint($this->websocket->getClient());

Check warning on line 27 in src/WebSocket/Endpoints/LiveOrderBookEndpoint.php

View check run for this annotation

Codecov / codecov/patch

src/WebSocket/Endpoints/LiveOrderBookEndpoint.php#L27

Added line #L27 was not covered by tests

foreach ($market->symbols()->throw()->json('result') as $item) {
if ($item['symbol'] === strtoupper(trim($symbol))) {
$symbol = $item['id'];

Check warning on line 31 in src/WebSocket/Endpoints/LiveOrderBookEndpoint.php

View check run for this annotation

Codecov / codecov/patch

src/WebSocket/Endpoints/LiveOrderBookEndpoint.php#L29-L31

Added lines #L29 - L31 were not covered by tests

$this->getLogger()->debug('Found symbol id: '.$symbol);
break;

Check warning on line 34 in src/WebSocket/Endpoints/LiveOrderBookEndpoint.php

View check run for this annotation

Codecov / codecov/patch

src/WebSocket/Endpoints/LiveOrderBookEndpoint.php#L33-L34

Added lines #L33 - L34 were not covered by tests
}
}

if (! is_numeric($symbol)) {
$this->getLogger()->debug('Invalid symbol name. Given: '.$symbol);

Check warning on line 39 in src/WebSocket/Endpoints/LiveOrderBookEndpoint.php

View check run for this annotation

Codecov / codecov/patch

src/WebSocket/Endpoints/LiveOrderBookEndpoint.php#L38-L39

Added lines #L38 - L39 were not covered by tests

throw new \InvalidArgumentException('Invalid symbol name. Given: '.$symbol);

Check warning on line 41 in src/WebSocket/Endpoints/LiveOrderBookEndpoint.php

View check run for this annotation

Codecov / codecov/patch

src/WebSocket/Endpoints/LiveOrderBookEndpoint.php#L41

Added line #L41 was not covered by tests
}
}

$this->websocket->addListener('orderbook/'.$symbol, $listeners);

Check warning on line 45 in src/WebSocket/Endpoints/LiveOrderBookEndpoint.php

View check run for this annotation

Codecov / codecov/patch

src/WebSocket/Endpoints/LiveOrderBookEndpoint.php#L45

Added line #L45 was not covered by tests

return $this;

Check warning on line 47 in src/WebSocket/Endpoints/LiveOrderBookEndpoint.php

View check run for this annotation

Codecov / codecov/patch

src/WebSocket/Endpoints/LiveOrderBookEndpoint.php#L47

Added line #L47 was not covered by tests
Expand Down
2 changes: 2 additions & 0 deletions src/WebSocket/Endpoints/MarketEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public function listen($streamName, $listeners)
$streamNames = $streamName;

Check warning on line 22 in src/WebSocket/Endpoints/MarketEndpoint.php

View check run for this annotation

Codecov / codecov/patch

src/WebSocket/Endpoints/MarketEndpoint.php#L22

Added line #L22 was not covered by tests
}

$this->getLogger()->debug('Add event listener for stream: '.implode(', ', $streamNames));

Check warning on line 25 in src/WebSocket/Endpoints/MarketEndpoint.php

View check run for this annotation

Codecov / codecov/patch

src/WebSocket/Endpoints/MarketEndpoint.php#L25

Added line #L25 was not covered by tests

foreach ($streamNames as $name) {
$this->websocket->addListener($this->getStreamName($name), $listeners);

Check warning on line 28 in src/WebSocket/Endpoints/MarketEndpoint.php

View check run for this annotation

Codecov / codecov/patch

src/WebSocket/Endpoints/MarketEndpoint.php#L27-L28

Added lines #L27 - L28 were not covered by tests
}
Expand Down
8 changes: 8 additions & 0 deletions src/WebSocket/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public function handle(array $listeners): void
{
$events = array_unique(array_keys($listeners));

Check warning on line 22 in src/WebSocket/Engine.php

View check run for this annotation

Codecov / codecov/patch

src/WebSocket/Engine.php#L22

Added line #L22 was not covered by tests

$this->logger->info('[WebSocket] - '.Carbon::now()->format('Y-m-d H:i:s').' - Connecting to WebSocket server...');

Check warning on line 24 in src/WebSocket/Engine.php

View check run for this annotation

Codecov / codecov/patch

src/WebSocket/Engine.php#L24

Added line #L24 was not covered by tests

$this->logger->debug('[WebSocket] - '.Carbon::now()->format('Y-m-d H:i:s').' - Events: '.implode(', ', $events));

Check warning on line 26 in src/WebSocket/Engine.php

View check run for this annotation

Codecov / codecov/patch

src/WebSocket/Engine.php#L26

Added line #L26 was not covered by tests

$client = new WebSocketClient('wss://api.bitkub.com/websocket-api/'.implode(',', $events));

Check warning on line 28 in src/WebSocket/Engine.php

View check run for this annotation

Codecov / codecov/patch

src/WebSocket/Engine.php#L28

Added line #L28 was not covered by tests

$client
Expand Down Expand Up @@ -56,6 +60,10 @@ public function handle(array $listeners): void
}
});

Check warning on line 61 in src/WebSocket/Engine.php

View check run for this annotation

Codecov / codecov/patch

src/WebSocket/Engine.php#L61

Added line #L61 was not covered by tests

$client->onClose(function () {
$this->logger->info('[WebSocket] - '.Carbon::now()->format('Y-m-d H:i:s').' - Connection closed.');
});

Check warning on line 65 in src/WebSocket/Engine.php

View check run for this annotation

Codecov / codecov/patch

src/WebSocket/Engine.php#L63-L65

Added lines #L63 - L65 were not covered by tests

$client->start();

Check warning on line 67 in src/WebSocket/Engine.php

View check run for this annotation

Codecov / codecov/patch

src/WebSocket/Engine.php#L67

Added line #L67 was not covered by tests
}
}
5 changes: 5 additions & 0 deletions src/WebSocketClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public function getLogger(): LoggerInterface
return $this->client->getLogger();
}

public function getClient(): ClientInterface

Check warning on line 35 in src/WebSocketClient.php

View check run for this annotation

Codecov / codecov/patch

src/WebSocketClient.php#L35

Added line #L35 was not covered by tests
{
return $this->client;

Check warning on line 37 in src/WebSocketClient.php

View check run for this annotation

Codecov / codecov/patch

src/WebSocketClient.php#L37

Added line #L37 was not covered by tests
}

/**
* Add event listener.
*
Expand Down
11 changes: 11 additions & 0 deletions tests/UriFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Farzai\Bitkub\UriFactory;

it('can create uri', function () {
$uri = UriFactory::createFromUri('https://api.bitkub.com');

expect($uri->getScheme())->toBe('https');
expect($uri->getHost())->toBe('api.bitkub.com');
expect($uri->getPath())->toBe('');
});
2 changes: 1 addition & 1 deletion tests/UserEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
->setHttpClient($psrClient)
->build();

$response = $client->user()->userLimits()->throw();
$response = $client->user()->limits()->throw();

expect($response->json('result'))->toBe([
'limits' => [
Expand Down

0 comments on commit f97a587

Please sign in to comment.