Skip to content

Commit

Permalink
Merge pull request #2 from uvasoftware/v3
Browse files Browse the repository at this point in the history
V3.0.0
  • Loading branch information
rferreira authored Sep 5, 2017
2 parents faf4ce7 + 5be9900 commit c854389
Show file tree
Hide file tree
Showing 5 changed files with 381 additions and 82 deletions.
12 changes: 7 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
"description": "A pure PHP interface to Scanii, a web based virus scanning engine (https://scanii.com)",
"type": "library",
"require": {
"php": "^5.5 || ^7.0",
"guzzlehttp/guzzle": "^6.2",
"php": "^7.0",
"guzzlehttp/guzzle": "^6.2"
},
"require-dev": {
"phpunit/phpunit": "^6.0"
},
"license" : "Apache 2.0",
"license": "Apache 2.0",
"autoload": {
"psr-0" : {
"Scanii\\" : "src/"
"psr-0": {
"Scanii\\": "src/"
}
}
}
11 changes: 11 additions & 0 deletions src/Scanii/HttpHeaders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Scanii;

abstract class HttpHeaders
{
const LOCATION = 'Location';
const X_RUNTIME_HEADER = 'X-Runtime';
const X_HOST_HEADER = 'X-Scanii-Host-Id';
const X_REQUEST_HEADER = 'X-Scanii-Request-Id';
}
121 changes: 86 additions & 35 deletions src/Scanii/ScaniiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,66 +10,69 @@
*/
class ScaniiClient
{
private $version, $verbose;
private $verbose, $httpClient;

// version constant - always update when changes are made:
const VERSION = '3.0.0';

/**
* ScaniiClient constructor.
* @param $key String API key
* @param $secret String API secret
* @param bool $verbose turn on verbose mode on the http client and lib
* @param string $base_url optional base url to be used @see ScaniiTarget
* @param string $baseUrl optional base url to be used @see ScaniiTarget
*/
function __construct($key, $secret, $verbose = false, $base_url = ScaniiTarget::v2_1)
function __construct($key, $secret, $verbose = false, $baseUrl = ScaniiTarget::v2_1)
{

assert(strlen($key) > 0);
assert(strlen($secret) > 0);

$this->verbose = $verbose;
$this->version = 'scanii-php/v2.1';

// small workaround for guzzle base uri handling
if (substr($base_url, -1) != '/') {
$base_url = $base_url . '/';
if (substr($baseUrl, -1) != '/') {
$baseUrl = $baseUrl . '/';
}

$this->http_client = new GuzzleHttp\Client([
'base_uri' => $base_url,
$this->httpClient = new GuzzleHttp\Client([
'base_uri' => $baseUrl,
'connect_timeout' => 30,
'read_timeout' => 30,
'debug' => $verbose,
'auth' => [$key, $secret],
'headers' => [
'User-Agent' => $this->version
'User-Agent' => 'scanii-php/v' . self::VERSION
]
]);
return $this;
}


/**
* Fetches the results of a previously processed file @see <a href="http://docs.scanii.com/v2.1/resources.html#files">http://docs.scanii.com/v2.1/resources.html#files</a>
* Fetches the results of a previously processed file @link <a href="http://docs.scanii.com/v2.1/resources.html#files">http://docs.scanii.com/v2.1/resources.html#files</a>
* @param $id String processing file id to retrieve results for
* @return \Psr\Http\Message\StreamInterface
* @return ScaniiResult
*/
public function retrieve($id)
public function retrieve($id): ScaniiResult
{
$this->log('loading result ' . $id);

$res = $this->http_client->request('GET', 'files/' . $id);
$res = $this->httpClient->request('GET', 'files/' . $id);

$this->log('content ' . $res->getBody());
$this->log('status code ' . $res->getStatusCode());

return $res->getBody();
return new ScaniiResult((string)$res->getBody(), $res->getHeaders());
}

/**
* Submits a file to be processed @see <a href="http://docs.scanii.com/v2.1/resources.html#files">http://docs.scanii.com/v2.1/resources.html#files</a>
* Submits a file to be processed @link <a href="http://docs.scanii.com/v2.1/resources.html#files">http://docs.scanii.com/v2.1/resources.html#files</a>
* @param $path String file path to the file to submit for processing
* @param array $metadata associative array of custom metadata
* @return \Psr\Http\Message\StreamInterface
* @return \Scanii\ScaniiResult
*/
public function process($path, $metadata = [])
public function process($path, $metadata = []): ScaniiResult
{

$this->log('processing ' . $path);
Expand All @@ -92,19 +95,19 @@ public function process($path, $metadata = [])

$this->log('post contents ' . var_dump($post_contents));

$r = $this->http_client->request('POST', 'files', $post_contents);
$r = $this->httpClient->request('POST', 'files', $post_contents);

$this->log("result message " . $r->getBody());
return $r->getBody();
return new ScaniiResult((string)$r->getBody(), $r->getHeaders());
}

/**
* Submits a file to be processed @see <a href="http://docs.scanii.com/v2.1/resources.html#files">http://docs.scanii.com/v2.1/resources.html#files</a>
* Submits a file to be processed @link <a href="http://docs.scanii.com/v2.1/resources.html#files">http://docs.scanii.com/v2.1/resources.html#files</a>
* @param $path String file path to the file to submit for processing
* @param array $metadata associative array of custom metadata
* @return \Psr\Http\Message\StreamInterface
* @return \Scanii\ScaniiResult
*/
public function process_async($path, $metadata = [])
public function processAsync($path, $metadata = []): ScaniiResult
{

$this->log('processing ' . $path);
Expand All @@ -127,22 +130,22 @@ public function process_async($path, $metadata = [])

$this->log('post contents ' . var_dump($post_contents));

$r = $this->http_client->request('POST', 'files/async', $post_contents);
$r = $this->httpClient->request('POST', 'files/async', $post_contents);

$this->log("result message " . $r->getBody());
return $r->getBody();
}
return new ScaniiResult((string)$r->getBody(), $r->getHeaders());

}


/**
* Makes a fetch call to scanii @see <a href="http://docs.scanii.com/v2.1/resources.html#files">http://docs.scanii.com/v2.1/resources.html#files</a>
* Makes a fetch call to scanii @link <a href="http://docs.scanii.com/v2.1/resources.html#files">http://docs.scanii.com/v2.1/resources.html#files</a>
* @param $location String the url of the content to be fetched and processed
* @param $callback String the callback url to submit the processing result to
* @param array $metadata associative array of custom metadata
* @return \Psr\Http\Message\StreamInterface
* @return \Scanii\ScaniiResult
*/
public function fetch($location, $callback, $metadata = [])
public function fetch($location, $callback, $metadata = []): ScaniiResult
{
$this->log("fetching $location with callback: $callback");

Expand All @@ -162,19 +165,19 @@ public function fetch($location, $callback, $metadata = [])

$this->log('post contents ' . var_dump($post_contents));

$r = $this->http_client->request('POST', 'files/fetch', $post_contents);
$r = $this->httpClient->request('POST', 'files/fetch', $post_contents);

return $r->getBody();
return new ScaniiResult((string)$r->getBody(), $r->getHeaders());

}

/**
* Pings the scanii service using the credentials provided @see <a href="http://docs.scanii.com/v2.1/resources.html#ping">http://docs.scanii.com/v2.1/resources.html#ping</a>
* Pings the scanii service using the credentials provided @link <a href="http://docs.scanii.com/v2.1/resources.html#ping">http://docs.scanii.com/v2.1/resources.html#ping</a>
* @return bool
*/
public function ping(): bool
{
$r = $this->http_client->request('GET', 'ping');
$r = $this->httpClient->request('GET', 'ping');
return $r->getStatusCode() == 200;
}

Expand All @@ -185,13 +188,63 @@ private function log($message)
}
}

/**
* Creates a new temporary authentication token @link <a href="http://docs.scanii.com/v2.1/resources.html#auth-tokens">http://docs.scanii.com/v2.1/resources.html#auth-tokens</a>
* @param int $timeout how long the token should be valid for
* @return ScaniiResult @see ScaniiResult
*/
public function createAuthToken($timeout = 300): ScaniiResult
{
assert($timeout > 0);

$this->log("creating auth token with timeout $timeout");
$post_contents = [
'form_params' => [
'timeout' => $timeout
]
];

$this->log('post contents ' . var_dump($post_contents));
$r = $this->httpClient->request('POST', 'auth/tokens', $post_contents);

return new ScaniiResult((string)$r->getBody(), $r->getHeaders());
}

/**
* Deletes a previously created authentication token
* @param $id string id of the token to be deleted
*/
public function deleteAuthToken($id): void
{
assert(strlen($id) > 0);

$this->log("deleting auth token $id");
$this->httpClient->request('DELETE', "auth/tokens/$id");
}


/**
* Retrieves a previously created auth token
* @param $id string the id of the token to be retrieved
* @return ScaniiResult
*/
public function retrieveAuthToken($id): ScaniiResult
{
assert(strlen($id) > 0);

$this->log("retrieving auth token $id");
$r = $this->httpClient->request('GET', "auth/tokens/$id");

return new ScaniiResult((string)$r->getBody(), $r->getHeaders());
}

/**
* Returns the client version
* @return string
*/
public function getVersion(): String
{
return $this->version;
return self::VERSION;
}

/**
Expand All @@ -202,8 +255,6 @@ public function isVerbose(): bool
{
return $this->verbose;
}


}


Loading

0 comments on commit c854389

Please sign in to comment.