api/
ved/
admin/
modules/
gyar/
admin/
cron/
api/gyar/log.php1 lines
<?php
# latest.php
# API endpoint
# Fil för att hämta den senaste väderdatan från en viss mätpunkt.
require_once "gyar/modules/utility.php";
# Hämtar den senaste tillgängliga väderdatan för dagen.
$functionPOST = function () {
$headers = getallheaders();
if (!isset($headers["Authorization"]))
throw new APIError(401, "Invalid authorization.");
$authData = explode(" ", $headers["Authorization"]);
if (sizeof($authData) != 2)
throw new APIError(401, "Invalid authorization.");
$authType = $authData[0];
$authToken = $authData[1];
$hashed = hash("sha256", $authToken);
require "gyar/modules/gyarDatabase.php";
$station = $db->query("SELECT stationId FROM weatherStation WHERE apiKeyHash=:apiKeyHash", array("apiKeyHash" => $hashed));
if (!$station)
throw new APIError(401, "Invalid authorization.");
$pointInstance = $db->query("SELECT stationPointInstanceId FROM stationPointInstance WHERE stationId=:stationId", $station);
if (!$pointInstance)
throw new APIError(403, "Station has not been assigned a point.");
$db->pdo->beginTransaction();
try {
$created = $db->query("INSERT INTO stationPointTimeData(stationPointInstanceId, timestamp) VALUES (:stationPointInstanceId, CURRENT_TIMESTAMP())", $pointInstance);
} catch (Exception $ex) {
# Om någonting misslyckas backas allting tillbaka..
$db->pdo->rollBack();
throw $ex;
}
$db->pdo->commit();
http_response_code(201);
return $created;
};
$api = new APIEndpoint();
$auth = (require "gyar/auth/authorization.php")(AuthLevel::STATION);
$httpPOST = new APIMethod("POST", $functionPOST);
$httpPOST->setAuthorization($auth);
$httpPOST->setRequiredBodyParameters(array("temperature"));
$api->addMethod($httpPOST);
return $api;
?>