/
connect.py
770 lines (626 loc) · 27.6 KB
/
connect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
# -*- coding: utf-8 -*-
"""
connect.py
API wrapper for Kite Connect REST APIs.
:copyright: (c) 2018 by Zerodha Technology.
:license: see LICENSE for details.
"""
from six import StringIO, PY2
from six.moves.urllib.parse import urljoin
import csv
import json
import dateutil.parser
import hashlib
import logging
import datetime
import requests
from .__version__ import __version__, __title__
import kiteconnect.exceptions as ex
log = logging.getLogger(__name__)
class KiteConnect(object):
"""
The Kite Connect API wrapper class.
In production, you may initialise a single instance of this class per `api_key`.
"""
# Default root API endpoint. It's possible to
# override this by passing the `root` parameter during initialisation.
_default_root_uri = "https://api.kite.trade"
_default_login_uri = "https://kite.trade/connect/login"
_default_timeout = 7 # In seconds
# Constants
# Products
PRODUCT_MIS = "MIS"
PRODUCT_CNC = "CNC"
PRODUCT_NRML = "NRML"
PRODUCT_CO = "CO"
PRODUCT_BO = "BO"
# Order types
ORDER_TYPE_MARKET = "MARKET"
ORDER_TYPE_LIMIT = "LIMIT"
ORDER_TYPE_SLM = "SL-M"
ORDER_TYPE_SL = "SL"
# Varities
VARIETY_REGULAR = "regular"
VARIETY_BO = "bo"
VARIETY_CO = "co"
VARIETY_AMO = "amo"
# Transaction type
TRANSACTION_TYPE_BUY = "BUY"
TRANSACTION_TYPE_SELL = "SELL"
# Validity
VALIDITY_DAY = "DAY"
VALIDITY_IOC = "IOC"
# Exchanges
EXCHANGE_NSE = "NSE"
EXCHANGE_BSE = "BSE"
EXCHANGE_NFO = "NFO"
EXCHANGE_CDS = "CDS"
EXCHANGE_BFO = "BFO"
EXCHANGE_MCX = "MCX"
# Margins segments
MARGIN_EQUITY = "equity"
MARGIN_COMMODITY = "commodity"
# Status constants
STATUS_COMPLETE = "COMPLETE"
STATUS_REJECTED = "REJECTED"
STATUS_CANCELLED = "CANCELLED"
# URIs to various calls
_routes = {
"api.token": "/session/token",
"api.token.invalidate": "/session/token",
"api.token.renew": "/session/refresh_token",
"user.profile": "/user/profile",
"user.margins": "/user/margins",
"user.margins.segment": "/user/margins/{segment}",
"orders": "/orders",
"trades": "/trades",
"order.info": "/orders/{order_id}",
"order.place": "/orders/{variety}",
"order.modify": "/orders/{variety}/{order_id}",
"order.cancel": "/orders/{variety}/{order_id}",
"order.trades": "/orders/{order_id}/trades",
"portfolio.positions": "/portfolio/positions",
"portfolio.holdings": "/portfolio/holdings",
"portfolio.positions.convert": "/portfolio/positions",
# MF api endpoints
"mf.orders": "/mf/orders",
"mf.order.info": "/mf/orders/{order_id}",
"mf.order.place": "/mf/orders",
"mf.order.cancel": "/mf/orders/{order_id}",
"mf.sips": "/mf/sips",
"mf.sip.info": "/mf/sips/{sip_id}",
"mf.sip.place": "/mf/sips",
"mf.sip.modify": "/mf/sips/{sip_id}",
"mf.sip.cancel": "/mf/sips/{sip_id}",
"mf.holdings": "/mf/holdings",
"mf.instruments": "/mf/instruments",
"market.instruments.all": "/instruments",
"market.instruments": "/instruments/{exchange}",
"market.margins": "/margins/{segment}",
"market.historical": "/instruments/historical/{instrument_token}/{interval}",
"market.trigger_range": "/instruments/trigger_range/{transaction_type}",
"market.quote": "/quote",
"market.quote.ohlc": "/quote/ohlc",
"market.quote.ltp": "/quote/ltp",
}
def __init__(self,
api_key,
access_token=None,
root=None,
debug=False,
timeout=None,
proxies=None,
pool=None,
disable_ssl=False):
"""
Initialise a new Kite Connect client instance.
- `api_key` is the key issued to you
- `access_token` is the token obtained after the login flow in
exchange for the `request_token` . Pre-login, this will default to None,
but once you have obtained it, you should
persist it in a database or session to pass
to the Kite Connect class initialisation for subsequent requests.
- `root` is the API end point root. Unless you explicitly
want to send API requests to a non-default endpoint, this
can be ignored.
- `debug`, if set to True, will serialise and print requests
and responses to stdout.
- `timeout` is the time (seconds) for which the API client will wait for
a request to complete before it fails. Defaults to 7 seconds
- `proxies` to set requests proxy.
Check [python requests documentation](http://docs.python-requests.org/en/master/user/advanced/#proxies) for usage and examples.
- `pool` is manages request pools. It takes a dict of params accepted by HTTPAdapter as described here in [python requests documentation](http://docs.python-requests.org/en/master/api/#requests.adapters.HTTPAdapter)
- `disable_ssl` disables the SSL verification while making a request.
If set requests won't throw SSLError if its set to custom `root` url without SSL.
"""
self.debug = debug
self.api_key = api_key
self.session_expiry_hook = None
self.disable_ssl = disable_ssl
self.access_token = access_token
self.proxies = proxies if proxies else {}
self.root = root or self._default_root_uri
self.timeout = timeout or self._default_timeout
pool = pool or {}
self.reqsession = requests.Session()
reqadapter = requests.adapters.HTTPAdapter(**pool)
self.reqsession.mount("https://", reqadapter)
# disable requests SSL warning
requests.packages.urllib3.disable_warnings()
def set_session_expiry_hook(self, method):
"""
Set a callback hook for session (`TokenError` -- timeout, expiry etc.) errors.
An `access_token` (login session) can become invalid for a number of
reasons, but it doesn't make sense for the client to
try and catch it during every API call.
A callback method that handles session errors
can be set here and when the client encounters
a token error at any point, it'll be called.
This callback, for instance, can log the user out of the UI,
clear session cookies, or initiate a fresh login.
"""
if not callable(method):
raise TypeError("Invalid input type. Only functions are accepted.")
self.session_expiry_hook = method
def set_access_token(self, access_token):
"""Set the `access_token` received after a successful authentication."""
self.access_token = access_token
def login_url(self):
"""Get the remote login url to which a user should be redirected to initiate the login flow."""
return "%s?api_key=%s&v=3" % (self._default_login_uri, self.api_key)
def generate_session(self, request_token, api_secret):
"""
Generate user session details like `access_token` etc by exchanging `request_token`.
Access token is automatically set if the session is retrieved successfully.
Do the token exchange with the `request_token` obtained after the login flow,
and retrieve the `access_token` required for all subsequent requests. The
response contains not just the `access_token`, but metadata for
the user who has authenticated.
- `request_token` is the token obtained from the GET paramers after a successful login redirect.
- `api_secret` is the API api_secret issued with the API key.
"""
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", {
"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 len(resp["login_time"]) == 19:
resp["login_time"] = dateutil.parser.parse(resp["login_time"])
return resp
def invalidate_access_token(self, access_token=None):
"""
Kill the session by invalidating the access token.
- `access_token` to invalidate. Default is the active `access_token`.
"""
access_token = access_token or self.access_token
return self._delete("api.token.invalidate", {
"api_key": self.api_key,
"access_token": access_token
})
def renew_access_token(self, refresh_token, api_secret):
"""
Renew expired `refresh_token` using valid `refresh_token`.
- `refresh_token` is the token obtained from previous successful login flow.
- `api_secret` is the API api_secret issued with the API key.
"""
h = hashlib.sha256(self.api_key.encode("utf-8") + refresh_token.encode("utf-8") + api_secret.encode("utf-8"))
checksum = h.hexdigest()
resp = self._post("api.token.renew", {
"api_key": self.api_key,
"refresh_token": refresh_token,
"checksum": checksum
})
if "access_token" in resp:
self.set_access_token(resp["access_token"])
return resp
def invalidate_refresh_token(self, refresh_token):
"""
Invalidate refresh token.
- `refresh_token` is the token which is used to renew access token.
"""
return self._delete("api.token.invalidate", {
"api_key": self.api_key,
"refresh_token": refresh_token
})
def margins(self, segment=None):
"""Get account balance and cash margin details for a particular segment.
- `segment` is the trading segment (eg: equity or commodity)
"""
if segment:
return self._get("user.margins.segment", {"segment": segment})
else:
return self._get("user.margins")
def profile(self):
"""Get user profile details."""
return self._get("user.profile")
# orders
def place_order(self,
variety,
exchange,
tradingsymbol,
transaction_type,
quantity,
product,
order_type,
price=None,
validity=None,
disclosed_quantity=None,
trigger_price=None,
squareoff=None,
stoploss=None,
trailing_stoploss=None,
tag=None):
"""Place an order."""
params = locals()
del(params["self"])
for k in list(params.keys()):
if params[k] is None:
del(params[k])
return self._post("order.place", params)["order_id"]
def modify_order(self,
variety,
order_id,
parent_order_id=None,
quantity=None,
price=None,
order_type=None,
trigger_price=None,
validity=None,
disclosed_quantity=None):
"""Modify an open order."""
params = locals()
del(params["self"])
for k in list(params.keys()):
if params[k] is None:
del(params[k])
return self._put("order.modify", params)["order_id"]
def cancel_order(self, variety, order_id, parent_order_id=None):
"""Cancel an order."""
return self._delete("order.cancel", {
"order_id": order_id,
"variety": variety,
"parent_order_id": parent_order_id
})["order_id"]
def exit_order(self, variety, order_id, parent_order_id=None):
"""Exit a BO/CO order."""
self.cancel_order(variety, order_id, parent_order_id=parent_order_id)
def _format_response(self, data):
"""Parse and format responses."""
if type(data) == list:
_list = data
elif type(data) == dict:
_list = [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"]:
if item.get(field) and len(item[field]) == 19:
item[field] = dateutil.parser.parse(item[field])
return _list[0] if type(data) == dict else _list
# orderbook and tradebook
def orders(self):
"""Get list of orders."""
return self._format_response(self._get("orders"))
def order_history(self, order_id):
"""
Get history of individual order.
- `order_id` is the ID of the order to retrieve order history.
"""
return self._format_response(self._get("order.info", {"order_id": order_id}))
def trades(self):
"""
Retrieve the list of trades executed (all or ones under a particular order).
An order can be executed in tranches based on market conditions.
These trades are individually recorded under an order.
- `order_id` is the ID of the order (optional) whose trades are to be retrieved.
If no `order_id` is specified, all trades for the day are returned.
"""
return self._format_response(self._get("trades"))
def order_trades(self, order_id):
"""
Retrieve the list of trades executed for a particular order.
- `order_id` is the ID of the order (optional) whose trades are to be retrieved.
If no `order_id` is specified, all trades for the day are returned.
"""
return self._format_response(self._get("order.trades", {"order_id": order_id}))
def positions(self):
"""Retrieve the list of positions."""
return self._get("portfolio.positions")
def holdings(self):
"""Retrieve the list of equity holdings."""
return self._get("portfolio.holdings")
def convert_position(self,
exchange,
tradingsymbol,
transaction_type,
position_type,
quantity,
old_product,
new_product):
"""Modify an open position's product type."""
return self._put("portfolio.positions.convert", {
"exchange": exchange,
"tradingsymbol": tradingsymbol,
"transaction_type": transaction_type,
"position_type": position_type,
"quantity": quantity,
"old_product": old_product,
"new_product": new_product
})
def mf_orders(self, order_id=None):
"""Get all mutual fund orders or individual order info."""
if order_id:
return self._format_response(self._get("mf.order.info", {"order_id": order_id}))
else:
return self._format_response(self._get("mf.orders"))
def place_mf_order(self,
tradingsymbol,
transaction_type,
quantity=None,
amount=None,
tag=None):
"""Place a mutual fund order."""
return self._post("mf.order.place", {
"tradingsymbol": tradingsymbol,
"transaction_type": transaction_type,
"quantity": quantity,
"amount": amount,
"tag": tag
})
def cancel_mf_order(self, order_id):
"""Cancel a mutual fund order."""
return self._delete("mf.order.cancel", {"order_id": order_id})
def mf_sips(self, sip_id=None):
"""Get list of all mutual fund SIP's or individual SIP info."""
if sip_id:
return self._format_response(self._get("mf.sip.info", {"sip_id": sip_id}))
else:
return self._format_response(self._get("mf.sips"))
def place_mf_sip(self,
tradingsymbol,
amount,
instalments,
frequency,
initial_amount=None,
instalment_day=None,
tag=None):
"""Place a mutual fund SIP."""
return self._post("mf.sip.place", {
"tradingsymbol": tradingsymbol,
"amount": amount,
"initial_amount": initial_amount,
"instalments": instalments,
"frequency": frequency,
"instalment_day": instalment_day,
"tag": tag
})
def modify_mf_sip(self,
sip_id,
amount=None,
status=None,
instalments=None,
frequency=None,
instalment_day=None):
"""Modify a mutual fund SIP."""
return self._put("mf.sip.modify", {
"sip_id": sip_id,
"amount": amount,
"status": status,
"instalments": instalments,
"frequency": frequency,
"instalment_day": instalment_day
})
def cancel_mf_sip(self, sip_id):
"""Cancel a mutual fund SIP."""
return self._delete("mf.sip.cancel", {"sip_id": sip_id})
def mf_holdings(self):
"""Get list of mutual fund holdings."""
return self._get("mf.holdings")
def mf_instruments(self):
"""Get list of mutual fund instruments."""
return self._parse_mf_instruments(self._get("mf.instruments"))
def instruments(self, exchange=None):
"""
Retrieve the list of market instruments available to trade.
Note that the results could be large, several hundred KBs in size,
with tens of thousands of entries in the list.
- `exchange` is specific exchange to fetch (Optional)
"""
if exchange:
params = {"exchange": exchange}
return self._parse_instruments(self._get("market.instruments", params))
else:
return self._parse_instruments(self._get("market.instruments.all"))
def quote(self, *instruments):
"""
Retrieve quote for list of instruments.
- `instruments` is a list of instruments, Instrument are in the format of `tradingsymbol:exchange`. For example NSE:INFY
"""
ins = list(instruments)
# If first element is a list then accept it as instruments list for legacy reason
if len(instruments) > 0 and type(instruments[0]) == list:
ins = instruments[0]
data = self._get("market.quote", {"i": ins})
return {key: self._format_response(data[key]) for key in data}
def ohlc(self, *instruments):
"""
Retrieve OHLC and market depth for list of instruments.
- `instruments` is a list of instruments, Instrument are in the format of `tradingsymbol:exchange`. For example NSE:INFY
"""
ins = list(instruments)
# If first element is a list then accept it as instruments list for legacy reason
if len(instruments) > 0 and type(instruments[0]) == list:
ins = instruments[0]
return self._get("market.quote.ohlc", {"i": ins})
def ltp(self, *instruments):
"""
Retrieve last price for list of instruments.
- `instruments` is a list of instruments, Instrument are in the format of `tradingsymbol:exchange`. For example NSE:INFY
"""
ins = list(instruments)
# If first element is a list then accept it as instruments list for legacy reason
if len(instruments) > 0 and type(instruments[0]) == list:
ins = instruments[0]
return self._get("market.quote.ltp", {"i": ins})
# def instruments_margins(self, segment):
# """
# Retrive margins provided for individual segments.
# `segment` is segment name to retrive.
# """
# return self._get("market.margins", {"segment": segment})
def historical_data(self, instrument_token, from_date, to_date, interval, continuous=False):
"""
Retrieve historical data (candles) for an instrument.
Although the actual response JSON from the API does not have field
names such has 'open', 'high' etc., this function call structures
the data into an array of objects with field names. For example:
- `instrument_token` is the instrument identifier (retrieved from the instruments()) call.
- `from_date` is the From date (datetime object or string in format of yyyy-mm-dd HH:MM:SS.
- `to_date` is the To date (datetime object or string in format of yyyy-mm-dd HH:MM:SS).
- `interval` is the candle interval (minute, day, 5 minute etc.).
- `continuous` is a boolean flag to get continuous data for futures and options instruments.
"""
date_string_format = "%Y-%m-%d %H:%M:%S"
from_date_string = from_date.strftime(date_string_format) if type(from_date) == datetime.datetime else from_date
to_date_string = to_date.strftime(date_string_format) if type(to_date) == datetime.datetime else to_date
data = self._get("market.historical", {
"instrument_token": instrument_token,
"from": from_date_string,
"to": to_date_string,
"interval": interval,
"continuous": 1 if continuous else 0
})
return self._format_historical(data)
def _format_historical(self, data):
records = []
for d in data["candles"]:
records.append({
"date": dateutil.parser.parse(d[0]),
"open": d[1],
"high": d[2],
"low": d[3],
"close": d[4],
"volume": d[5]
})
return records
def trigger_range(self, transaction_type, *instruments):
"""Retrieve the buy/sell trigger range for Cover Orders."""
ins = list(instruments)
# If first element is a list then accept it as instruments list for legacy reason
if len(instruments) > 0 and type(instruments[0]) == list:
ins = instruments[0]
return self._get("market.trigger_range", {
"i": ins,
"transaction_type": transaction_type.lower()
})
def _parse_instruments(self, data):
# decode to string for Python 3
d = data
# Decode unicode 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 len(row["expiry"]) == 10:
row["expiry"] = dateutil.parser.parse(row["expiry"]).date()
records.append(row)
return records
def _parse_mf_instruments(self, data):
# decode to string for Python 3
d = 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 len(row["last_price_date"]) == 10:
row["last_price_date"] = dateutil.parser.parse(row["last_price_date"]).date()
records.append(row)
return records
def _user_agent(self):
return (__title__ + "-python/").capitalize() + __version__
def _get(self, route, params=None):
"""Alias for sending a GET request."""
return self._request(route, "GET", params)
def _post(self, route, params=None):
"""Alias for sending a POST request."""
return self._request(route, "POST", params)
def _put(self, route, params=None):
"""Alias for sending a PUT request."""
return self._request(route, "PUT", params)
def _delete(self, route, params=None):
"""Alias for sending a DELETE request."""
return self._request(route, "DELETE", params)
def _request(self, route, method, parameters=None):
"""Make an HTTP request."""
params = parameters.copy() if parameters else {}
# Form a restful URL
uri = self._routes[route].format(**params)
url = urljoin(self.root, uri)
# Custom headers
headers = {
"X-Kite-Version": "3", # For version 3
"User-Agent": self._user_agent()
}
if self.api_key and self.access_token:
# set authorization header
auth_header = self.api_key + ":" + self.access_token
headers["Authorization"] = "token {}".format(auth_header)
if self.debug:
log.debug("Request: {method} {url} {params} {headers}".format(method=method, url=url, params=params, headers=headers))
try:
r = self.reqsession.request(method,
url,
data=params if method in ["POST", "PUT"] else None,
params=params if method in ["GET", "DELETE"] else None,
headers=headers,
verify=not self.disable_ssl,
allow_redirects=True,
timeout=self.timeout,
proxies=self.proxies)
# Any requests lib related exceptions are raised here - http://docs.python-requests.org/en/master/_modules/requests/exceptions/
except Exception as e:
raise e
if self.debug:
log.debug("Response: {code} {content}".format(code=r.status_code, content=r.content))
# Validate the content type.
if "json" in r.headers["content-type"]:
try:
data = json.loads(r.content.decode("utf8"))
except ValueError:
raise ex.DataException("Couldn't parse the JSON response received from the server: {content}".format(
content=r.content))
# api error
if data.get("error_type"):
# Call session hook if its registered and TokenException is raised
if self.session_expiry_hook and r.status_code == 403 and data["error_type"] == "TokenException":
self.session_expiry_hook()
# native Kite errors
exp = getattr(ex, data["error_type"], ex.GeneralException)
raise exp(data["message"], code=r.status_code)
return data["data"]
elif "csv" in r.headers["content-type"]:
return r.content
else:
raise ex.DataException("Unknown Content-Type ({content_type}) with response: ({content})".format(
content_type=r.headers["content-type"],
content=r.content))