Skip to content

Commit

Permalink
Committing Library
Browse files Browse the repository at this point in the history
  • Loading branch information
JonnyD committed Jan 13, 2016
0 parents commit 1c85adb
Show file tree
Hide file tree
Showing 27 changed files with 2,168 additions and 0 deletions.
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2011-2015 Jonathan Devine, https://github.com/jonnyd <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
96 changes: 96 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Peggy
A PHP Client for 80Legs.com API.

## Installation
Add to composer.json

```"jonnyd/peggy": "dev-master"```

## Usage
### Initialize
```php
$peggy = new Peggy\Client('<your api key>');
```
### Making API Calls
#### Crawls
```php
// create crawl
$request = $peggy->crawl()->createCrawlRequest($crawlName, $appName, $urllist, $maxDepth, $maxUrls);
$peggy->crawl()->create($request);

// get crawl
$crawl = $peggy->crawl()->get($crawlName);
echo $crawl->getName();

// cancel crawl
$peggy->crawl()->cancel($crawlName);

// get all crawls
$allCrawls = $peggy->crawl()->all();
foreach ($crawls->getData() as $crawl) {
echo $crawl->getName();
}
```

#### Results
```php
// get result
$result = $peggy->result()->get($crawlName);
$urls = $result->getUrls(); // Returns the results of the crawl specified by CRAWL_NAME. This will return a 404 if no results have been posted. Example Url: "http://s3.amazonaws.com/results1"
```

#### Apps
```php
// upload app
$request = $peggy->app()->createAppRequest($name, $filePath);
$peggy->app()->upload($request);

// get app
$app = $peggy->app()->get($appName); // this API is broken on 80legs.com

// remove app
$peggy->app()->remove($appName);

// get all apps
$allApps = $peggy->app()->all();
foreach ($allApps->getData() as $app) {
echo $app->getName();
}
```

#### Url Lists
```php
// create url list
$request = $peggy->urllist()->createUrllistRequest($name, $filePath);
$peggy->urllist()->upload($request);

// get url list
$urllist = $peggy->urllist()->get($name);
echo $urllist->getName();

// remove url list
$peggy->urllist->remove($name);

// get all url lists
$allUrllists = $peggy->urllist()->all();
foreach ($allUrllists->getData() as $urllist) {
echo $urllist->getName();
}
```

#### User
```php
// get me
$me = $peggy->user()->me();

// get user
$user = $peggy->user()->get($token);
```

## Todo
* Tests
* Error Handling
* anything else?

## Pull Requests
Welcomed!
31 changes: 31 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "jonnyd/peggy",
"type": "library",
"description": "PHP wrapper for 80legs.com API",
"keywords": ["framework", "http", "rest", "web service", "curl", "client", "HTTP client"],
"homepage": "https://github.com/JonnyD/Peggy",
"license": "MIT",
"authors": [
{
"name": "Jonathan Devine",
"email": "[email protected]",
"homepage": "https://github.com/jonnyd"
}
],
"require": {
"php": ">=5.5.0",
"guzzlehttp/guzzle": "*",
"ext-curl": "*",
"jms/serializer": "~0.15"
},
"require-dev": {
"ext-curl": "*",
"phpunit/phpunit": "~4.0",
"psr/log": "~1.0"
},
"autoload": {
"psr-4": {
"Peggy\\": "src/Peggy/"
}
}
}
19 changes: 19 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Peggy Test Suite">
<directory>tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">src/Monolog/</directory>
</whitelist>
</filter>

<php>
<ini name="date.timezone" value="UTC"/>
</php>
</phpunit>
79 changes: 79 additions & 0 deletions src/Peggy/API/AbstractAPI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Peggy\API;

use Peggy\Client;
use Psr\Http\Message\ResponseInterface as GuzzleResponseInterface;
use Peggy\Response\ResponseInterface as PeggyResponseInterface;

