Kotak-Neo / kotak-neo-api

113 stars 105 forks source link

Login on every API call? #43

Closed shusinghal closed 1 year ago

shusinghal commented 1 year ago

Hi. Your docs suggest to use 'client' object before calling any method (place oder, modify, cancel, etc.)

Does this mean that I need to login again and again every single time? If yes, why did you opt this approach?

If I don't use your github code and rely only on API documentation, then I can create session one time and run x number of methods.

Like in your API documentation, can you allow making calls by passing token, sid, server? Or can you add API doc for quotes in postman?

ShivdasBachewar commented 1 year ago

it seems they have some session time, which is default to 3600 seconds (1 hr). we can update that and we need not have to login again for specified period. https://napi.kotaksecurities.com/devportal/applications/

note: I was also facing same issue, I am also trying the same now. after try please add your views

shusinghal commented 1 year ago

Hi Shidas,

client.modify_order(order_id = "", price = 0, quantity = 1, disclosed_quantity = 0, trigger_price = 0, validity = "GFD")

To use modify order, I need client object. This object is initiating the login process. If I have logged in one time, I should not require to login again till session timeout.

syedasif11 commented 1 year ago

@shusinghal That may be because you are calling client.login(mobilenumber=neo_creds.mobile_num, password=neo_creds.password) again. This is what I am doing: Ex.,


neo_object = None

def get_neo_client():
    if neo_object is not None:
        return neo_object
    # Normal login process
    client = NeoAPI(access_token=neo_creds.accesstoken.strip(), consumer_key=neo_creds.consumerkey.strip(),
                    consumer_secret=neo_creds.consumer_secret.strip(), environment='prod', on_close=None, on_open=None)
    .....
    .....
    # Once session is generated, set neo_object
    neo_object = client
ShivdasBachewar commented 1 year ago

shusinghal

As per my analysis, once you are logged in to kotak api and generated session, generated session is used in all your subsequent request. Library is not doing re-login

`class ModifyOrder(object): def init(self, api_client): self.api_client = api_client ## already logged in session self.rest_client = rest.RESTClientObject(api_client.configuration)

def quick_modification(self, order_id, price, order_type, quantity, validity, instrument_token,
                       exchange_segment, product, trading_symbol, transaction_type, trigger_price,
                       dd, market_protection, disclosed_quantity, filled_quantity, amo):
    header_params = {'Authorization': "Bearer " + self.api_client.configuration.bearer_token,
                     "Sid": self.api_client.configuration.edit_sid,
                     "Auth": self.api_client.configuration.edit_token,
                     "neo-fin-key": self.api_client.configuration.get_neo_fin_key(),
                     "Content-Type": "application/x-www-form-urlencoded"}

    body_params = {"tk": instrument_token, "mp": market_protection, "pc": product, "dd": dd,
                   "dq": disclosed_quantity, "vd": validity, "ts": trading_symbol, "tt": transaction_type,
                   "pr": price, "pt": order_type, "fq": filled_quantity, 'am': amo,
                   "tp": trigger_price, "qt": quantity, "no": order_id, "es": exchange_segment}

    query_params = {"sId": self.api_client.configuration.serverId}
    try:
        URL = self.api_client.configuration.get_url_details("modify_order")
        orders_resp = self.rest_client.request(
            url=URL, method='POST',
            query_params=query_params,
            headers=header_params,
            body=body_params
        )

        return orders_resp.json()

    except ApiException as ex:
        return {"error": ex}`
Kotak-Neo commented 1 year ago

@shusinghal The library is not re-logging while calling any method. Please debug your code to rectify this issue.