Best Python code snippet using pyatom_python
antares.py
Source:antares.py
1'''2Antares Python v1.0.03Available functions:4setAccessKey()5getAccessKey()6get()7getAll() 8getAllId()9getDevices()10getSpecific()11send()12'''13import requests14import json15_antaresAccessKey = ''16_debug = False17def getDebug():18 global _debug;19 return _debug;20def setDebug(debugStatus):21 global _debug22 _debug = debugStatus23def test():24 print('Hello from antares package!')25def setAccessKey(accessKey):26 global _antaresAccessKey27 _antaresAccessKey = accessKey28 if(getDebug()):29 print('Access key:', _antaresAccessKey)30def getAccessKey():31 global _antaresAccessKey32 return _antaresAccessKey33def get(projectName, deviceName, debug=True):34 # print(getDebug())35 # print('Requesting...')36 url = 'https://platform.antares.id:8443/~/antares-cse/antares-id/{}/{}/la'.format(projectName, deviceName)37 38 # print('Access key: ', getAccessKey())39 headers = {40 'X-M2M-Origin' : getAccessKey(),41 'Content-Type' : 'application/json;ty=4',42 'Accept' : 'application/json'43 }44 r = requests.get(url, headers=headers)45 response = r.json()46 data = response['m2m:cin']47 48 parsedContent = {}49 try:50 parsedContent = json.loads(data['con'])51 except:52 parsedContent = data['con']53 finalResponse = {54 'resource_name': data['rn'],55 'resource_identifier': data['ri'],56 'parent_id': data['pi'],57 'created_time': data['ct'],58 'last_modified_time': data['lt'],59 'content' : parsedContent60 }61 62 if(getDebug()):63 print(json.dumps(finalResponse, indent=4))64 # print(finalResponse['content'])65 return finalResponse66def getDevices(projectName, debug=True):67 # print('Requesting...')68 url = 'https://platform.antares.id:8443/~/antares-cse/antares-id/{}?fu=1&ty=3'.format(projectName)69 70 # print('Access key: ', getAccessKey())71 headers = {72 'X-M2M-Origin' : getAccessKey(),73 'Content-Type' : 'application/json;ty=3',74 'Accept' : 'application/json'75 }76 r = requests.get(url, headers=headers)77 response = r.json()78 deviceUrl = response['m2m:uril']79 devicesList = []80 for device in deviceUrl:81 device = device.split('/');82 devicesList.append(device[4]) 83 84 if(getDebug()):85 print(json.dumps(devicesList, indent=4))86 # print(finalResponse['content'])87 return devicesList88def getAll(projectName, deviceName, limit=0):89 # print('Requesting...')90 if(limit != 0):91 url = 'https://platform.antares.id:8443/~/antares-cse/antares-id/{}/{}?fu=1&ty=4&drt=1&lim={}'.format(projectName, deviceName, limit)92 else:93 url = 'https://platform.antares.id:8443/~/antares-cse/antares-id/{}/{}?fu=1&ty=4&drt=1'.format(projectName, deviceName)94 # print('Access key: ', getAccessKey())95 headers = {96 'X-M2M-Origin' : getAccessKey(),97 'Content-Type' : 'application/json',98 'Accept' : 'application/json'99 }100 r = requests.get(url, headers=headers)101 response = r.json()102 allData = response['m2m:uril']103 dataCounter = 0104 dataStorage = []105 for urlInd in allData:106 # print(urlInd)107 url = 'https://platform.antares.id:8443/~{}'.format(urlInd)108 r = requests.get(url, headers=headers)109 response = r.json()['m2m:cin']110 dataCounter+=1111 # Parse content112 parsedContent = {}113 try:114 parsedContent = json.loads(response['con'])115 except:116 parsedContent = response['con']117 finalResponse = {118 'resource_name': response['rn'],119 'resource_identifier': response['ri'],120 'parent_id': response['pi'],121 'created_time': response['ct'],122 'last_modified_time': response['lt'],123 'content' : parsedContent124 }125 if(getDebug()):126 print('Get success:{} out of {}'.format(dataCounter, len(allData)))127 128 dataStorage.append(finalResponse)129 if(limit > 0):130 if(dataCounter >= limit):131 if(getDebug()):132 print(json.dumps(dataStorage, indent=4))133 print('Data size: {}'.format(len(dataStorage)))134 return dataStorage135 else:136 if(dataCounter >= len(allData)):137 if(getDebug()):138 print(json.dumps(dataStorage, indent=4))139 print(len(dataStorage))140 return dataStorage141def getAllId(projectName, deviceName, limit=0):142 # print('Requesting...')143 if(limit != 0):144 url = 'https://platform.antares.id:8443/~/antares-cse/antares-id/{}/{}?fu=1&ty=4&drt=1&lim={}'.format(projectName, deviceName, limit)145 else:146 url = 'https://platform.antares.id:8443/~/antares-cse/antares-id/{}/{}?fu=1&ty=4&drt=1'.format(projectName, deviceName)147 # print('Access key: ', getAccessKey())148 headers = {149 'X-M2M-Origin' : getAccessKey(),150 'Content-Type' : 'application/json',151 'Accept' : 'application/json'152 }153 r = requests.get(url, headers=headers)154 response = r.json()155 allData = response['m2m:uril']156 157 dataIds = []158 for url in allData:159 url = url.split('/')160 dataIds.append(url[5])161 if(getDebug()):162 print(json.dumps(dataIds, indent=4))163 print('Length: {} data'.format(len(dataIds)))164 return dataIds165def send(data, projectName, deviceName):166 # print('Requesting...')167 url = 'https://platform.antares.id:8443/~/antares-cse/antares-id/{}/{}'.format(projectName, deviceName)168 169 # print('Access key: ', getAccessKey())170 headers = {171 'X-M2M-Origin' : getAccessKey(),172 'Content-Type' : 'application/json;ty=4',173 'Accept' : 'application/json',174 }175 strData = ''176 try:177 strData = json.dumps(data) 178 except:179 strData = data180 dataTemplate = {181 "m2m:cin" : {182 "con" : strData,183 }184 }185 dataTemplate = json.dumps(dataTemplate)186 # print(dataTemplate)187 # print(url)188 r = requests.post(url, headers=headers, data=dataTemplate)189 response = r.json()190 data = response['m2m:cin']191 parsedContent = {}192 try:193 parsedContent = json.loads(data['con'])194 except:195 parsedContent = data['con']196 finalData = {197 'resource_name' : data['rn'],198 'resource_identifier' : data['ri'],199 'parent_id' : data['pi'],200 'created_time' : data['ct'],201 'last_modified_time' : data['lt'],202 'content' : parsedContent203 }204 if(getDebug()):205 print(json.dumps(finalData, indent=4))206 return(finalData)207def createDevice(projectName, newDeviceName):208 # print('Requesting...')209 url = 'https://platform.antares.id:8443/~/antares-cse/antares-id/{}'.format(projectName)210 211 # print('Access key: ', getAccessKey())212 headers = {213 'X-M2M-Origin' : getAccessKey(),214 'Content-Type' : 'application/json;ty=3',215 'Accept' : 'application/json',216 }217 dataTemplate = {218 "m2m:cnt" : {219 "rn" : newDeviceName,220 }221 }222 dataTemplate = json.dumps(dataTemplate)223 # print(dataTemplate)224 # print(url)225 r = requests.post(url, headers=headers, data=dataTemplate)226 response = r.json()227 data = response['m2m:cnt']228 newData = {229 'resource_name' : data['rn'],230 'resource_identifier' : data['ri'],231 'parent_id' : data['pi'],232 'created_time' : data['ct'],233 'last_modified_time' : data['lt'],234 'acpi' : data['acpi'],235 'et' : data['et'],236 }237 if(getDebug()):238 print(json.dumps(newData, indent=4))239 return(newData)240def getSpecific(projectName, deviceName, identifier):241 # print('Requesting...')242 url = 'https://platform.antares.id:8443/~/antares-cse/antares-id/{}/{}/{}'.format(projectName, deviceName, identifier)243 # print(url)244 # print('Access key: ', getAccessKey())245 headers = {246 'X-M2M-Origin' : getAccessKey(),247 'Content-Type' : 'application/json;ty=4',248 'Accept' : 'application/json'249 }250 r = requests.get(url, headers=headers)251 response = r.json()252 # resText = r.text253 # print(resText)254 data = response['m2m:cin']255 256 parsedContent = {}257 try:258 parsedContent = json.loads(data['con'])259 except:260 parsedContent = data['con']261 finalResponse = {262 'resource_name': data['rn'],263 'resource_identifier': data['ri'],264 'parent_id': data['pi'],265 'created_time': data['ct'],266 'last_modified_time': data['lt'],267 'content' : parsedContent268 }269 270 if(getDebug()):271 print(json.dumps(finalResponse, indent=4))272 # print(finalResponse['content'])273 return finalResponse274def getDeviceId(projectName, deviceName):275 url = 'https://platform.antares.id:8443/~/antares-cse/antares-id/{}/{}'.format(projectName, deviceName)276 277 # print('Access key: ', getAccessKey())278 headers = {279 'X-M2M-Origin' : getAccessKey(),280 'Content-Type' : 'application/json',281 'Accept' : 'application/json',282 }283 r = requests.get(url, headers=headers)284 response = r.json()285 # print(response)286 data = response['m2m:cnt']['ri']287 data = data.split('/')[2]288 289 parsedContent = {}290 if(getDebug()):291 print(data)292 return data293def sendById(data, deviceId):294 # print('Requesting...')295 url = 'https://platform.antares.id:8443/~/antares-cse/{}'.format(deviceId)296 297 # print('Access key: ', getAccessKey())298 headers = {299 'X-M2M-Origin' : getAccessKey(),300 'Content-Type' : 'application/json;ty=4',301 'Accept' : 'application/json',302 }303 strData = ''304 try:305 strData = json.dumps(data) 306 except:307 strData = data308 dataTemplate = {309 "m2m:cin" : {310 "con" : strData,311 }312 }313 dataTemplate = json.dumps(dataTemplate)314 # print(dataTemplate)315 # print(url)316 r = requests.post(url, headers=headers, data=dataTemplate)317 response = r.json()318 # print(response)319 data = response['m2m:cin']320 parsedContent = {}321 try:322 parsedContent = json.loads(data['con'])323 except:324 parsedContent = data['con']325 finalData = {326 'resource_name' : data['rn'],327 'resource_identifier' : data['ri'],328 'parent_id' : data['pi'],329 'created_time' : data['ct'],330 'last_modified_time' : data['lt'],331 'content' : parsedContent332 }333 if(getDebug()):334 print(json.dumps(finalData, indent=4))...
basicServices.py
Source:basicServices.py
1from BitcoinSDK import HuobiServices2from .env import ENV3# æ¥å£è¯´æææ¡£: https://github.com/huobiapi/API_Docs/wiki/REST_api_reference4def getAccessKey(request):5 # if(ENV == 'dev'):6 # return 'bb3a0e08-7ad6be2b-4ab32336-eecf7'7 return request.session['ACCESS_KEY']8def getSecretKey(request):9 # if(ENV == 'dev'):10 # return '17497c8e-781c132b-00cf96d4-457c4'11 return request.session['SECRET_KEY']12'''13Market data API14'''15# è·åKLine16def get_kline(symbol, period, size=150):17 """18 :param symbol19 :param period: å¯éå¼ï¼{1min, 5min, 15min, 30min, 60min, 1day, 1mon, 1week, 1year }20 :param size: å¯éå¼ï¼ [1,2000]21 :return:22 """23 return HuobiServices.get_kline(symbol, period, size)24# è·åmarketdepth25def get_depth(symbol, type):26 """27 :param symbol28 :param type: å¯éå¼ï¼{ percent10, step0, step1, step2, step3, step4, step5 }29 :return:30 """31 return HuobiServices.get_depth(symbol, type)32# è·åtradedetail33def get_trade(symbol):34 """35 :param symbol36 :return:37 """38 return HuobiServices.get_trade(symbol)39# è·åmerge ticker40def get_ticker(symbol):41 """42 :param symbol: 43 :return:44 """45 return HuobiServices.get_ticker(symbol)46# è·å Market Detail 24å°æ¶æ交éæ°æ®47def get_detail(symbol):48 """49 :param symbol50 :return:51 """52 return HuobiServices.get_detail(symbol)53# è·å æ¯æç交æ对54def get_symbols(request, long_polling=None):55 return HuobiServices.get_symbols(56 long_polling,57 getAccessKey(request),58 getSecretKey(request)59 )60'''61Trade/Account API62'''63def get_accounts(request):64 return HuobiServices.get_accounts(65 getAccessKey(request),66 getSecretKey(request)67 )68# ACCOUNT_ID = 069# è·åå½åè´¦æ·èµäº§70# def get_balance(acct_id=None):71def get_balance(request):72 return HuobiServices.get_balance(73 getAccessKey(request),74 getSecretKey(request)75 )76# ä¸å77# å建并æ§è¡è®¢å78def send_order(request, amount, source, _type, price=0):79 """80 :param amount: 81 :param source: å¦æ使ç¨åè´·èµäº§äº¤æï¼è¯·å¨ä¸åæ¥å£,请æ±åæ°sourceä¸å¡«å'margin-api'82 :param symbol: 83 :param _type: å¯éå¼ {buy-marketï¼å¸ä»·ä¹°, sell-marketï¼å¸ä»·å, buy-limitï¼éä»·ä¹°, sell-limitï¼éä»·å}84 :param price: 85 :return: 86 """87 return HuobiServices.send_order(88 amount, source,89 symbol, _type,90 getAccessKey(request),91 getSecretKey(request),92 price93 )94# æ¤é订å95def cancel_order(request, order_id):96 return HuobiServices.cancel_order(97 order_id,98 getAccessKey(request),99 getSecretKey(request)100 )101# æ¥è¯¢æ个订å102def order_info(request, order_id):103 return HuobiServices.order_info(104 order_id,105 getAccessKey(request),106 getSecretKey(request)107 )108# æ¥è¯¢æ个订åçæ交æç»109def order_matchresults(request, order_id):110 return HuobiServices.order_matchresults(111 order_id,112 getAccessKey(request),113 getSecretKey(request)114 )115# æ¥è¯¢å½åå§æãåå²å§æ116def orders_list(117 request,118 symbol, states,119 types=None, start_date=None,120 end_date=None, _from=None,121 direct=None, size=None122):123 """124 125 :param symbol: 126 :param states: å¯éå¼ {pre-submitted åå¤æ交, submitted å·²æ交, partial-filled é¨åæ交, partial-canceled é¨åæ交æ¤é, filled å®å
¨æ交, canceled å·²æ¤é}127 :param types: å¯éå¼ {buy-marketï¼å¸ä»·ä¹°, sell-marketï¼å¸ä»·å, buy-limitï¼éä»·ä¹°, sell-limitï¼éä»·å}128 :param start_date: 129 :param end_date: 130 :param _from: 131 :param direct: å¯éå¼{prev ååï¼next åå}132 :param size: 133 :return: 134 """135 return HuobiServices.orders_list(136 symbol, states,137 getAccessKey(request),138 getSecretKey(request),139 types, start_date,140 end_date, _from,141 direct, size142 )143# æ¥è¯¢å½åæ交ãåå²æ交144def orders_matchresults(145 request,146 symbol, types=None,147 start_date=None, end_date=None,148 _from=None, direct=None,149 size=None150):151 """152 153 :param symbol: 154 :param types: å¯éå¼ {buy-marketï¼å¸ä»·ä¹°, sell-marketï¼å¸ä»·å, buy-limitï¼éä»·ä¹°, sell-limitï¼éä»·å}155 :param start_date: 156 :param end_date: 157 :param _from: 158 :param direct: å¯éå¼{prev ååï¼next åå}159 :param size: 160 :return: 161 """162 return HuobiServices.orders_matchresults(163 symbol,164 getAccessKey(request),165 getSecretKey(request),166 types, start_date,167 end_date, _from,168 direct, size169 )170# ç³è¯·æç°èæå¸171def withdraw(172 request, address,173 amount, currency,174 fee=0, addr_tag=""175):176 """177 :param address_id: 178 :param amount: 179 :param currency:btc, ltc, bcc, eth, etc ...(ç«å¸Proæ¯æçå¸ç§)180 :param fee: 181 :param addr-tag:182 :return: {183 "status": "ok",184 "data": 700185 }186 """187 return HuobiServices.withdraw(188 address, amount, currency,189 getAccessKey(request),190 getSecretKey(request),191 fee, addr_tag192 )193# ç³è¯·åæ¶æç°èæå¸194def cancel_withdraw(request, address_id):195 """196 :param address_id: 197 :return: {198 "status": "ok",199 "data": 700200 }201 """202 return HuobiServices.cancel_withdraw(203 address_id,204 getAccessKey(request),205 getSecretKey(request)206 )207'''208åè´·API209'''210# å建并æ§è¡å贷订å211def send_margin_order(request, amount, source, symbol, _type, price=0):212 """213 :param amount: 214 :param source: 'margin-api'215 :param symbol: 216 :param _type: å¯éå¼ {buy-marketï¼å¸ä»·ä¹°, sell-marketï¼å¸ä»·å, buy-limitï¼éä»·ä¹°, sell-limitï¼éä»·å}217 :param price: 218 :return: 219 """220 return HuobiServices.send_margin_order(221 amount, source,222 symbol, _type,223 getAccessKey(request),224 getSecretKey(request),225 price226 )227# ç°è´§è´¦æ·åå
¥è³åè´·è´¦æ·228def exchange_to_margin(symbol, currency, amount, request):229 """230 :param amount: 231 :param currency: 232 :param symbol: 233 :return: 234 """235 return HuobiServices.exchange_to_margin(236 symbol, currency, amount,237 getAccessKey(request),238 getSecretKey(request)239 )240# åè´·è´¦æ·ååºè³ç°è´§è´¦æ·241def margin_to_exchange(request, symbol, currency, amount):242 """243 :param amount: 244 :param currency: 245 :param symbol: 246 :return: 247 """248 return HuobiServices.margin_to_exchange(249 symbol, currency,250 amount,251 getAccessKey(request),252 getSecretKey(request)253 )254# ç³è¯·åè´·255def get_margin(request, symbol, currency, amount):256 """257 :param amount: 258 :param currency: 259 :param symbol: 260 :return: 261 """262 return HuobiServices.get_margin(263 symbol, currency, amount,264 getAccessKey(request),265 getSecretKey(request)266 )267# å½è¿åè´·268def repay_margin(request, order_id, amount):269 """270 :param order_id: 271 :param amount: 272 :return: 273 """274 return HuobiServices.repay_margin(275 order_id, amount,276 getAccessKey(request),277 getSecretKey(request)278 )279# å贷订å280def loan_orders(281 request,282 symbol, currency, start_date="",283 end_date="", start="", direct="",284 size=""):285 """286 :param symbol: 287 :param currency: 288 :param direct: prev ååï¼next åå289 :return: 290 """291 return HuobiServices.loan_orders(292 symbol, currency,293 getAccessKey(request),294 getSecretKey(request),295 start_date, end_date,296 start, direct,297 size298 )299# åè´·è´¦æ·è¯¦æ
,æ¯ææ¥è¯¢å个å¸ç§300def margin_balance(request, symbol):301 """302 :param symbol: 303 :return: 304 """305 return HuobiServices.margin_balance(306 symbol,307 getAccessKey(request),308 getSecretKey(request)309 )310if __name__ == '__main__':...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!