Skip to content

Commit 5aef588

Browse files
committed
feat: add all time fields to _format_response
1 parent 72f2e9b commit 5aef588

File tree

1 file changed

+6
-39
lines changed

1 file changed

+6
-39
lines changed

kiteconnect/connect.py

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -256,18 +256,15 @@ def generate_session(self, request_token, api_secret):
256256
h = hashlib.sha256(self.api_key.encode("utf-8") + request_token.encode("utf-8") + api_secret.encode("utf-8"))
257257
checksum = h.hexdigest()
258258

259-
resp = self._post("api.token", params={
259+
resp = self._format_response(self._post("api.token", params={
260260
"api_key": self.api_key,
261261
"request_token": request_token,
262262
"checksum": checksum
263-
})
263+
}))
264264

265265
if "access_token" in resp:
266266
self.set_access_token(resp["access_token"])
267267

268-
if resp["login_time"] and self.is_timestamp(resp["login_time"]):
269-
resp["login_time"] = self.parseDateTime(resp["login_time"])
270-
271268
return resp
272269

273270
def invalidate_access_token(self, access_token=None):
@@ -399,7 +396,8 @@ def _format_response(self, data):
399396

400397
for item in _list:
401398
# 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"]:
403401
if item.get(field) and self.is_timestamp(item[field]):
404402
item[field] = self.parseDateTime(item[field])
405403

@@ -786,50 +784,19 @@ def _parse_instruments(self, data):
786784
if not PY2 and type(d) == bytes:
787785
d = data.decode("utf-8").strip()
788786

789-
records = []
790787
reader = csv.DictReader(StringIO(d))
791788

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))
806790

807791
def _parse_mf_instruments(self, data):
808792
# decode to string for Python 3
809793
d = data
810794
if not PY2 and type(d) == bytes:
811795
d = data.decode("utf-8").strip()
812796

813-
records = []
814797
reader = csv.DictReader(StringIO(d))
815798

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))
833800

834801
def is_timestamp(self, string):
835802
"""Checks if string is timestamp"""

0 commit comments

Comments
 (0)