def check(self, service_path): """ Check if subscription exists or not @param service_path : service path to check the notification from @return True if subscription already exists. Return False otherwise """ url = FcbgConfig.FIWARE_SUBSCRIPTION_URL headers = { "fiware-service": FcbgConfig.FIWARE_SUBSCRIPTION_SERVICE, "fiware-servicepath": service_path, "Content-type" : "application/json" } try: client = FcbgHttpClient(headers, url, {}, "") result = client.get() if result.status_code not in [200, 201]: raise FcbgException("Unable to get subscriptions from source CB. Response: " + str(result.status_code) + ", Reason: " + result.reason) try: subscriptions = json.loads(result.text) if len(subscriptions) > 0: return True else: return False except: raise FcbgException("Response from CB is not a JSON object:\n" + result.text) except: raise
def create(self, service_path): """ Create subscription on source CB @param service_path : service path to get notifications from """ url = FcbgConfig.FIWARE_SUBSCRIPTION_URL headers = { "fiware-service": FcbgConfig.FIWARE_SUBSCRIPTION_SERVICE, "fiware-servicepath": service_path, "Content-type" : "application/json" } body = { "description": "Full subscription to " + service_path, "subject": { "condition" : {"attrs": []} }, "notification": { "http": {"url": "http://localhost:1812/notify"}, "attrs": [] } } try: client = FcbgHttpClient(headers, url, {}, body) result = client.post() if result.status_code not in [200, 201]: raise FcbgException("Unable to create subscription on source CB. Response: " + str(result.status_code) + ", Reason: " + result.reason) except: raise
def create(self, entity): """ Create entity on target CB @param entity : Entity object """ if "id" in entity and "type" in entity: entity_str = entity["type"] + ":" + entity["id"] # Create entity on target CB url = FcbgConfig.FIWARE_TARGET_URL headers = { "fiware-service": FcbgConfig.FIWARE_TARGET_SERVICE, "fiware-servicepath": FcbgConfig.FIWARE_TARGET_SERVICE_PATH, "Content-type": "application/json", 'X-Auth-Token': self.auth_token } try: client = FcbgHttpClient(headers, url, {}, json.dumps(entity)) result = client.post() if result.status_code in [200, 201, 422]: # Enter entity in memory self.entities.append(entity_str) else: raise FcbgException( "Unable to create entity on target CB. Response: " + str(result.status_code) + ", Reason: " + result.reason) except: raise
def update_auth_token(self): """ Update auth token from target server """ url = FcbgConfig.FIWARE_AUTH_TOKEN_URL payload = "{\"auth\": {\"identity\": {\"methods\": [\"password\"],\"password\": {\"user\": {\"domain\": {\"name\": \"" payload += FcbgConfig.FIWARE_TARGET_SERVICE + "\"},\"name\": \"" + FcbgConfig.FIWARE_TARGET_USER_NAME + "\",\"password\": \"" payload += FcbgConfig.FIWARE_TARGET_USER_PASSWORD + "\"}}},\"scope\": {\"project\": {\"domain\": {\"name\": \"" payload += FcbgConfig.FIWARE_TARGET_SERVICE + "\"},\"name\": \"" + FcbgConfig.FIWARE_TARGET_SERVICE_PATH + "\"}}}}" headers = {'Content-Type': "application/json"} threading.Timer(FcbgConfig.FIWARE_AUTH_TOKEN_INTERVAL, self.update_auth_token).start() try: client = FcbgHttpClient(headers, url, {}, payload) result = client.post() if result.status_code in [200, 201]: if "X-Subject-Token" in result.headers: self.auth_token = result.headers["X-Subject-Token"] else: raise FcbgException( "Unable to retrieve auth token from target server") except: raise
def patch(self): """ Run HTTP patch request """ try: return requests.patch(self.url, headers=self.header, params=self.parameters, data=self.payload, verify=False) except Exception as ex: raise FcbgException(str(ex))
def update(self, entity): """ Update entity on target CB @param entity : Entity object """ if "id" in entity and "type" in entity: entity_str = entity["type"] + ":" + entity["id"] # Entity in memory? if entity_str in self.entities: # OK, update entity on target CB url = FcbgConfig.FIWARE_TARGET_URL + "/" + entity[ "id"] + "/attrs" headers = { "fiware-service": FcbgConfig.FIWARE_TARGET_SERVICE, "fiware-servicepath": FcbgConfig.FIWARE_TARGET_SERVICE_PATH, "Content-type": "application/json", 'X-Auth-Token': self.auth_token } del entity["type"] del entity["id"] for field in entity: if "metadata" in entity[field]: del entity[field]["metadata"] try: client = FcbgHttpClient(headers, url, {}, json.dumps(entity)) result = client.patch() if result.status_code not in [200, 201, 204]: raise FcbgException( "Unable to update entity on target CB. Response: " + str(result.status_code) + ", Reason: " + result.reason) except: raise else: # Entity not in memory # Let's try to create it on the target CB self.create(entity)