Skip to content

Commit

Permalink
Started work on Group resource code
Browse files Browse the repository at this point in the history
  • Loading branch information
Rene Schmidt committed Jan 14, 2016
1 parent fb8bd5f commit 97230d2
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 14 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,17 +338,17 @@ $client = new Client(

| Resource | Support grade |
| ---------------------- | ------------- |
| Account | :+1: (except migrate) |
| Starred Files | contribute! |
| Group | contribute! |
| File Share Link | partial |
| Library/Library | partial |
| Library/File | partial |
| Library/Directory | partial |
| Library/Multiple Files | :+1: |
| Avatar | :+1: |
| Events | contribute! |
| Organization | contribute! |
| Account | :large_blue_circle::large_blue_circle::large_blue_circle::white_circle: |
| Starred Files | :white_circle::white_circle::white_circle::white_circle: |
| Group | :large_blue_circle::white_circle::white_circle::white_circle: |
| File Share Link | :large_blue_circle::large_blue_circle::white_circle::white_circle: |
| Library/Library | :large_blue_circle::large_blue_circle::white_circle::white_circle: |
| Library/File | :large_blue_circle::large_blue_circle::white_circle::white_circle: |
| Library/Directory | :large_blue_circle::large_blue_circle::white_circle::white_circle: |
| Library/Multiple Files | :large_blue_circle::large_blue_circle::large_blue_circle::large_blue_circle: |
| Avatar | :large_blue_circle::large_blue_circle::large_blue_circle::large_blue_circle: |
| Events | :white_circle::white_circle::white_circle::white_circle: |
| Organization | :white_circle::white_circle::white_circle::white_circle: |

## Seafile server compatibility

Expand Down
66 changes: 66 additions & 0 deletions bin/examples/group.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/**
* Attention: This example script will modify the test library! Do not run this script
* unless you are prepared for that.
*/

require_once __DIR__ . '/../../vendor/autoload.php';

use Seafile\Client\Resource\Group;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\MessageFormatter;
use Monolog\Logger;
use Seafile\Client\Http\Client;

$logger = new Logger('Logger');

$stack = HandlerStack::create();
$stack->push(
Middleware::log(
$logger,
new MessageFormatter("{hostname} {req_header_Authorization} - {req_header_User-Agent} - [{date_common_log}] \"{method} {host}{target} HTTP/{version}\" {code} {res_header_Content-Length} req_body: {req_body} response_body: {res_body}")
)
);

/**
* Example:
* {"token": "your_token"}
*/
$tokenFile = getenv("HOME") . "/.seafile-php-sdk/api-token.json";
$cfgFile = getenv("HOME") . "/.seafile-php-sdk/cfg.json";

if (!is_readable($tokenFile)) {
throw new Exception($tokenFile . ' is not readable or does not exist.');
}

if (!is_readable($cfgFile)) {
throw new Exception($cfgFile . ' is not readable or does not exist.');
}

$token = json_decode(file_get_contents($tokenFile));
$cfg = json_decode(file_get_contents($cfgFile));

$client = new Client(
[
'base_uri' => $cfg->baseUri,
'debug' => true,
'handler' => $stack,
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Token ' . $token->token
]
]
);

$groupResource = new Group($client);
$logger->log(Logger::INFO, "#################### Get all groups ");

$groups = $groupResource->getAll();

foreach ($groups as $group) {
$logger->log(Logger::INFO, "#################### " . sprintf("Group name: %s", $group->name));
}

print(PHP_EOL . 'Done' . PHP_EOL);
41 changes: 41 additions & 0 deletions lib/Resource/Group.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Seafile\Client\Resource;

use \Seafile\Client\Type\Group as GroupType;

/**
* Handles everything regarding Seafile groups.
*
* PHP version 5
*
* @category API
* @package Seafile\Resource
* @author Rene Schmidt DevOps UG (haftungsbeschränkt) & Co. KG <[email protected]>
* @copyright 2015 Rene Schmidt DevOps UG (haftungsbeschränkt) & Co. KG <[email protected]>
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/rene-s/seafile-php-sdk
*/
class Group extends AbstractResource
{

/**
* List groups
*
* @return GroupType[]
*/
public function getAll()
{
$response = $this->client->request('GET', $this->client->getConfig('base_uri') . '/groups/');

$json = json_decode($response->getBody());

$groupCollection = [];

foreach ($json->groups as $group) {
$groupCollection[] = (new GroupType)->fromJson($group);
}

return $groupCollection;
}
}
21 changes: 21 additions & 0 deletions test/assets/GroupTest_getAll.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"replynum": 0,
"groups": [
{
"ctime": 1452804381467425,
"creator": "[email protected]",
"msgnum": 0,
"mtime": 0,
"id": 1,
"name": "A"
},
{
"ctime": 1452804387552160,
"creator": "[email protected]",
"msgnum": 0,
"mtime": 0,
"id": 2,
"name": "B"
}
]
}
4 changes: 2 additions & 2 deletions test/unit/Resource/DirectoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class DirectoryTest extends TestCase
{
/**
* getAll()
* Test getAll()
*
* @return void
*/
Expand All @@ -46,7 +46,7 @@ public function testGetAll()
}

/**
* getAll() with directory path
* Test getAll() with directory path
*
* @return void
*/
Expand Down
46 changes: 46 additions & 0 deletions test/unit/Resource/GroupTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Seafile\Client\Tests\Resource;

use GuzzleHttp\Psr7\Response;
use Seafile\Client\Resource\Group;
use Seafile\Client\Tests\TestCase;

/**
* Group resource test
*
* PHP version 5
*
* @category API
* @package Seafile\Resource
* @author Rene Schmidt DevOps UG (haftungsbeschränkt) & Co. KG <[email protected]>
* @copyright 2015 Rene Schmidt DevOps UG (haftungsbeschränkt) & Co. KG <[email protected]>
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/rene-s/seafile-php-sdk
*/
class GroupTest extends TestCase
{
/**
* Test getAll()
*
* @return void
*/
public function testGetAll()
{
$groupResource = new Group($this->getMockedClient(
new Response(
200,
['Content-Type' => 'application/json'],
file_get_contents(__DIR__ . '/../../assets/GroupTest_getAll.json')
)
));

$groups = $groupResource->getAll();

$this->assertInternalType('array', $groups);

foreach ($groups as $group) {
$this->assertInstanceOf('Seafile\Client\Type\Group', $group);
}
}
}
2 changes: 1 addition & 1 deletion test/unit/Resource/LibraryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class LibraryTest extends TestCase
{
/**
* getAll()
* Test getAll()
*
* @return void
*/
Expand Down

0 comments on commit 97230d2

Please sign in to comment.