Skip to content

Commit

Permalink
Merge pull request #6 from blocktrail/webhook-batch-events
Browse files Browse the repository at this point in the history
webhooks api - batch create 'address-transaction' events
  • Loading branch information
rubensayshi committed Jan 20, 2015
2 parents 0b12e98 + bd243f3 commit e08be97
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ install:
- composer update
script:
- "./vendor/bin/phpunit"
- "./vendor/bin/phpcs --standard=./phpcs.xml -n -a ./src/"
- "./vendor/bin/phpcs --standard=./phpcs.xml -n ./src/"
- "php coverage-checker.php clover.xml 70"
notifications:
slack:
Expand Down
21 changes: 21 additions & 0 deletions src/BlocktrailSDK.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,27 @@ public function subscribeAddressTransactions($identifier, $address, $confirmatio
return self::jsonDecode($response->body(), true);
}

/**
* batch subscribes a webhook to multiple transaction events
* @param string $identifier the unique identifier of the webhook
* @param array $batchData A 2D array of event data:
* [address => $address, confirmations => $confirmations]
* where $address is the address to subscibe to and $confirmations (optional) is the amount of confirmations
* @return boolean true on success
*/
public function batchSubscribeAddressTransactions($identifier, $batchData) {
$postData = array();
foreach ($batchData as $record) {
$postData[] = array(
'event_type' => 'address-transactions',
'address' => $record['address'],
'confirmations' => isset($record['confirmations']) ? $record['confirmations'] : 6,
);
}
$response = $this->client->post("webhook/{$identifier}/events/batch", null, $postData, 'http-signatures');
return self::jsonDecode($response->body(), true);
}

/**
* subscribes a webhook to a new block event
* @param string $identifier the unique identifier of the webhook to be triggered
Expand Down
25 changes: 25 additions & 0 deletions tests/BlocktrailSDKTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,5 +365,30 @@ public function testWebhooks() {
//unsubscribe webhook event (block)
$response = $client->unsubscribeNewBlocks($webhookID2);
$this->assertTrue($response === true, "response does not match expected value");

//batch create webhook events
$batchData = array(
array(
'event_type' => 'address-transactions',
'address' => '18FA8Tn54Hu8fjn7kkfAygPoGEJLHMbHzo',
'confirmations' => 1
),
array(
'event_type' => 'address-transactions',
'address' => '1LUCKYwD6V9JHVXAFEEjyQSD4Dj5GLXmte',
'confirmations' => 1
),
array(
'event_type' => 'address-transactions',
'address' => '1qMBuZnrmGoAc2MWyTnSgoLuWReDHNYyF',
'confirmations' => 1
)
);
$response = $client->batchSubscribeAddressTransactions($webhookID2, $batchData);
$this->assertTrue($response);
$response = $client->getWebhookEvents($webhookID2);
$this->assertEquals(3, $response['total'], "'total' does not match expected value");
$this->assertEquals(3, count($response['data']), "Count of events returned is not equal to 3");
$this->assertEquals($batchData[2]['address'], $response['data'][2]['address'], "Batch created even not as expected");
}
}

0 comments on commit e08be97

Please sign in to comment.