Skip to content

Commit

Permalink
updating Base controller and Other Endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
ivikramsahu committed Aug 26, 2020
1 parent 4674439 commit 36a9b84
Show file tree
Hide file tree
Showing 15 changed files with 1,707 additions and 41 deletions.
84 changes: 43 additions & 41 deletions src/Controllers/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,52 +16,54 @@
use Unirest\Request;

/**
* Base controller
*/
* Base controller
*/
class BaseController
{
/**
* User-agent to be sent with API calls
* @var string
*/
const USER_AGENT = 'pepi-php-sdk v5';
/**
* User-agent to be sent with API calls
* @var string
*/
const USER_AGENT = 'APIMATIC 2.0';

/**
* HttpCallBack instance associated with this controller
* @var HttpCallBack
*/
private $httpCallBack = null;
/**
* HttpCallBack instance associated with this controller
* @var HttpCallBack
*/
private $httpCallBack = null;

/**
* Set HttpCallBack for this controller
* @param HttpCallBack $httpCallBack Http Callbacks called before/after each API call
*/
public function setHttpCallBack(HttpCallBack $httpCallBack)
{
$this->httpCallBack = $httpCallBack;
}
/**
* Set HttpCallBack for this controller
* @param HttpCallBack $httpCallBack Http Callbacks called before/after each API call
*/
public function setHttpCallBack(HttpCallBack $httpCallBack)
{
$this->httpCallBack = $httpCallBack;
}

/**
* Get HttpCallBack for this controller
* @return HttpCallBack The HttpCallBack object set for this controller
*/
public function getHttpCallBack()
{
return $this->httpCallBack;
}
/**
* Get HttpCallBack for this controller
* @return HttpCallBack The HttpCallBack object set for this controller
*/
public function getHttpCallBack()
{
return $this->httpCallBack;
}

/**
* Get a new JsonMapper instance for mapping objects
* @return \apimatic\jsonmapper\JsonMapper JsonMapper instance
*/
protected function getJsonMapper()
{
$mapper = new JsonMapper();
return $mapper;
}
/**
* Get a new JsonMapper instance for mapping objects
* @return \apimatic\jsonmapper\JsonMapper JsonMapper instance
*/
protected function getJsonMapper()
{
$mapper = new JsonMapper();
return $mapper;
}

protected function validateResponse(HttpResponse $response, HttpContext $_httpContext)
{
return $_httpContext;
}
protected function validateResponse(HttpResponse $response, HttpContext $_httpContext)
{
if (($response->getStatusCode() < 200) || ($response->getStatusCode() > 208)) { //[200,208] = HTTP OK
throw new APIException('HTTP Response Not OK', $_httpContext);
}
}
}
111 changes: 111 additions & 0 deletions src/Controllers/DomainController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
/*
* PepipostLib
*
* This file was automatically generated by APIMATIC v2.0 ( https://apimatic.io ).
*/

namespace PepipostLib\Controllers;

use PepipostLib\APIException;
use PepipostLib\APIHelper;
use PepipostLib\Configuration;
use PepipostLib\Models;
use PepipostLib\Exceptions;
use PepipostLib\Http\HttpRequest;
use PepipostLib\Http\HttpResponse;
use PepipostLib\Http\HttpMethod;
use PepipostLib\Http\HttpContext;
use Unirest\Request;

