Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
PGBI committed Apr 19, 2016
0 parents commit 2785c81
Show file tree
Hide file tree
Showing 12 changed files with 961 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/.idea
*.DS_Store
/vendor
composer.lock
tmp
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: php

php:
- 5.5
- 5.6

install:
- composer self-update
- composer install

script:
- vendor/bin/phpunit --coverage-clover=coverage.xml

after_success:
- bash <(curl -s https://codecov.io/bash)
89 changes: 89 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Classy PHP SDK [![Build Status](https://travis-ci.org/classy-org/classy-php-sdk.png?branch=master)](https://travis-ci.org/classy-org/classy-php-sdk) [![codecov.io](https://codecov.io/github/classy-org/classy-php-sdk/coverage.svg?branch=master)](https://codecov.io/github/classy-org/classy-php-sdk?branch=master)

This repository contains a php HTTP client library to let your php App interact with Classy's API.

## Installation

The Classy PHP SDK can be installed with [Composer](https://getcomposer.org/):

```sh
composer require classy-org/classy-php-sdk
```

Be sure you included composer autoloader in your app:

```php
require_once '/path/to/your/project/vendor/autoload.php';
```

## Usage

### Basic example

```php
$client = new \Classy\Client([
'client_id' => 'your_client_id',
'client_secret' => 'your_client_secret',
'version' => '2.0' // version of the API to be used
]);

$session = $client->newAppSession();

// Get information regarding the campaign #1234
$campaign = $client->get('/campaigns/1234', $session);

// Access the campaign goal: $campaign->goal

// Unpublish the campaign
$client->post('/campaign/1234/deactivate', $session);
```

### Sessions handling

Sessions have an expiration date. It is possible to refresh a session:

```php
if ($session->expired()) {
$client->refresh($session)
}

// $session->expired() is now false.
```

Sessions are serializable, they can be saved an reused to reduce the amount of API calls:

```php
$client = new \Classy\Client([
'client_id' => 'your_client_id',
'client_secret' => 'your_client_secret',
'version' => '2.0' // version of the API to be used
]);

// Retrieve the session from a file
$session = unserialize(file_get_contents("path/to/a/cache/file"));

// ... work with the API...

// Save the session for later
file_put_contents("path/to/a/cache/file", serialize($session));
```

### Errors handling

This client can throw two types of Exceptions:

* Classy\Exceptions\SDKException when the SDK is misused
* Classy\Exceptions\APIResponseException when the API is not returning an OK response

```php
try {
$response = $client->get('/endpoint', $session);
} catch (\Classy\Exceptions\APIResponseException $e) {
// Get the HTTP response code
$code = $e->getCode();
// Get the response content
$content = $e->getResponseData();
// Get the response headers
$headers = $e->getResponseHeaders();
}
```
22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "classy-org/classy-php-sdk",
"type": "library",
"description": "Php HTTP client library for Classy API",
"require": {
"guzzlehttp/guzzle": "^6.1"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
"mockery/mockery": "^0.9.4"
},
"autoload": {
"psr-4": {
"Classy\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Classy\\Tests\\": "tests"
}
}
}
13 changes: 13 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
Loading

0 comments on commit 2785c81

Please sign in to comment.