def test_computed_value_in_insert(self): with responses.RequestsMock() as rsps: rsps.add(rsps.GET, 'http://demo.local/odata/$metadata', body=metadata_xml, content_type='text/xml') Service = ODataService('http://demo.local/odata/', reflect_entities=True) Product = Service.entities['Products'] test_product = Product() def request_callback_part(request): payload = json.loads(request.body) self.assertNotIn('ExampleComputed', payload) headers = {} return requests.codes.created, headers, json.dumps(payload) with responses.RequestsMock() as rsps: rsps.add_callback( rsps.POST, Product.__odata_url__(), callback=request_callback_part, content_type='application/json', ) Service.save(test_product)
def get_thresholds(): a = {} url = 'https://iotp2000450966trial.hanatrial.ondemand.com/iot/device.xsodata/' Service = ODataService(url, reflect_entities=True) items = Service.entities['IOT_VSP'] query_items = Service.query(items) query_items = query_items.limit(1000000) for item in query_items: a[item.ID_VSP] = int(item.WATER_MIN) return a
def find_vsp(i): a = {} url = 'https://iotp2000450966trial.hanatrial.ondemand.com/iot/device.xsodata/' Service = ODataService(url, reflect_entities=True) items = Service.entities['IOT_ITEM'] query_items = Service.query(items) query_items = query_items.limit(1000000) query_items = query_items.order_by(items.DATE.asc()) for item in query_items: a[item.ID_VSP] = i return a
def _retrieve_data(self, data_source: ODataDataSource) -> pd.DataFrame: if self.auth: session = self.auth.get_session() else: session = None service = ODataService(self.baseroute, reflect_entities=True, session=session) entities = service.entities[data_source.entity] data = service.query(entities).raw(data_source.query) return pd.DataFrame(data)
def get_devices(): a = {} url = 'https://iotp2000450966trial.hanatrial.ondemand.com/iot/device.xsodata/' Service = ODataService(url, reflect_entities=True) for key in find_vsp(10).keys(): mapping = Service.entities['IOT_MAP'] query_map = Service.query(mapping) query_map = query_map.limit(1000000) query_map = query_map.filter(mapping.ID_VSP == key) for item in query_map: a[item.G_DEVICE] = key return a
def test_read(self): MetaData.flush_cache() with responses.RequestsMock() as rsps: rsps.add(rsps.GET, 'http://demo.local/odata/$metadata/', body=metadata_xml, content_type='text/xml') Service = ODataService('http://demo.local/odata/', reflect_entities=True) self.assertIn('Products', Service.entities) # non-entityset things should not be listed in entities expected_keys = { 'Products', 'ProductsWithNavigation', 'Manufacturers', 'Product_Manufacturer_Sales' } self.assertEqual(set(Service.entities.keys()), expected_keys) Product = Service.entities['Products'] ProductWithNavigation = Service.entities['ProductsWithNavigation'] assert issubclass(Product, EntityBase) assert hasattr(Product, 'DemoCollectionAction') test_product = Product() # shortcut for saving the entity test_product.__odata__.persisted = True assert hasattr(test_product, 'DemoActionWithParameters') assert hasattr(ProductWithNavigation, 'Manufacturer') self.assertIn('Manufacturers', Service.entities) self.assertIn('DemoUnboundAction', Service.actions)
def send_alert(id_vsp, name, warning, n): url = 'https://iotp2000450966trial.hanatrial.ondemand.com/iot/device.xsodata/' Service = ODataService(url, reflect_entities=True) users = Service.entities['IOT_SKYPE'] query = Service.query(users) query = query.limit(1000000) query = query.filter(users.ID_VSP == id_vsp) if warning: for user in query: if user.ID_SKYPE != "*****@*****.**": sk.contacts[user.ID_SKYPE].chat.sendMsg( "Внимание, в {}, заканчивается вода, осталось - {}бут. закажите поставку." .format(name)) print("message send to {} for vsp_id {}".format( user.ID_SKYPE, user.ID_VSP, n)) else: for user in query: if user.ID_SKYPE != "*****@*****.**": sk.contacts[user.ID_SKYPE].chat.sendMsg( "Внимание, в {}, закончилась вода, срочно закажите поставку!" .format(name)) print("message send to {} for vsp_id {}".format( user.ID_SKYPE, user.ID_VSP))
def main(): parsed_args = singer.utils.parse_args(REQUIRED_CONFIG_KEYS) url = 'https://{}.dynamics.com/api/data/v9.0/'.format( parsed_args.config['domain']) service = ODataService(url, reflect_entities=True, auth=DynamicsAuth(parsed_args.config)) if parsed_args.discover: do_discover(service) else: sync(service, parsed_args.catalog, parsed_args.state, parsed_args.config['start_date'])
def main(): args = utils.parse_args(REQUIRED_CONFIG_KEYS) url = "https://api.businesscentral.dynamics.com/v2.0/{}/{}/api/BCSItera/dashboards/v1.0/".format(args.config["tenantId"], args.config["environment"]) service = ODataService( url, reflect_entities=True, auth=requests.auth.HTTPBasicAuth(args.config["username"], args.config["password"]) ) catalog = args.catalog or do_discover(service) if args.discover: catalog = discover(service) catalog.dump() else: sync(service, catalog, args.state, args.config["start_date"],)
def main(): parsed_args = singer.utils.parse_args(REQUIRED_CONFIG_KEYS) url = "https://{}.dynamics.com/api/data/v9.0/".format( parsed_args.config["domain"]) service = ODataService(url, reflect_entities=True, auth=DynamicsAuth(parsed_args.config)) catalog = parsed_args.catalog or do_discover(service) if parsed_args.discover: json.dump(catalog.to_dict(), sys.stdout, indent=2) else: sync( service, catalog, parsed_args.state, parsed_args.config["start_date"], )
# -*- coding: utf-8 -*- from odata import ODataService from odata.property import StringProperty, IntegerProperty, DecimalProperty, \ NavigationProperty, DatetimeProperty url = 'http://unittest.server.local/odata/' Service = ODataService(url) class Product(Service.Base): id = IntegerProperty('ProductID', primary_key=True) name = StringProperty('ProductName') category = StringProperty('Category') price = DecimalProperty('Price') class ProductPart(Service.Base): __odata_type__ = 'ODataTest.Objects.ProductPart' __odata_collection__ = 'ProductParts' id = IntegerProperty('PartID', primary_key=True) name = StringProperty('PartName') size = DecimalProperty('Size') product_id = IntegerProperty('ProductID') class Manufacturer(Service.Base): __odata_type__ = 'ODataTest.Objects.Manufacturer' __odata_collection__ = 'Manufacturers' id = IntegerProperty('ManufacturerID', primary_key=True)
from odata import ODataService # CONNECT TO PYRAMID USE THIS LIBRARY https://github.com/tuomur/python-odata from requests.auth import HTTPBasicAuth # SUPPORT BASIC AUTHENTICATION from sklearn.tree import DecisionTreeClassifier from sklearn import datasets from sklearn import tree import joblib import pandas as pd from urllib.request import urlopen my_auth = HTTPBasicAuth( "USERNAME", "PASSWORD" ) # APPLY CREDENTIALS FOR PRODUCTION GRADE USE, USE TECHNOLOGY TO ENCRYPT PASSWORDS #PYRAMID ODATA FEED URL: url = "https://PYRAMID_SERVER_ODATA_FEED_URL" Service = ODataService(url, reflect_entities=True, auth=my_auth) #DISCOVER REPORT ITEM ID Entity = Service.entities['ITEM_ID'] result = Service.query(Entity) target = [] data = [] observ_result = [] dataDict = {} i = 0 j = 0 for Entity in result: target.append(Entity.CUSTOMER_CLASS) data.append([Entity.GENDER, Entity.FLAG_MOBIL, Entity.OWN_CAR, Entity.OWN_REALTY,\ Entity.FLAG_PHONE, Entity.FLAG_WORK_PHONE, Entity.EDUCATION_TYPE, Entity.FAMILY_STATUS,\ Entity.HOUSING_TYPE, Entity.INCOME_TYPE, Entity.OCCUPATION, Entity.AMT_INCOME_TOTAL,\
def __service(self) -> ODataService: return ODataService(self.__url(), session=self.__session(), reflect_entities=True)
from odata import ODataService #import pdb;pdb.set_trace() url = 'http://services.odata.org/V4/Northwind/Northwind.svc/' Service = ODataService(url, reflect_entities=True) Supplier = Service.entities['Supplier'] query = Service.query(Supplier) query = query.limit(2) query = query.order_by(Supplier.CompanyName.asc()) for supplier in query: print('Company:', supplier.CompanyName) for product in supplier.Products: print('- Product:', product.ProductName)
def main(): thresholds = get_thresholds() date0 = {} weight0 = {} last_qsum = find_vsp(10) checksum = find_vsp(2) for key in last_qsum.keys(): date0[key] = datetime(1970, 1, 1) weight0[key] = 0 while True: url = 'https://iotp2000450966trial.hanatrial.ondemand.com/iot/device.xsodata/' Service = None while Service is None: try: # connect Service = ODataService(url, reflect_entities=True) except: print("database is not found") devices = get_devices() for device in get_devices().keys(): id = devices.get(device) dev_d = Service.entities['IOT_DEVICE_DATA'] query1 = Service.query(dev_d) query1 = query1.filter(dev_d.G_DEVICE == device) query1 = query1.limit(100000) query1 = query1.order_by(dev_d.G_CREATED.desc()) item_d = query1.first() time_string = item_d.G_CREATED print(get_date(time_string), " ? ", date0[id]) if get_date(time_string) > date0[id]: date0[id] = get_date(time_string) if float(item_d.C_WEIGHT) > weight0[id]: items = Service.entities['IOT_ITEM'] mapping = Service.entities['IOT_MAP'] names = Service.entities['IOT_VSP'] query_items = Service.query(items) query_items = query_items.limit(1000000) query_names = Service.query(names) query_names = query_names.limit(1000000) query_map = Service.query(mapping) query_map = query_map.limit(1000000) item = query_items.entity.__new__(cls=query_items.entity) item.ID_VSP = query_map.get(item_d.G_DEVICE).ID_VSP print(item.ID_VSP) query_items = query_items.order_by(items.DATE.asc()) for item1 in query_items: last_qsum[item1.ID_VSP] = int(item1.QSUM) if last_qsum[item.ID_VSP] > thresholds[item.ID_VSP]: checksum[item.ID_VSP] = 0 if last_qsum[item.ID_VSP] > 0: item.QUANTITY = -1 item.DATE = str(time_string) item.ID_MATERIAL = 1 item.DEB_KRE = 'H' url = "https://iotp2000450966trial.hanatrial.ondemand.com/iot/device.xsodata/IOT_ITEM" payload = ({ "ID_VSP": str(item.ID_VSP), "ID_MATERIAL": str(item.ID_MATERIAL), "DATE": str(item.DATE), "DEB_KRE": str(item.DEB_KRE), "QUANTITY": str(item.QUANTITY), "QSUM": str(-1) }) print("send") requests.post(url, json=payload) if thresholds[item.ID_VSP] > last_qsum[item.ID_VSP] > 0: if checksum[item.ID_VSP] == 0: name = query_names.get(item.ID_VSP).NAME_VSP send_alert(item.ID_VSP, name, True, last_qsum[item.ID_VSP]) checksum[item.ID_VSP] = 1 elif last_qsum[item.ID_VSP] == 0: if checksum[item.ID_VSP] == 1 or checksum[ item.ID_VSP] == 0: name = query_names.get(item.ID_VSP).NAME_VSP send_alert(item.ID_VSP, name, False, last_qsum[item.ID_VSP]) checksum[item.ID_VSP] = 2 print("QSUM lower than 0") weight0[id] = float(item_d.C_WEIGHT) time.sleep(60) return jsonify(status=200)