@@ -256,18 +256,15 @@ def generate_session(self, request_token, api_secret):
256
256
h = hashlib .sha256 (self .api_key .encode ("utf-8" ) + request_token .encode ("utf-8" ) + api_secret .encode ("utf-8" ))
257
257
checksum = h .hexdigest ()
258
258
259
- resp = self ._post ("api.token" , params = {
259
+ resp = self ._format_response ( self . _post ("api.token" , params = {
260
260
"api_key" : self .api_key ,
261
261
"request_token" : request_token ,
262
262
"checksum" : checksum
263
- })
263
+ }))
264
264
265
265
if "access_token" in resp :
266
266
self .set_access_token (resp ["access_token" ])
267
267
268
- if resp ["login_time" ] and self .is_timestamp (resp ["login_time" ]):
269
- resp ["login_time" ] = self .parseDateTime (resp ["login_time" ])
270
-
271
268
return resp
272
269
273
270
def invalidate_access_token (self , access_token = None ):
@@ -399,7 +396,8 @@ def _format_response(self, data):
399
396
400
397
for item in _list :
401
398
# Convert date time string to datetime object
402
- for field in ["order_timestamp" , "exchange_timestamp" , "created" , "last_instalment" , "fill_timestamp" , "timestamp" , "last_trade_time" ]:
399
+ for field in ["order_timestamp" , "exchange_timestamp" , "created" , "last_instalment" , "fill_timestamp" ,
400
+ "timestamp" , "last_trade_time" , "login_time" , "expiry" , "last_price_date" ]:
403
401
if item .get (field ) and self .is_timestamp (item [field ]):
404
402
item [field ] = self .parseDateTime (item [field ])
405
403
@@ -786,50 +784,19 @@ def _parse_instruments(self, data):
786
784
if not PY2 and type (d ) == bytes :
787
785
d = data .decode ("utf-8" ).strip ()
788
786
789
- records = []
790
787
reader = csv .DictReader (StringIO (d ))
791
788
792
- for row in reader :
793
- row ["instrument_token" ] = int (row ["instrument_token" ])
794
- row ["last_price" ] = float (row ["last_price" ])
795
- row ["strike" ] = float (row ["strike" ])
796
- row ["tick_size" ] = float (row ["tick_size" ])
797
- row ["lot_size" ] = int (row ["lot_size" ])
798
-
799
- # Parse date
800
- if self .is_timestamp (row ["expiry" ]):
801
- row ["expiry" ] = self .parseDateTime (row ["expiry" ]).date ()
802
-
803
- records .append (row )
804
-
805
- return records
789
+ return self ._format_response (list (reader ))
806
790
807
791
def _parse_mf_instruments (self , data ):
808
792
# decode to string for Python 3
809
793
d = data
810
794
if not PY2 and type (d ) == bytes :
811
795
d = data .decode ("utf-8" ).strip ()
812
796
813
- records = []
814
797
reader = csv .DictReader (StringIO (d ))
815
798
816
- for row in reader :
817
- row ["minimum_purchase_amount" ] = float (row ["minimum_purchase_amount" ])
818
- row ["purchase_amount_multiplier" ] = float (row ["purchase_amount_multiplier" ])
819
- row ["minimum_additional_purchase_amount" ] = float (row ["minimum_additional_purchase_amount" ])
820
- row ["minimum_redemption_quantity" ] = float (row ["minimum_redemption_quantity" ])
821
- row ["redemption_quantity_multiplier" ] = float (row ["redemption_quantity_multiplier" ])
822
- row ["purchase_allowed" ] = bool (int (row ["purchase_allowed" ]))
823
- row ["redemption_allowed" ] = bool (int (row ["redemption_allowed" ]))
824
- row ["last_price" ] = float (row ["last_price" ])
825
-
826
- # Parse date
827
- if self .is_timestamp (row ["last_price_date" ]):
828
- row ["last_price_date" ] = self .parseDateTime (row ["last_price_date" ]).date ()
829
-
830
- records .append (row )
831
-
832
- return records
799
+ return self ._format_response (list (reader ))
833
800
834
801
def is_timestamp (self , string ):
835
802
"""Checks if string is timestamp"""
0 commit comments