/**
* @todo Add a general description for this controller.
*/
class DomainController extends BaseController
{
/**
* @var DomainController The reference to *Singleton* instance of this class
*/
private static $instance;

/**
* Returns the *Singleton* instance of this class.
* @return DomainController The *Singleton* instance.
*/
public static function getInstance()
{
if (null === static::$instance) {
static::$instance = new static();
}

return static::$instance;
}

/**
* This endpoint enables you to add a sending domain which is one of the pre-requisites for sending
* emails.
*
* @param Models\DomainStruct $body Add new domain
* @return object response from the API call
* @throws APIException Thrown if API call fails
*/
public function adddomain(
$body
) {

//prepare query string for API call
$_queryBuilder = '/domain';

//validate and preprocess url
$_queryUrl = APIHelper::cleanUrl(Configuration::$BASEURI . $_queryBuilder);

//prepare headers
$_headers = array (
'user-agent' => BaseController::USER_AGENT,
'content-type' => 'application/json; charset=utf-8',
'api_key' => Configuration::$apiKey
);

//json encode body
$_bodyJson = Request\Body::Json($body);

//call on-before Http callback
$_httpRequest = new HttpRequest(HttpMethod::POST, $_headers, $_queryUrl);
if ($this->getHttpCallBack() != null) {
$this->getHttpCallBack()->callOnBeforeRequest($_httpRequest);
}

//and invoke the API call request to fetch the response
$response = Request::post($_queryUrl, $_headers, $_bodyJson);

$_httpResponse = new HttpResponse($response->code, $response->headers, $response->raw_body);
$_httpContext = new HttpContext($_httpRequest, $_httpResponse);

//call on-after Http callback
if ($this->getHttpCallBack() != null) {
$this->getHttpCallBack()->callOnAfterRequest($_httpContext);
}

//Error handling using HTTP status codes
if ($response->code == 400) {
throw new APIException('API Response', $_httpContext);
}

if ($response->code == 401) {
throw new APIException('API Response', $_httpContext);
}

if ($response->code == 403) {
throw new APIException('API Response', $_httpContext);
}

if ($response->code == 405) {
throw new APIException('Invalid input', $_httpContext);
}

//handle errors defined at the API level
$this->validateResponse($_httpResponse, $_httpContext);

return $response->body;
}
}
110 changes: 110 additions & 0 deletions src/Controllers/DomainDeleteController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
/*
* PepipostLib
*
* This file was automatically generated by APIMATIC v2.0 ( https://apimatic.io ).
*/

namespace PepipostLib\Controllers;

use PepipostLib\APIException;
use PepipostLib\APIHelper;
use PepipostLib\Configuration;
use PepipostLib\Models;
use PepipostLib\Exceptions;
use PepipostLib\Http\HttpRequest;
use PepipostLib\Http\HttpResponse;
use PepipostLib\Http\HttpMethod;
use PepipostLib\Http\HttpContext;
use Unirest\Request;

/**
* @todo Add a general description for this controller.
*/
class DomainDeleteController extends BaseController
{
/**
* @var DomainDeleteController The reference to *Singleton* instance of this class
*/
private static $instance;

/**
* Returns the *Singleton* instance of this class.
* @return DomainDeleteController The *Singleton* instance.
*/
public static function getInstance()
{
if (null === static::$instance) {
static::$instance = new static();
}

return static::$instance;
}

/**
* This endpoint allows you to delete a domain from your Pepipost account.
*
* @param Models\Deletedomain $body delete domain
* @return object response from the API call
* @throws APIException Thrown if API call fails
*/
public function deletedomain(
$body
) {

//prepare query string for API call
$_queryBuilder = '/domain/delete';

//validate and preprocess url
$_queryUrl = APIHelper::cleanUrl(Configuration::$BASEURI . $_queryBuilder);

//prepare headers
$_headers = array (
'user-agent' => BaseController::USER_AGENT,
'content-type' => 'application/json; charset=utf-8',
'api_key' => Configuration::$apiKey
);

//json encode body
$_bodyJson = Request\Body::Json($body);

//call on-before Http callback
$_httpRequest = new HttpRequest(HttpMethod::DELETE, $_headers, $_queryUrl);
if ($this->getHttpCallBack() != null) {
$this->getHttpCallBack()->callOnBeforeRequest($_httpRequest);
}

//and invoke the API call request to fetch the response
$response = Request::delete($_queryUrl, $_headers, $_bodyJson);

$_httpResponse = new HttpResponse($response->code, $response->headers, $response->raw_body);
$_httpContext = new HttpContext($_httpRequest, $_httpResponse);

//call on-after Http callback
if ($this->getHttpCallBack() != null) {
$this->getHttpCallBack()->callOnAfterRequest($_httpContext);
}

//Error handling using HTTP status codes
if ($response->code == 400) {
throw new APIException('API Response', $_httpContext);
}

if ($response->code == 401) {
throw new APIException('API Response', $_httpContext);
}

if ($response->code == 403) {
throw new APIException('API Response', $_httpContext);
}

if ($response->code == 405) {
throw new APIException('Invalid input', $_httpContext);
}

//handle errors defined at the API level
$this->validateResponse($_httpResponse, $_httpContext);

return $response->body;
}
}
Loading

0 comments on commit 36a9b84

Please sign in to comment.