Commit c09a9acf authored by Mark Tearle's avatar Mark Tearle

Initial working version of update_desec_txt() using Guzzle directly

parent 34f9db9e
This diff is collapsed.
......@@ -7,6 +7,7 @@ error_reporting(E_ALL);
require_once('config.php');
use Ramsey\Uuid\Uuid;
use RestService\RestService;
use GuzzleHttp\Client;
function check_username($username) {
// returns true if we get some info about the username
......@@ -69,23 +70,49 @@ function check_challenge($challenge) {
// EOF
//
function update_desec_txt($name, $txt) {
function update_desec_txt($config, $name, $txt) {
$path = "/api/v1/domains/" . $config['QV_DOMAIN'] . "/rrsets/" . $name . "/TXT/";
$auth_string = "Token ". $config['QV_DESEC_API'];
$headers = [
'Authorization' => $auth_string,
'Content-Type' => "application/json"
$restService = new RestService();
];
// $response = $restService
// ->setEndpoint('https://jsonplaceholder.typicode.com')
// ->get('/posts/1');
// $restService
// ->setEndpoint('https://jsonplaceholder.typicode.com')
// ->put('/posts/1',
// [
// 'id' => 1,
// 'text' => 'Test'
// ]
// );
$val = [
'subname' => $name,
'type' => 'TXT',
'ttl' => 3600,
'records' => ['"'. $txt . '"',],
];
$body = json_encode($val);
$client = new GuzzleHttp\Client([
'base_uri' => 'https://desec.io',
'timeout' => 2,
'debug' => true
]);
try {
$response = $client->request('PUT', $path, [ 'headers'=>$headers, 'body'=>$body ]);
} catch (ClientException $e) {
throw new Exception($e->getResponse()->getReasonPhrase(), $e->getResponse()->getStatusCode());
} catch (BadResponseException $e) {
throw new Exception($e->getResponse()->getReasonPhrase(), $e->getResponse()->getStatusCode());
} catch (Exception $e) {
throw new Exception($e->getMessage(), $e->getCode());
}
$result = json_decode($response->getBody(), true);
if (array_key_exists('records', $result)) {
return stripQuotes($result['records'][0]);
} else {
return "";
}
}
// Instantiate Leaf
......@@ -94,9 +121,10 @@ $leaf = new Leaf\App;
// Add routes
$leaf->get('/', function () use($leaf) {
$config=get_config();
$s = update_desec_txt($config, "mtearle-test", "test flight");
$r = get_desec_txt($config, "mtearle-test");
// since the response object is directly tied to the leaf instance
$html = '<h5>My first Leaf app</h5>' . $r;
$html = '<h5>My first Leaf app</h5>' . $r . " --- ". $s;
$leaf->response()->markup($html);
});
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment