Skip to content

Commit

Permalink
adding slack notification for error
Browse files Browse the repository at this point in the history
  • Loading branch information
sleiman committed Jan 12, 2019
1 parent 5550828 commit 1c8e5a6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/Airtable.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ class Airtable
private $_key;

private $_base;

private $_slack;

public function __construct($config)
{
if (is_array($config)) {
$this->setKey($config['api_key']);
$this->setBase($config['base']);
$this->setSlack($config['slack_webhook']);
} else {
echo 'Error: __construct() - Configuration data is missing.';
}
Expand All @@ -49,6 +52,16 @@ public function getBase()
return $this->_base;
}

public function setSlack($slack)
{
$this->_slack = $slack;
}

public function getSlack()
{
return $this->_slack;
}

public function getApiUrl($request){
$request = str_replace( ' ', '%20', $request );
$url = self::API_URL.$this->getBase().'/'.$request;
Expand Down
42 changes: 41 additions & 1 deletion src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,48 @@ public function getResponse()

$response_string = curl_exec( $this->curl );

return new Response( $this->airtable, $this, $response_string, $this->relations );
$response = new Response( $this->airtable, $this, $response_string, $this->relations );

if ($response['error'] && $this->airtable->getSlack()){

$data['base'] = $this->airtable->getBase();
$data['table'] = $this->content_type;
$data['error'] = $response['error'];
$data = json_encode($data);
$this->postSlack($data);

}

return $response;

}


public function postSlack($text) {

$url = $this->airtable->getSlack();

$text = "```".$text."```";

$data_string = array(
"text" => $text,
);

$data_string = json_encode($data_string);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
curl_close($ch);



}

public function __set( $key, $value )
Expand Down

0 comments on commit 1c8e5a6

Please sign in to comment.