Skip to content

Commit

Permalink
pagination for transaction lists
Browse files Browse the repository at this point in the history
  • Loading branch information
darthmaim committed May 17, 2015
1 parent 3f11aac commit 3240520
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 32 deletions.
46 changes: 18 additions & 28 deletions src/V2/Endpoint/Commerce/Transaction/ListEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,45 @@

use GuzzleHttp\Client;
use GW2Treasures\GW2Api\V2\AuthenticatedEndpoint;
use GW2Treasures\GW2Api\V2\Interfaces\IPaginatedEndpoint;
use GW2Treasures\GW2Api\V2\PaginatedEndpoint;
use InvalidArgumentException;

// TODO: should support pagination
class ListEndpoint extends AuthenticatedEndpoint {
class ListEndpoint extends AuthenticatedEndpoint implements IPaginatedEndpoint {
use PaginatedEndpoint;

protected static $types = [ 'current', 'history' ];
protected static $lists = [ 'buys', 'sells' ];

/** @var string $type The type of the list (one of self::$types) */
/** @var string $type */
protected $type;

public function __construct( Client $client, $apiKey, $type, array $options = [ ] ) {
/** @var string $list */
protected $list;

public function __construct( Client $client, $apiKey, $type, $list, array $options = [ ] ) {
if( !in_array( $type, self::$types )) {
throw new InvalidArgumentException(
'Invalid $type ("' . $type . '""), has to be one of: ' . implode(', ', self::$types)
);
}

if( !in_array( $list, self::$lists )) {
throw new InvalidArgumentException(
'Invalid $list ("' . $list . '""), has to be one of: ' . implode(', ', self::$lists)
);
}

$this->type = $type;
$this->list = $list;

parent::__construct( $client, $apiKey, $options );
}


/**
* {@inheritdoc}
*/
protected function url() {
return 'v2/commerce/transactions/' . $this->type;
}

/**
* Get pending/completed buy transactions.
*
* @return array
*/
public function buys() {
$request = $this->createRequest( [], $this->url() . '/buys' );
$response = $this->request( $request );
return $this->getResponseAsJson( $response );
}

/**
* Get pending/completed sell transactions.
*
* @return mixed
*/
public function sells() {
$request = $this->createRequest( [], $this->url() . '/sells' );
$response = $this->request( $request );
return $this->getResponseAsJson( $response );
return 'v2/commerce/transactions/' . $this->type . '/' . $this->list;
}
}
8 changes: 4 additions & 4 deletions src/V2/Endpoint/Commerce/Transaction/TransactionEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ protected function url() {
/**
* Get current transactions.
*
* @return ListEndpoint
* @return TypeEndpoint
*/
public function current() {
return new ListEndpoint( $this->client, $this->apiKey, 'current' );
return new TypeEndpoint( $this->client, $this->apiKey, 'current' );
}

/**
* Get transaction history.
*
* @return ListEndpoint
* @return TypeEndpoint
*/
public function history() {
return new ListEndpoint( $this->client, $this->apiKey, 'history' );
return new TypeEndpoint( $this->client, $this->apiKey, 'history' );
}
}
53 changes: 53 additions & 0 deletions src/V2/Endpoint/Commerce/Transaction/TypeEndpoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace GW2Treasures\GW2Api\V2\Endpoint\Commerce\Transaction;

use GuzzleHttp\Client;
use GW2Treasures\GW2Api\V2\AuthenticatedEndpoint;
use InvalidArgumentException;

class TypeEndpoint extends AuthenticatedEndpoint {

protected static $types = [ 'current', 'history' ];

/** @var string $type */
protected $type;

public function __construct( Client $client, $apiKey, $type, array $options = [ ] ) {
if( !in_array( $type, self::$types )) {
throw new InvalidArgumentException(
'Invalid $type ("' . $type . '""), has to be one of: ' . implode(', ', self::$types)
);
}

$this->type = $type;

parent::__construct( $client, $apiKey, $options );
}


/**
* {@inheritdoc}
*/
protected function url() {
return 'v2/commerce/transactions/' . $this->type;
}

/**
* Get pending/completed buy transactions.
*
* @return ListEndpoint
*/
public function buys() {
return new ListEndpoint( $this->getClient(), $this->apiKey, $this->type, 'buys' );
}

/**
* Get pending/completed sell transactions.
*
* @return ListEndpoint
*/
public function sells() {
return new ListEndpoint( $this->getClient(), $this->apiKey, $this->type, 'sells' );
}
}

0 comments on commit 3240520

Please sign in to comment.