Skip to content

Commit 7295a19

Browse files
authored
Merge pull request #42 from chadicus/master
Version 3
2 parents d75a474 + 47bbd5f commit 7295a19

38 files changed

+1506
-4848
lines changed

.gitattributes

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Enforce Unix newlines
2+
* text=lf
3+
4+
# Exclude unused files
5+
# see: https://redd.it/2jzp6k
6+
/tests export-ignore
7+
/.github export-ignore
8+
/.gitattributes export-ignore
9+
/.gitignore export-ignore
10+
/.*.yml export-ignore
11+
/phpcs.xml export-ignore
12+
/phpunit.xml.dist export-ignore
13+
/README.md export-ignore

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @traderinteractive/opensource

.github/CONTRIBUTING.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Contribution Guidelines
2+
We welcome you to report [issues](/../../issues) or submit [pull requests](/../../pulls). While the below guidelines are necessary to get code merged, you can
3+
submit pull requests that do not adhere to them and we will try to take care of them in our spare time. We are a smallish group of developers,
4+
though, so if you can make sure the build is passing 100%, that would be very useful.
5+
6+
We recommend including details of your particular usecase(s) with any issues or pull requests. We love to hear how our libraries are being used
7+
and we can get things merged in quicker when we understand its expected usage.
8+
9+
## Pull Requests
10+
Code changes should be sent through [GitHub Pull Requests](/../../pulls). Before submitting the pull request, make sure that phpunit reports success
11+
by running:
12+
```sh
13+
./vendor/bin/phpunit
14+
```
15+
And there are not coding standard violations by running
16+
```sh
17+
./vendor/bin/phpcs
18+
```
19+
20+
## Builds
21+
Our [Travis build](https://travis-ci.org/traderinteractive/tol-api-php) executes [PHPUnit](http://www.phpunit.de) and uses [Coveralls](https://coveralls.io/) to enforce code coverage.
22+
While the build does not strictly enforce 100% code coverage, it will not allow coverage to drop below its current percentage.
23+
[Scrutinizer](https://scrutinizer-ci.com/) is used to ensure code quality and enforce the [coding standard](http://www.php-fig.org/psr/psr-2/).

.github/ISSUE_TEMPLATE.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Expected Behavior
2+
3+
## Actual Behavior
4+
5+
## Steps to reproduce the behavior
6+

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Fixes # .
2+
3+
#### What does this PR do?
4+
5+
#### Checklist
6+
- [ ] Pull request contains a clear definition of changes
7+
- [ ] Tests (either unit, integration, or acceptance) written and passing
8+
- [ ] Relevant documentation produced and/or updated

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
/vendor/
22
/coverage/
33
/clover.xml
4+
composer.lock
5+
phpunit.xml

.travis.yml

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
language: php
2-
dist: trusty
32
php:
43
- 7.0
5-
- 5.6
6-
services:
7-
- redis-server
8-
- mongodb
4+
- 7.1
5+
- 7.2
6+
- nightly
7+
env:
8+
- PREFER_LOWEST="--prefer-lowest --prefer-stable"
9+
- PREFER_LOWEST=""
10+
matrix:
11+
fast_finish: true
12+
allow_failures:
13+
- php: nightly
914
before_script:
10-
- composer self-update || true
11-
- yes '' | pecl install -f mongodb-1.1
12-
- bash -c 'while ! exec 6<>/dev/tcp/localhost/27017; do echo "$(date) - still trying to connect to mongo"; sleep 1; done'
13-
script: ./build.php
14-
after_script: ./vendor/bin/coveralls -v
15+
- composer update $PREFER_LOWEST
16+
script:
17+
- ./vendor/bin/phpunit --coverage-clover clover.xml
18+
after_success: ./vendor/bin/coveralls -v

LICENSE.md renamed to LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2014 Dominion Enterprises
3+
Copyright (c) 2017 Trader Interactive
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of
66
this software and associated documentation files (the "Software"), to deal in

README.md

Lines changed: 34 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
# tol-api-php
2-
[![Build Status](http://img.shields.io/travis/dominionenterprises/tol-api-php.svg?style=flat)](https://travis-ci.org/dominionenterprises/tol-api-php)
3-
[![Scrutinizer Code Quality](http://img.shields.io/scrutinizer/g/dominionenterprises/tol-api-php.svg?style=flat)](https://scrutinizer-ci.com/g/dominionenterprises/tol-api-php/)
4-
[![Code Coverage](http://img.shields.io/coveralls/dominionenterprises/tol-api-php.svg?style=flat)](https://coveralls.io/r/dominionenterprises/tol-api-php)
2+
[![Build Status](https://travis-ci.org/traderinteractive/tol-api-php.svg?branch=master)](https://travis-ci.org/traderinteractive/tol-api-php)
3+
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/traderinteractive/tol-api-php/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/traderinteractive/tol-api-php/?branch=master)
4+
[![Coverage Status](https://coveralls.io/repos/github/traderinteractive/tol-api-php/badge.svg?branch=master)](https://coveralls.io/github/traderinteractive/tol-api-php?branch=master)
55

6-
[![Latest Stable Version](http://img.shields.io/packagist/v/dominionenterprises/tol-api.svg?style=flat)](https://packagist.org/packages/dominionenterprises/tol-api)
7-
[![Total Downloads](http://img.shields.io/packagist/dt/dominionenterprises/tol-api.svg?style=flat)](https://packagist.org/packages/dominionenterprises/tol-api)
8-
[![License](http://img.shields.io/packagist/l/dominionenterprises/tol-api.svg?style=flat)](https://packagist.org/packages/dominionenterprises/tol-api)
6+
[![Latest Stable Version](https://poser.pugx.org/traderinteractive/tol-api/v/stable)](https://packagist.org/packages/traderinteractive/tol-api)
7+
[![Latest Unstable Version](https://poser.pugx.org/traderinteractive/tol-api/v/unstable)](https://packagist.org/packages/traderinteractive/tol-api)
8+
[![License](https://poser.pugx.org/traderinteractive/tol-api/license)](https://packagist.org/packages/traderinteractive/tol-api)
9+
10+
[![Total Downloads](https://poser.pugx.org/traderinteractive/tol-api/downloads)](https://packagist.org/packages/traderinteractive/tol-api)
11+
[![Daily Downloads](https://poser.pugx.org/traderinteractive/tol-api/d/daily)](https://packagist.org/packages/traderinteractive/tol-api)
12+
[![Monthly Downloads](https://poser.pugx.org/traderinteractive/tol-api/d/monthly)](https://packagist.org/packages/traderinteractive/tol-api)
913

1014
This is a PHP client for [REST](http://en.wikipedia.org/wiki/Representational_state_transfer) APIs like the TraderOnline APIs.
1115

1216
## Requirements
1317

14-
This api client requires PHP 5.4 or newer and uses composer to install further PHP dependencies. See the [composer specification](composer.json) for more details. For caching, there are also optional dependencies on mongo (using the 1.3+ PECL extension) and redis (via the Predis library).
18+
This api client requires PHP 7.0 or newer and uses composer to install further PHP dependencies. See the [composer specification](composer.json) for more details.
1519

16-
When contributing, access to a working mongo database for testing is needed. See the [Contribution Guidelines](#Contributing) for more details.
20+
When contributing, access to a working mongo database for testing is needed. See the [Contribution Guidelines](.github/CONTRIBUTING.md) for more details.
1721

1822
## Installation
1923

2024
tol-api-php can be installed for use in your project using [composer](http://getcomposer.org).
2125

2226
The recommended way of using this library in your project is to add a `composer.json` file to your project. The following contents would add tol-api-php as a dependency:
2327

24-
```json
25-
{
26-
"require": {
27-
"dominionenterprises/tol-api": "~0.1.0"
28-
}
29-
}
28+
```sh
29+
composer require traderinteractive/tol-api
3030
```
3131

3232
## Basic Usage
@@ -35,12 +35,13 @@ The basic guzzle client, without caching or automated pagination handling is mos
3535

3636
To instantiate a client, you need the guzzle adapter, client id, client secret, and API url. This client should work with apis like the TOL APIs.
3737
```php
38-
$apiAdapter = new \DominionEnterprises\Api\GuzzleAdapter();
39-
$auth = \DominionEnterprisees\Api\Authentication::createClientCredentials(
38+
use TraderInteractive\Api;
39+
$apiAdapter = new Api\GuzzleAdapter();
40+
$auth = Api\Authentication::createClientCredentials(
4041
'clientId',
4142
'clientSecret'
4243
)
43-
$apiClient = new \DominionEnterprises\Api\Client(
44+
$apiClient = new Api\Client(
4445
$apiAdapter,
4546
$auth,
4647
'https://baseApiUrl/v1'
@@ -56,11 +57,11 @@ $response = $apiClient->index(
5657
array('aFilter' => '5')
5758
);
5859

59-
if ($response->getHttpCode() !== 200) {
60+
if ($response->getStatusCode() !== 200) {
6061
throw new Exception('Non successful index call');
6162
}
6263

63-
$body = $response->getResponse();
64+
$body = json_decode($response->getBody(), true);
6465
$total = $body['pagination']['total'];
6566

6667
// Loop over the first page of items
@@ -76,11 +77,11 @@ For getting just a single item back from the api, you can use the `get` method:
7677
// Get item 1234
7778
$response = $apiClient->get('resourceName', '1234');
7879

79-
if ($response->getHttpCode() !== 200) {
80+
if ($response->getStatusCode() !== 200) {
8081
throw new Exception('Failed to fetch item 1234');
8182
}
8283

83-
$item = $response->getResponse();
84+
$item = json_decode($response->getBody(), true);
8485
echo "Fetched item {$item['foo']}\n";
8586
```
8687

@@ -96,11 +97,11 @@ $response = $apiClient->post(
9697
)
9798
);
9899

99-
if ($response->getHttpCode() !== 201) {
100+
if ($response->getStatusCode() !== 201) {
100101
throw new Exception('Failed to create item foo');
101102
}
102103

103-
$item = $response->getResponse();
104+
$item = json_decode($response->getBody(), true);
104105
echo $item['result']['foo'];
105106
```
106107

@@ -113,7 +114,7 @@ $response = $apiClient->put(
113114
array('bing' => array('foo' => 'bar'))
114115
);
115116

116-
if ($response->getHttpCode() !== 200) {
117+
if ($response->getStatusCode() !== 200) {
117118
throw new Exception('Failed to update item 1234');
118119
}
119120
```
@@ -123,7 +124,7 @@ For deleting an item, you can use the `delete` method:
123124
// Delete item 1234.
124125
$response = $apiClient->delete('resourceName', '1234');
125126

126-
if ($response->getHttpCode() !== 204) {
127+
if ($response->getStatusCode() !== 204) {
127128
throw new Exception('Failed to delete item 1234');
128129
}
129130
```
@@ -136,75 +137,32 @@ $handleTwo = $apiClient->startGet('resourceName', '5678');
136137
$responseOne = $apiClient->end($handleOne);
137138
$responseTwo = $apiClient->end($handleTwo);
138139

139-
if ($responseOne->getHttpCode() !== 200) {
140+
if ($responseOne->getStatusCode() !== 200) {
140141
throw new Exception('Failed to fetch item 1234');
141142
}
142143

143-
if ($responseTwo->getHttpCode() !== 200) {
144+
if ($responseTwo->getStatusCode() !== 200) {
144145
throw new Exception('Failed to fetch item 5678');
145146
}
146147

147-
$itemOne = $responseOne->getResponse();
148-
$itemTwo = $responseTwo->getResponse();
148+
$itemOne = json_decode($responseOne->getBody(), true);
149+
$itemTwo = json_decode($responseTwo->getBody(), true);
149150

150151
echo "Fetched item {$itemOne['foo']}\n";
151152
echo "Fetched item {$itemTwo['foo']}\n";
152153
```
153154

154155
### Cache
155-
156-
Here's an example of instantiating the included mongo cache adapter.
157-
158-
Note that it is important to call ensureIndexes() on the mongo cache adapter at least once before using the cache. This can be done when building/deploying the application, manually before release, or on application startup. Without calling this method, the index for expiry won't be set and the responses will never be removed from the collection effectively making all cached objects stay cached forever. **Important**: Calling it every time you use the cache is not recommended because it can be a very expensive call that can cause extreme load on the mongo database.
159-
160-
```php
161-
$cacheAdapter = new \DominionEnterprises\Api\MongoCache(
162-
$mongoUrl,
163-
$mongoDbName,
164-
$mongoCollectionName
165-
);
166-
167-
// This has to be called at least once to set the expiry index.
168-
// Without this call, the response won't ever expire. This should
169-
// definitely not be done on every request. It can cause extreme
170-
// load on the mongo database and should therefore be done
171-
// sparingly.
172-
$cacheAdapter->ensureIndexes();
173-
```
174-
175-
And then later, you can use the cache adapter when creating the client and all of the GET requests made using that client will be sent through the cache to check if they exist, and if not, any successful responses from the API that include Expires headers will be cached for future calls.
176-
177-
```php
178-
$cacheAdapter = new \DominionEnterprises\Api\MongoCache(
179-
$mongoUrl,
180-
$mongoDbName,
181-
$mongoCollectionName
182-
);
183-
184-
$apiClient = new \DominionEnterprises\Api\Client(
185-
$apiAdapter,
186-
$auth,
187-
$apiBaseUrl,
188-
$cacheAdapter
189-
);
190-
191-
// Assuming this doesn't exist in the cache, it will fetch from the
192-
// API and store the result in the cache.
193-
$apiClient->index('resourceName');
194-
195-
// Because of the above call, the response should already be
196-
// cached. This will mean that no further requests are being made
197-
// to the API.
198-
$apiClient->index('resourceName');
199-
```
156+
The library allows for a [PSR-16 SimpleCache](https://www.php-fig.org/psr/psr-16/) implementation.
200157

201158
### Collection
202159

203160
This is the preferred way to make index requests so that you don't have to handle (or forget to handle!) pagination yourself. Using this iterator is simple with the API Client. As an example, here is a snippet of code that will create a dropdown list of items.
161+
**WARNING** Updates should not be performed to the items in the collection while interating as this may change the pagination.
204162
```php
205163
<ul>
206164
<?php
207-
$items = new \DominionEnterprises\Api\Collection(
165+
$items = new \TraderInteractive\Api\Collection(
208166
$apiClient,
209167
'resourceName',
210168
array('aFilter' => '5')
@@ -218,10 +176,7 @@ foreach ($items as $item) {
218176

219177
## Contributing
220178

221-
If you would like to contribute, please use our build process for any changes and after the build passes, send us a pull request on github! The build requires a running mongo and redis. The URI's to these services can be specified via environment variables or left to their defaults (localhost on the default port):
222-
```sh
223-
TESTING_MONGO_URL=mongodb://127.0.0.1:27017 TESTING_REDIS_URL=tcp://127.0.0.7:6379 ./build.php
224-
```
179+
If you would like to contribute, please use our build process for any changes and after the build passes, send us a pull request on github!
225180

226181
There is also a [docker](http://www.docker.com/)-based [fig](http://www.fig.sh/) configuration that will standup docker containers for the databases, execute the build inside a docker container, and then terminate everything. This is an easy way to build the application:
227182
```sh

build.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)