Citycom API

This commit is contained in:
Frank Schubert
2025-07-29 13:34:13 +02:00
parent d1696c663e
commit fdf1291d17
8 changed files with 338 additions and 14 deletions

View File

@@ -1,6 +1,6 @@
<?php
class Citycom_OanApi {
class Citycom_OanApiClient {
private $baseurl;
private $username;
private $password;
@@ -34,7 +34,7 @@ class Citycom_OanApi {
]
];
$locations = $this->sendGetRequest($url, $ctx_options);
$locations = $this->runApiRequest($url, $ctx_options);
return $locations;
}
@@ -60,7 +60,7 @@ class Citycom_OanApi {
]
];
$homes = $this->sendGetRequest($url, $ctx_options);
$homes = $this->runApiRequest($url, $ctx_options);
return $homes;
}
@@ -74,7 +74,23 @@ class Citycom_OanApi {
}
$url = $this->baseurl.CITYCOM_OAN_API_EP_GET_PRODUCTS;
echo $url;
echo "$url\n";
$url = $this->baseurl.CITYCOM_OAN_API_EP_GET_PRODUCTS;
$ctx_options = [
"http" => [
"ignore_errors" => true,
"method" => "GET",
"header" => [
"Accept: application/json",
"Authorization: Bearer ".$this->token,
],
]
];
$products = $this->runApiRequest($url, $ctx_options);
return $products;
}
@@ -86,9 +102,29 @@ class Citycom_OanApi {
}
}
if(!array_key_exists("name", $data) || !array_key_exists("bb_down", $data) || !array_key_exists("bb_up", $data)) {
return false;
}
$url = $this->baseurl.CITYCOM_OAN_API_EP_CREATE_PRODUCT;
echo "$url\n";
$ctx_options = [
"http" => [
"ignore_errors" => true,
"method" => "POST",
"content" => json_encode($data),
"header" => [
"Accept: application/json",
"Content-Type: application/json",
"Authorization: Bearer ".$this->token,
],
]
];
$new_product = $this->runApiRequest($url, $ctx_options);
return $new_product;
}
public function updateProduct($product_id, $data) {
@@ -129,6 +165,20 @@ class Citycom_OanApi {
$url = $this->baseurl.CITYCOM_OAN_API_EP_GET_SERVICES;
echo "$url\n";
$ctx_options = [
"http" => [
"ignore_errors" => true,
"method" => "GET",
"header" => [
"Accept: application/json",
"Authorization: Bearer ".$this->token,
],
]
];
$services = $this->runApiRequest($url, $ctx_options);
return $services;
}
public function createService($home_id, $data) {
@@ -142,6 +192,20 @@ class Citycom_OanApi {
$url = $this->baseurl.CITYCOM_OAN_API_EP_CREATE_SERVICES;
echo "$url\n";
$ctx_options = [
"http" => [
"ignore_errors" => true,
"method" => "POST",
"header" => [
"Accept: application/json",
"Content-type: application/json",
"Authorization: Bearer ".$this->token,
],
]
];
$new_service = $this->runApiRequest($url, $ctx_options);
return $new_service;
}
public function updateService($service_id, $data) {
@@ -248,7 +312,7 @@ class Citycom_OanApi {
return true;
}
private function sendGetRequest($url, $ctx_opts, $url_params = [], $page_num = 1) {
private function runApiRequest($url, $ctx_opts, $url_params = [], $page_num = 1) {
$current_page = $page_num;
$return_data = [];
@@ -273,7 +337,7 @@ class Citycom_OanApi {
$resp = json_decode($output);
//var_dump($resp);
if(!is_object($resp) || !property_exists($resp, "success") || !$resp->success) {
if(!is_object($resp) || (property_exists($resp, "success") && !$resp->success)) {
return false;
}
@@ -287,7 +351,7 @@ class Citycom_OanApi {
if(property_exists($response, "last_page") && $response->last_page > 1) {
if($current_page < $response->last_page) {
$next_page = $current_page + 1;
$next_data = $this->sendGetRequest($url, $ctx_opts, $url_params, $next_page);
$next_data = $this->runApiRequest($url, $ctx_opts, $url_params, $next_page);
if(!$next_data) {
return $return_data;
}
@@ -302,6 +366,4 @@ class Citycom_OanApi {
}
}

View File

@@ -0,0 +1,122 @@
<?php
class Citycom_OanApiHelper {
private $log;
private $api;
public function __construct(Citycom_OanApiClient $apiClient) {
$this->api = $apiClient;
}
public function findOrCreateProduct($specs = []) {
if(!array_key_exists("up", $specs) || !array_key_exists("down", $specs) || !is_numeric($specs["up"]) || !is_numeric($specs["down"])) {
return false;
}
$up = $specs["up"];
$down = $specs["down"];
$search_name = false;
if(array_key_exists("name", $specs) && $specs["name"]) {
$search_name = $specs["name"];
}
$products = $this->api->getProducts();
foreach($products as $product) {
if($up == $product->bb_up && $down == $product->bb_down) {
if($search_name) {
if($product->name == $search_name) {
return $product->id;
}
} else {
return $product->id;
}
}
}
// not found, so create new product
$name = "xinon_tt_{$down}_{$up}";
if(array_key_exists("name", $specs)) {
$name = $specs["name"];
}
$product_id = $this->api->createProduct([
"name" => $name,
"bb_down" => $down,
"bb_up" => $up
]);
if(!$product_id) {
return false;
}
return $product_id;
}
public function orderServices($preorder, $sublocation_id, $data) {
if(!is_numeric($sublocation_id) || !$sublocation_id) {
return false;
}
if(!array_key_exists("up", $data) || !array_key_exists("down", $data) || !is_numeric($data["up"]) || !is_numeric($data["down"])) {
return false;
}
if(!array_key_exists("services", $data) || is_array($data["services"]) || !count($data["services"])) {
return false;
}
$up = $data["up"];
$down = $data["down"];
$product_name = $data["product_name"];
$execution_date = $data["execution_date"];
$services = $data["services"];
if(!$execution_date) {
$execution_date = date("Y-m-d");
}
$product_data = [
"bb_up" => $up,
"bb_down" => $down,
"name" => $product_name
];
// find or craete product
$product_id = $this->findOrCreateProduct($product_data);
if(!$product_id) {
$this->log->debug(__METHOD__.": no citycom product for query ".print_r($product_data, true));
return false;
}
// order all services and save ctags
$cc_service_types = $this->api->getServices();
$new_services = [];
foreach($cc_service_types as $stype) {
$service_data = [
"service_type" => $stype->id,
"product" => $product_id,
"billing_date" => $execution_date,
"ctag"
];
$new_service = $this->api->createService($sublocation_id, $service_data);
}
return false;
}
public static function citycomIdToHausnummerExtref($id) {
if(!$id) return false;
return "citycom-$id";
}
public static function hausnummerExtrefToCitycomId($extref) {
if(!$extref) return false;
return str_replace("citycom-", "", $extref);
}
}