-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from railsware/feature/projects2
Support project endpoints
- Loading branch information
Showing
16 changed files
with
611 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
use Mailtrap\Config; | ||
use Mailtrap\Helper\ResponseHelper; | ||
use Mailtrap\MailtrapClient; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
// your API token from here https://mailtrap.io/api-tokens | ||
$apiKey = getenv('MAILTRAP_API_KEY'); | ||
$mailtrap = new MailtrapClient(new Config($apiKey)); | ||
|
||
/** | ||
* List projects and their inboxes to which the API token has access. | ||
* | ||
* GET https://mailtrap.io/api/accounts/{account_id}/projects | ||
*/ | ||
try { | ||
$accountId = getenv('MAILTRAP_ACCOUNT_ID'); | ||
|
||
$response = $mailtrap->sandbox()->projects()->getList($accountId); | ||
|
||
// print the response body (array) | ||
var_dump(ResponseHelper::toArray($response)); | ||
} catch (Exception $e) { | ||
echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
} | ||
|
||
|
||
/** | ||
* Get the project and its inboxes. | ||
* | ||
* GET https://mailtrap.io/api/accounts/{account_id}/projects/{project_id} | ||
*/ | ||
try { | ||
$accountId = getenv('MAILTRAP_ACCOUNT_ID'); | ||
$projectId = getenv('MAILTRAP_PROJECT_ID'); | ||
|
||
$response = $mailtrap->sandbox()->projects()->getById($accountId, $projectId); | ||
|
||
// print the response body (array) | ||
var_dump(ResponseHelper::toArray($response)); | ||
} catch (Exception $e) { | ||
echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
} | ||
|
||
/** | ||
* Create project | ||
* The project name is min 2 characters and max 100 characters long. | ||
* | ||
* POST https://mailtrap.io/api/accounts/{account_id}/projects | ||
*/ | ||
try { | ||
$accountId = getenv('MAILTRAP_ACCOUNT_ID'); | ||
$projectName = 'Some project name'; | ||
|
||
$response = $mailtrap->sandbox()->projects()->create($accountId, $projectName); | ||
|
||
// print the response body (array) | ||
var_dump(ResponseHelper::toArray($response)); | ||
} catch (Exception $e) { | ||
echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
} | ||
|
||
|
||
/** | ||
* Update project | ||
* The project name is min 2 characters and max 100 characters long. | ||
* | ||
* PATCH https://mailtrap.io/api/accounts/{account_id}/projects/{project_id} | ||
*/ | ||
try { | ||
$accountId = getenv('MAILTRAP_ACCOUNT_ID'); | ||
$projectId = getenv('MAILTRAP_PROJECT_ID'); | ||
$newProjectName = 'New project name'; | ||
|
||
$response = $mailtrap->sandbox()->projects()->updateName($accountId, $projectId, $newProjectName); | ||
|
||
// print the response body (array) | ||
var_dump(ResponseHelper::toArray($response)); | ||
} catch (Exception $e) { | ||
echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
} | ||
|
||
|
||
/** | ||
* Delete project and its inboxes. | ||
* | ||
* DELETE https://mailtrap.io/api/accounts/{account_id}/projects/{project_id} | ||
*/ | ||
try { | ||
$accountId = getenv('MAILTRAP_ACCOUNT_ID'); | ||
$projectId = getenv('MAILTRAP_PROJECT_ID'); | ||
|
||
$response = $mailtrap->sandbox()->projects()->delete($accountId, $projectId); | ||
|
||
// print the response body (array) | ||
var_dump(ResponseHelper::toArray($response)); | ||
} catch (Exception $e) { | ||
echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Mailtrap\Api\Sandbox; | ||
|
||
use Mailtrap\Api\AbstractEmails; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* Class Project | ||
*/ | ||
class Project extends AbstractEmails implements SandboxInterface | ||
{ | ||
/** | ||
* List projects and their inboxes to which the API token has access. | ||
* | ||
* @param int $accountId | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public function getList(int $accountId): ResponseInterface | ||
{ | ||
return $this->handleResponse($this->httpGet( | ||
sprintf('%s/api/accounts/%s/projects', $this->getHost(), $accountId) | ||
)); | ||
} | ||
|
||
/** | ||
* Get the project and its inboxes. | ||
* | ||
* @param int $accountId | ||
* @param int $projectId | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public function getById(int $accountId, int $projectId): ResponseInterface | ||
{ | ||
return $this->handleResponse($this->httpGet( | ||
sprintf('%s/api/accounts/%s/projects/%s', $this->getHost(), $accountId, $projectId) | ||
)); | ||
} | ||
|
||
/** | ||
* Create project | ||
* | ||
* @param int $accountId | ||
* @param string $projectName | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public function create(int $accountId, string $projectName): ResponseInterface | ||
{ | ||
return $this->handleResponse($this->httpPost( | ||
sprintf('%s/api/accounts/%s/projects', $this->getHost(), $accountId), | ||
[], | ||
['project' => ['name' => $projectName]] | ||
)); | ||
} | ||
|
||
/** | ||
* Delete project and its inboxes. | ||
* | ||
* @param int $accountId | ||
* @param int $projectId | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public function delete(int $accountId, int $projectId): ResponseInterface | ||
{ | ||
return $this->handleResponse($this->httpDelete( | ||
sprintf('%s/api/accounts/%s/projects/%s', $this->getHost(), $accountId, $projectId) | ||
)); | ||
} | ||
|
||
/** | ||
* Update project name. | ||
* | ||
* @param int $accountId | ||
* @param int $projectId | ||
* @param string $projectName | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public function updateName(int $accountId, int $projectId, string $projectName): ResponseInterface | ||
{ | ||
return $this->handleResponse($this->httpPatch( | ||
sprintf('%s/api/accounts/%s/projects/%s', $this->getHost(), $accountId, $projectId), | ||
[], | ||
['project' => ['name' => $projectName]] | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Mailtrap\Exception; | ||
|
||
/** | ||
* Class InvalidTypeException | ||
*/ | ||
class InvalidTypeException extends RuntimeException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.