From 5aef5889f47172cc5edae5344e6d59ecf2e59c21 Mon Sep 17 00:00:00 2001 From: Rakesh R Date: Fri, 16 Jul 2021 14:35:46 +0530 Subject: [PATCH] feat: add all time fields to _format_response --- kiteconnect/connect.py | 45 ++++++------------------------------------ 1 file changed, 6 insertions(+), 39 deletions(-) diff --git a/kiteconnect/connect.py b/kiteconnect/connect.py index 462e8d0b..b189f4f6 100644 --- a/kiteconnect/connect.py +++ b/kiteconnect/connect.py @@ -256,18 +256,15 @@ def generate_session(self, request_token, api_secret): h = hashlib.sha256(self.api_key.encode("utf-8") + request_token.encode("utf-8") + api_secret.encode("utf-8")) checksum = h.hexdigest() - resp = self._post("api.token", params={ + resp = self._format_response(self._post("api.token", params={ "api_key": self.api_key, "request_token": request_token, "checksum": checksum - }) + })) if "access_token" in resp: self.set_access_token(resp["access_token"]) - if resp["login_time"] and self.is_timestamp(resp["login_time"]): - resp["login_time"] = self.parseDateTime(resp["login_time"]) - return resp def invalidate_access_token(self, access_token=None): @@ -399,7 +396,8 @@ def _format_response(self, data): for item in _list: # Convert date time string to datetime object - for field in ["order_timestamp", "exchange_timestamp", "created", "last_instalment", "fill_timestamp", "timestamp", "last_trade_time"]: + for field in ["order_timestamp", "exchange_timestamp", "created", "last_instalment", "fill_timestamp", + "timestamp", "last_trade_time", "login_time", "expiry", "last_price_date"]: if item.get(field) and self.is_timestamp(item[field]): item[field] = self.parseDateTime(item[field]) @@ -786,23 +784,9 @@ def _parse_instruments(self, data): if not PY2 and type(d) == bytes: d = data.decode("utf-8").strip() - records = [] reader = csv.DictReader(StringIO(d)) - for row in reader: - row["instrument_token"] = int(row["instrument_token"]) - row["last_price"] = float(row["last_price"]) - row["strike"] = float(row["strike"]) - row["tick_size"] = float(row["tick_size"]) - row["lot_size"] = int(row["lot_size"]) - - # Parse date - if self.is_timestamp(row["expiry"]): - row["expiry"] = self.parseDateTime(row["expiry"]).date() - - records.append(row) - - return records + return self._format_response(list(reader)) def _parse_mf_instruments(self, data): # decode to string for Python 3 @@ -810,26 +794,9 @@ def _parse_mf_instruments(self, data): if not PY2 and type(d) == bytes: d = data.decode("utf-8").strip() - records = [] reader = csv.DictReader(StringIO(d)) - for row in reader: - row["minimum_purchase_amount"] = float(row["minimum_purchase_amount"]) - row["purchase_amount_multiplier"] = float(row["purchase_amount_multiplier"]) - row["minimum_additional_purchase_amount"] = float(row["minimum_additional_purchase_amount"]) - row["minimum_redemption_quantity"] = float(row["minimum_redemption_quantity"]) - row["redemption_quantity_multiplier"] = float(row["redemption_quantity_multiplier"]) - row["purchase_allowed"] = bool(int(row["purchase_allowed"])) - row["redemption_allowed"] = bool(int(row["redemption_allowed"])) - row["last_price"] = float(row["last_price"]) - - # Parse date - if self.is_timestamp(row["last_price_date"]): - row["last_price_date"] = self.parseDateTime(row["last_price_date"]).date() - - records.append(row) - - return records + return self._format_response(list(reader)) def is_timestamp(self, string): """Checks if string is timestamp"""