abstract class AbstractAPI
{
/**
* @var Client $client
*/
protected $client;

/**
* @param Client $client
*/
public function __construct(Client $client)
{
$this->client = $client;
}

/**
* @param $path
* @param null $deserializeTo
* @param array $parameters
* @param array $headers
* @return PeggyResponseInterface|GuzzleResponseInterface|null
*/
protected function getRequest($path, $deserializeTo = null, array $parameters = [], $headers = [])
{
$httpClient = $this->client->getHttpClient();
$response = $httpClient->get($path, $deserializeTo, $parameters, $headers);
return $response;
}

/**
* @param $path
* @param null $deserializeTo
* @param null $body
* @param array $headers
* @return PeggyResponseInterface|GuzzleResponseInterface|null
*/
protected function postRequest($path, $deserializeTo = null, $body = null, array $headers = [])
{
$httpClient = $this->client->getHttpClient();
$response = $httpClient->post($path, $deserializeTo, $body, $headers);
return $response;
}

/**
* @param $path
* @param null $deserializeTo
* @param null $body
* @param array $headers
* @return PeggyResponseInterface|GuzzleResponseInterface|null
*/
protected function putRequest($path, $deserializeTo = null, $body = null, array $headers = [])
{
$httpClient = $this->client->getHttpClient();
$response = $httpClient->put($path, $deserializeTo, $body, $headers);
return $response;
}

/**
* @param $path
* @param null $deserializeTo
* @param null $body
* @param array $headers
* @return PeggyResponseInterface|GuzzleResponseInterface|null
*/
protected function deleteRequest($path, $deserializeTo = null, $body = null, array $headers = [])
{
$httpClient = $this->client->getHttpClient();
$response = $httpClient->delete($path, $deserializeTo, $body, $headers);
return $response;
}
}
61 changes: 61 additions & 0 deletions src/Peggy/API/App.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Peggy\API;

use Peggy\Request\App\CreateAppRequest;
use Peggy\Response\App\AppResponse;
use Peggy\Response\App\ListAppsResponse;
use GuzzleHttp\Psr7\Response as GuzzleResponse;

class App extends AbstractAPI
{
const APP_RESPONSE_CLASS = 'Peggy\Response\App\AppResponse';
const LIST_APPS_RESPONSE_CLASS = 'Peggy\Response\App\ListAppsResponse';

/**
* @return ListAppsResponse
*/
public function all()
{
return $this->getRequest('apps/', self::LIST_APPS_RESPONSE_CLASS);
}

/**
* @param CreateAppRequest $request
* @return AppResponse
*/
public function upload(CreateAppRequest $request)
{
$headers = ['content-type' => 'application/octet-stream'];
return $this->putRequest('apps/'.$request->getName(), self::APP_RESPONSE_CLASS, $request, $headers);
}

/**
* @param string $appName
* @return GuzzleResponse
*/
public function get($appName)
{
//todo API doesn't work as expected on 80legs.com
return $this->getRequest('apps/'.urlencode($appName));
}

/**
* @param string $appName
* @return null
*/
public function remove($appName)
{
return $this->deleteRequest('apps/'.urlencode($appName));
}

/**
* @param string $name
* @param string $filePath
* @return CreateAppRequest
*/
public function createAppRequest($name, $filePath)
{
return new CreateAppRequest($name, $filePath);
}
}
64 changes: 64 additions & 0 deletions src/Peggy/API/Crawl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Peggy\API;

use Peggy\Request\Crawl\CreateCrawlRequest;
use Peggy\Response\Crawl\ListCrawlsResponse;
use Peggy\Response\Crawl\CrawlResponse;

class Crawl extends AbstractAPI
{
const CRAWL_RESPONSE_CLASS = 'Peggy\Response\Crawl\CrawlResponse';
const LIST_CRAWLS_RESPONSE_CLASS = 'Peggy\Response\Crawl\ListCrawlsResponse';

/**
* @return ListCrawlsResponse
*/
public function all()
{
return $this->getRequest('crawls/', self::LIST_CRAWLS_RESPONSE_CLASS);
}

/**
* @param CreateCrawlRequest $request
* @return CrawlResponse
* @throws \Exception
*/
public function create(CreateCrawlRequest $request)
{
$headers = ['content-type' => 'application/json'];
$response = $this->postRequest('crawls/' . $request->getName(), self::CRAWL_RESPONSE_CLASS, $request, $headers);
return $response;
}

/**
* @param string $crawlName
* @return CrawlResponse
*/
public function get($crawlName)
{
return $this->getRequest('crawls/'.urlencode($crawlName), self::CRAWL_RESPONSE_CLASS);
}

/**
* @param string $crawlName
* @return null
*/
public function cancel($crawlName)
{
return $this->deleteRequest('crawls/'.urlencode($crawlName));
}

/**
* @param string $name
* @param string $app
* @param string $urllist
* @param integer $maxDepth
* @param integer $maxUrls
* @return CreateCrawlRequest
*/
public function createCrawlRequest($name, $app, $urllist, $maxDepth, $maxUrls)
{
return new CreateCrawlRequest($name, $app, $urllist, $maxDepth, $maxUrls);
}
}
19 changes: 19 additions & 0 deletions src/Peggy/API/Result.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Peggy\API;

use Peggy\Response\Result\ResultResponse;

class Result extends AbstractAPI
{
const RESULT_RESPONSE_CLASS = 'Peggy\Response\Result\ResultResponse';

/**
* @param string $crawlName
* @return ResultResponse
*/
public function get($crawlName)
{
return $this->getRequest('results/'.urlencode($crawlName), self::RESULT_RESPONSE_CLASS);
}
}
Loading

0 comments on commit 1c85adb

Please sign in to comment.