diff --git a/catalyst/api-bundle/Command/TestCommand.php b/catalyst/api-bundle/Command/TestCommand.php index 18ea68f4..ae99f6cd 100644 --- a/catalyst/api-bundle/Command/TestCommand.php +++ b/catalyst/api-bundle/Command/TestCommand.php @@ -32,9 +32,20 @@ class TestCommand extends Command $api_key = $input->getArgument('api_key'); $secret_key = $input->getArgument('secret_key'); + // api client $api = new APIClient($server, $api_key, $secret_key); $api->setProtocol($protocol); + // test $api->get('/capi/test'); + + // warranty register + $params = [ + ]; + $api->post('/capi/warranty', $params); + + // warranty find + $api->get('/capi/warranty/LJ34LJADR12SDLKJL'); + } } diff --git a/catalyst/api-bundle/Connector/Client.php b/catalyst/api-bundle/Connector/Client.php index 259d72d8..904b7cad 100644 --- a/catalyst/api-bundle/Connector/Client.php +++ b/catalyst/api-bundle/Connector/Client.php @@ -47,23 +47,35 @@ class Client return $this; } - public function get($url, $params = []) + protected function getDateString() { $date = new DateTime(); - $date_string = $date->format(self::DATE_FORMAT); + return $date->format(self::DATE_FORMAT); + } + public function get($url, $params = []) + { + curl_reset($this->curl); + + $date_string = $this->getDateString(); $headers = $this->generateHeaders('GET', $url, $date_string); + // build query string + if (count($params) > 0) + $query_string = '?' . http_build_query($params); + else + $query_string = ''; + // build url if ($this->port == null) - $full_url = $this->protocol . '://' . $this->server . $url; + $full_url = $this->protocol . '://' . $this->server . $url . $query_string; else - $full_url = $this->protocol . '://' . $this->server . ':' . $this->port . $url; + $full_url = $this->protocol . '://' . $this->server . ':' . $this->port . $url . $query_string; error_log($full_url); // curl - curl_setopt($this->curl, CURLOPT_VERBOSE, true); + // curl_setopt($this->curl, CURLOPT_VERBOSE, true); curl_setopt($this->curl, CURLOPT_URL, $full_url); curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($this->curl, CURLOPT_USERAGENT, self::USER_AGENT); @@ -74,8 +86,36 @@ class Client error_log($res); } - public function post($params) + public function post($url, $params = []) { + curl_reset($this->curl); + + $date_string = $this->getDateString(); + $headers = $this->generateHeaders('POST', $url, $date_string); + + // build query string + $query_string = http_build_query($params); + + // build url + if ($this->port == null) + $full_url = $this->protocol . '://' . $this->server . $url; + else + $full_url = $this->protocol . '://' . $this->server . ':' . $this->port . $url; + + error_log($full_url); + + // curl + // curl_setopt($this->curl, CURLOPT_VERBOSE, true); + curl_setopt($this->curl, CURLOPT_URL, $full_url); + curl_setopt($this->curl, CURLOPT_POST, true); + curl_setopt($this->curl, CURLOPT_POSTFIELDS, $query_string); + curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers); + curl_setopt($this->curl, CURLOPT_USERAGENT, self::USER_AGENT); + curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($this->curl, CURLOPT_TIMEOUT, 0); + + $res = curl_exec($this->curl); + error_log($res); } protected function generateSignature($method, $url, $date_string) @@ -88,6 +128,8 @@ class Client ]; $sig_source = implode('|', $creds); + error_log('SIG SOURCE - ' . $sig_source); + $raw_sig = hash_hmac('sha1', $sig_source, $this->secret_key, true); $enc_sig = base64_encode($raw_sig); diff --git a/catalyst/api-bundle/Security/APIKeyAuthenticator.php b/catalyst/api-bundle/Security/APIKeyAuthenticator.php index bb3e4c14..f40728df 100644 --- a/catalyst/api-bundle/Security/APIKeyAuthenticator.php +++ b/catalyst/api-bundle/Security/APIKeyAuthenticator.php @@ -93,7 +93,7 @@ class APIKeyAuthenticator implements SimplePreAuthenticatorInterface, Authentica 'date' => $hdate_string, 'signature' => $sig, 'method' => $req->getRealMethod(), - 'uri' => $req->getRequestUri(), + 'uri' => $req->getPathInfo(), ]; return new PreAuthenticatedToken( diff --git a/src/Controller/APITestController.php b/src/Controller/APITestController.php deleted file mode 100644 index 2f368ca3..00000000 --- a/src/Controller/APITestController.php +++ /dev/null @@ -1,28 +0,0 @@ - true, - ]; - return new JsonResponse($data); - } -} diff --git a/src/Controller/CAPI/BatteryController.php b/src/Controller/CAPI/BatteryController.php new file mode 100644 index 00000000..af2361e2 --- /dev/null +++ b/src/Controller/CAPI/BatteryController.php @@ -0,0 +1,38 @@ + true, + ]; + return new JsonResponse($data); + } +} diff --git a/src/Controller/CAPI/TestController.php b/src/Controller/CAPI/TestController.php new file mode 100644 index 00000000..0143166a --- /dev/null +++ b/src/Controller/CAPI/TestController.php @@ -0,0 +1,20 @@ + 'Test successful.', + ]; + return new APIResponse(true, 'Test successful.', $data); + } +} diff --git a/src/Controller/CAPI/WarrantyController.php b/src/Controller/CAPI/WarrantyController.php new file mode 100644 index 00000000..a17b7701 --- /dev/null +++ b/src/Controller/CAPI/WarrantyController.php @@ -0,0 +1,85 @@ +cleanSerial($serial); + $warr = $em->getRepository(Warranty::class)->findOneBy(['serial' => $clean_serial]); + + if ($warr == null) + return new APIResponse(false, 'No warranty found with that serial number.', null, 404); + + + $data = [ + 'warranty' => [ + 'id' => (int) $warr->getID(), + 'serial' => (string) $warr->getSerial(), + 'date_create' => (string) $warr->getDateCreate()->format('YmdHis'), + 'date_expire' => (string) $warr->getDateExpire()->format('Ymd'), + ], + ]; + + return new APIResponse(true, 'Warranty found.', $data); + } + + public function register(Request $req, EntityManagerInterface $em) + { + // required parameters + $serial = $req->request->get('serial'); + $date_expire_string = $req->request->get('date_expire'); + + // missing serial + if ($serial == null) + return new APIResponse(false, 'Required parameter missing: serial.'); + + // missing date expire + if ($date_expire_string == null) + return new APIResponse(false, 'Required parameter missing: date_expire.'); + + // wrong date format + $date_expire = DateTime::createFromFormat('Ymd', $date_expire_string); + if ($date_expire === false) + return new APIResponse(false, 'Wrong date format: date_expire.'); + + // warranty + $warr = new Warranty(); + $warr->setSerial($serial) + ->setDateExpire($date_expire); + + $em->persist($warr); + $em->flush(); + + // data + $data = [ + 'warranty' => [ + 'id' => (int) $warr->getID(), + 'serial' => (string) $warr->getSerial(), + 'date_create' => (string) $warr->getDateCreate(), + 'date_expire' => (string) $warr->getDateExpire(), + ], + ]; + + return new APIResponse(true, 'Warranty registered.', $data); + } + + public function claim($serial) + { + } +} diff --git a/src/Entity/Warranty.php b/src/Entity/Warranty.php new file mode 100644 index 00000000..b2340a08 --- /dev/null +++ b/src/Entity/Warranty.php @@ -0,0 +1,81 @@ +date_create = new DateTime(); + } + + public function getID() + { + return $this->id; + } + + public function setSerial($serial) + { + $this->serial = $serial; + return $this; + } + + public function getSerial() + { + return $this->serial; + } + + public function getDateCreate() + { + return $this->date_create; + } + + public function setDateExpire(DateTime $date) + { + $this->date_expire = $date; + return $this; + } + + public function getDateExpire() + { + return $this->date_expire; + } +}