Citycom API
This commit is contained in:
369
lib/Citycom/OanApiClient.php
Normal file
369
lib/Citycom/OanApiClient.php
Normal file
@@ -0,0 +1,369 @@
|
||||
<?php
|
||||
|
||||
class Citycom_OanApiClient {
|
||||
private $baseurl;
|
||||
private $username;
|
||||
private $password;
|
||||
private $token;
|
||||
|
||||
|
||||
public function __construct($user, $pass) {
|
||||
$this->baseurl = CITYCOM_OAN_API_URL;
|
||||
$this->username = $user;
|
||||
$this->password = $pass;
|
||||
}
|
||||
|
||||
public function getLocations() {
|
||||
if(!$this->token) {
|
||||
$this->getAuthToken();
|
||||
if(!$this->token) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$url = $this->baseurl.CITYCOM_OAN_API_EP_GET_LOCATIONS;
|
||||
|
||||
$ctx_options = [
|
||||
"http" => [
|
||||
"ignore_errors" => true,
|
||||
"method" => "GET",
|
||||
"header" => [
|
||||
"Accept: application/json",
|
||||
"Authorization: Bearer ".$this->token,
|
||||
],
|
||||
]
|
||||
];
|
||||
|
||||
$locations = $this->runApiRequest($url, $ctx_options);
|
||||
return $locations;
|
||||
}
|
||||
|
||||
public function getHomes($location_id) {
|
||||
if(!$this->token) {
|
||||
$this->getAuthToken();
|
||||
if(!$this->token) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$url = $this->baseurl.CITYCOM_OAN_API_EP_GET_HOMES;
|
||||
$url = str_replace("{location_id}", $location_id, $url);
|
||||
|
||||
$ctx_options = [
|
||||
"http" => [
|
||||
"ignore_errors" => true,
|
||||
"method" => "GET",
|
||||
"header" => [
|
||||
"Accept: application/json",
|
||||
"Authorization: Bearer ".$this->token,
|
||||
],
|
||||
]
|
||||
];
|
||||
|
||||
$homes = $this->runApiRequest($url, $ctx_options);
|
||||
return $homes;
|
||||
}
|
||||
|
||||
|
||||
public function getProducts() {
|
||||
if(!$this->token) {
|
||||
$this->getAuthToken();
|
||||
if(!$this->token) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$url = $this->baseurl.CITYCOM_OAN_API_EP_GET_PRODUCTS;
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
public function createProduct($data) {
|
||||
if(!$this->token) {
|
||||
$this->getAuthToken();
|
||||
if(!$this->token) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
if(!$this->token) {
|
||||
$this->getAuthToken();
|
||||
if(!$this->token) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$url = str_replace("{product_id}", $product_id, $this->baseurl.CITYCOM_OAN_API_EP_UPDATE_PRODUCT);
|
||||
echo $url;
|
||||
|
||||
}
|
||||
|
||||
public function deleteProduct($product_id) {
|
||||
if(!$this->token) {
|
||||
$this->getAuthToken();
|
||||
if(!$this->token) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$url = str_replace("{product_id}", $product_id, $this->baseurl.CITYCOM_OAN_API_EP_DELETE_PRODUCT);
|
||||
echo "$url\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function getServices() {
|
||||
if(!$this->token) {
|
||||
$this->getAuthToken();
|
||||
if(!$this->token) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$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) {
|
||||
if(!$this->token) {
|
||||
$this->getAuthToken();
|
||||
if(!$this->token) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$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) {
|
||||
if(!$this->token) {
|
||||
$this->getAuthToken();
|
||||
if(!$this->token) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$url = str_replace("{service_id}", $service_id, $this->baseurl.CITYCOM_OAN_API_EP_UPDATE_SERVICES);
|
||||
echo $url;
|
||||
|
||||
}
|
||||
|
||||
public function cancelService($service_id, $data) {
|
||||
if(!$this->token) {
|
||||
$this->getAuthToken();
|
||||
if(!$this->token) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$url = str_replace("{service_id}", $service_id, $this->baseurl.CITYCOM_OAN_API_EP_CANCEL_SERVICES);
|
||||
echo "$url\n";
|
||||
|
||||
}
|
||||
|
||||
public function getServiceTypes() {
|
||||
if(!$this->token) {
|
||||
$this->getAuthToken();
|
||||
if(!$this->token) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$url = $this->baseurl.CITYCOM_OAN_API_EP_GET_SERVICE_TYPES;
|
||||
$ctx_options = [
|
||||
"http" => [
|
||||
"ignore_errors" => true,
|
||||
"method" => "GET",
|
||||
"header" => [
|
||||
"Accept: application/json",
|
||||
"Authorization: Bearer ".$this->token,
|
||||
],
|
||||
]
|
||||
];
|
||||
|
||||
$ctx = stream_context_create($ctx_options);
|
||||
$output = file_get_contents($url, false, $ctx);
|
||||
|
||||
$resp = json_decode($output);
|
||||
if(!is_object($resp) || !property_exists($resp, "success") || !$resp->success) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$types = $resp->data;
|
||||
|
||||
var_dump($types);exit;
|
||||
|
||||
return $types;
|
||||
}
|
||||
|
||||
private function getAuthToken() {
|
||||
$token = new mfConfig("adb.import.citycom.auth.token");
|
||||
if($token && $token->value()) {
|
||||
$this->token = $token->value();
|
||||
return true;
|
||||
}
|
||||
|
||||
$url = $this->baseurl.CITYCOM_OAN_API_EP_LOGIN;
|
||||
|
||||
$ctx_options = ["http" => [
|
||||
"ignore_errors" => true,
|
||||
"method" => "POST",
|
||||
"headers" => [
|
||||
"Accept: application/json",
|
||||
"Content-Type: multipart/form-data",
|
||||
],
|
||||
"content" => http_build_query([
|
||||
"email" => $this->username,
|
||||
"password" => $this->password,
|
||||
]),
|
||||
]];
|
||||
|
||||
$ctx = stream_context_create($ctx_options);
|
||||
$output = file_get_contents($url, false, $ctx);
|
||||
|
||||
$resp = json_decode($output);
|
||||
if(!is_object($resp) || !property_exists($resp, "success") || !$resp->success) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!$resp->data->token) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->token = $resp->data->token;
|
||||
|
||||
// save token
|
||||
$token->value($this->token);
|
||||
$token->save();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function runApiRequest($url, $ctx_opts, $url_params = [], $page_num = 1) {
|
||||
$current_page = $page_num;
|
||||
$return_data = [];
|
||||
|
||||
$ctx = stream_context_create($ctx_opts);
|
||||
|
||||
// build final url with url_params and page_num
|
||||
$final_url_params = $url_params;
|
||||
if($page_num > 1) {
|
||||
$final_url_params["page"] = $page_num;
|
||||
}
|
||||
|
||||
if(count($final_url_params)) {
|
||||
$qs = http_build_query($final_url_params);
|
||||
$final_url = "$url?$qs";
|
||||
} else {
|
||||
$final_url = "$url";
|
||||
}
|
||||
|
||||
// run request
|
||||
//echo "URL: $final_url\n";
|
||||
$output = file_get_contents($final_url, false, $ctx);
|
||||
|
||||
$resp = json_decode($output);
|
||||
//var_dump($resp);
|
||||
if(!is_object($resp) || (property_exists($resp, "success") && !$resp->success)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$response = $resp->data;
|
||||
|
||||
if(is_array($response)) {
|
||||
$return_data = $response;
|
||||
} elseif(is_object($response) && property_exists($response, "data") && is_array($response->data)) {
|
||||
$return_data = $response->data;
|
||||
|
||||
if(property_exists($response, "last_page") && $response->last_page > 1) {
|
||||
if($current_page < $response->last_page) {
|
||||
$next_page = $current_page + 1;
|
||||
$next_data = $this->runApiRequest($url, $ctx_opts, $url_params, $next_page);
|
||||
if(!$next_data) {
|
||||
return $return_data;
|
||||
}
|
||||
$return_data = array_merge($return_data, $next_data);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new Exception(__METHOD__.": Unknown API return type");
|
||||
}
|
||||
|
||||
return $return_data;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user