Skip to content

Commit

Permalink
Package improvements (#1)
Browse files Browse the repository at this point in the history
* Container name changed now that we are using a faster docker-compose.
* Put the default token in the Client class as a constant.
* Add FetchAll query for fetching all users.
  • Loading branch information
jonpugh authored Mar 2, 2021
1 parent 88ffbe0 commit 309f43a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/LagoonClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class LagoonClient implements LagoonClientInterface {
*/
protected $client;

/**
* The default token used for development instances of the Lagoon API.
*/
const defaultToken = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlIjoiYWRtaW4iLCJpc3MiOiJhcGktZGF0YS13YXRjaGVyLXB1c2hlciIsImF1ZCI6ImFwaS5kZXYiLCJzdWIiOiJhcGktZGF0YS13YXRjaGVyLXB1c2hlciJ9.GiSJpvNXF2Yj9IXVCsp7KrxVp8N2gcp7-6qpyNOakVw';

/**
* An array of additional headers to send with the request.
*/
Expand All @@ -30,7 +35,8 @@ class LagoonClient implements LagoonClientInterface {
/**
* {@inheritdoc}
*/
public function __construct($endpoint, $token) {
public function __construct($endpoint, $token = null) {
$token = $token?: self::defaultToken;
$this->client = new Client($endpoint);
$this->headers = [
'Authorization' => "Bearer {$token}"
Expand Down
17 changes: 16 additions & 1 deletion src/Operation/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Lagoon\Operation;

use Lagoon\Operation\LagoonOperationBase;
use Lagoon\Query\Users\FetchAll;
use Lagoon\Query\Users\UsersBySshKey;
use Lagoon\Mutation\User\Add;
use Lagoon\Mutation\User\AddUserToProject;
Expand All @@ -18,14 +19,18 @@ class User extends LagoonOperationBase {
const SSH_KEY = 'find_ssh_key';
const ADD = 'add';
const ADD_TO_PROJECT = 'add_to_project';
const FETCH_ALL = 'all';

/**
* {@inheritdoc}
*/
protected function bind() {
$this->addQuery(self::SSH_KEY, UsersBySshKey::class)
->addQuery(self::FETCH_ALL, FetchAll::class)
->addMutation(self::ADD, Add::class)
->addMutation(self::ADD_TO_PROJECT, AddUserToProject::class);
->addMutation(self::ADD_TO_PROJECT, AddUserToProject::class)
;

}

/**
Expand Down Expand Up @@ -73,4 +78,14 @@ public function addToProject($email, $group) {
'group' => $group,
]);
}

/**
* Fetch all users from the API.
*
* @return Lagoon\LagoonQueryInterface
* The lagoon query object.
*/
public function all() {
return $this->query(SELF::FETCH_ALL);
}
}
36 changes: 36 additions & 0 deletions src/Query/Users/FetchAll.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Lagoon\Query\Users;

use Lagoon\LagoonQueryBase;

/**
* Fetch all projects from Lagoon.
*/
class FetchAll extends LagoonQueryBase {

/**
* {@inheritdoc}
*/
protected function expectedKeys() {
return [];
}

/**
* {@inheritdoc}
*/
protected function query() {
return <<<'QUERY'
query findAll {
allGroups {
members {
user {
id
email
}
}
}
}
QUERY;
}
}
2 changes: 1 addition & 1 deletion tests/scripts/api-start
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ cd $THIS_FILE_PATH
docker-compose up -d

# Connects the Lando container group to the API container.
docker network connect demolagoonsh_default lagoon_api_1 \
docker network connect demolagoonsh_default tests_api_1 \
|| echo "Docker network connect failed, but that's ok. Moving on."

0 comments on commit 309f43a

Please sign in to comment.