Optimizing api_instance Management in Upstox Python Client

Hello everyone,

I’m integrating the Upstox Python Client in my project and have a few questions about managing api_instance efficiently. Currently, my approach involves creating a new api_instance for each function that makes an API call, as shown below:

import upstox_client

def get_positions(api_version, configuration):
    api_instance = upstox_client.PortfolioApi(
        upstox_client.ApiClient(configuration))
    return api_instance.get_positions(api_version)

def convert_positions(api_version, configuration, body):
    api_instance = upstox_client.PortfolioApi(
        upstox_client.ApiClient(configuration))
    return api_instance.convert_positions(body, api_version)

def get_holdings(api_version, configuration):
    api_instance = upstox_client.PortfolioApi(
        upstox_client.ApiClient(configuration))
    return api_instance.get_holdings(api_version)

I am seeking advice on the following:

  1. Instance Creation: Is it advisable to create a new api_instance for each API call?
  2. Performance Impact: What are the performance implications of creating multiple api_instance objects?
  3. Best Practices: Should I reuse a single api_instance across different API calls, and if so, how can I do this effectively?

Any guidance or examples of best practices would be greatly appreciated!

1.Creating a new api_instance for each API call, is generally not the most efficient approach. This may negatively impact performance, especially if you are making a large number of API calls. It is advisable to create an api_instance once and reuse it for multiple API calls.
2.Each instance creation involves network setup, memory allocation, and other resource-intensive operations that can slow down your application.
3. You can use something like this

class UpstoxClient:
    def __init__(self, configuration):
        self.api_client = upstox_client.ApiClient(configuration)
        self.portfolio_api = upstox_client.PortfolioApi(self.api_client)

    def get_positions(self, api_version):
        return self.portfolio_api.get_positions(api_version)

    def convert_positions(self, api_version, body):
        return self.portfolio_api.convert_positions(body, api_version)

    def get_holdings(self, api_version):
        return self.portfolio_api.get_holdings(api_version)

Thanks a lot. Yes, I was thinking on same lines. But I didn’t find any good examples where the api_instance being reused. So I was confused. Your reply helped to clear it.
Thankyou once again :smile: .

1 Like

I extended on the idea @fallen_coconut shared and created a class with all the upstox API, if in case some one needs.

import upstox_client


class UpstoxApi:

    def __init__(self, api_version, access_token):
        self.api_version = api_version
        self.configuration = upstox_client.Configuration()
        self.configuration.access_token = access_token
        self.api_client = upstox_client.ApiClient(self.configuration)

        self.login_api = upstox_client.LoginApi(self.api_client)
        self.user_api = upstox_client.UserApi(self.api_client)
        self.charge_api = upstox_client.ChargeApi(self.api_client)
        self.order_api = upstox_client.OrderApi(self.api_client)
        self.portfolio_api = upstox_client.PortfolioApi(self.api_client)
        self.trade_profit_and_loss_api = upstox_client.TradeProfitAndLossApi(self.api_client)
        self.history_api = upstox_client.HistoryApi(self.api_client)
        self.market_quote_api = upstox_client.MarketQuoteApi(self.api_client)
        self.websocket_api = upstox_client.WebsocketApi(self.api_client)

    # LoginApi
    def authorize(self, client_id, redirect_uri, **kwargs):
        return self.login_api.authorize(client_id, redirect_uri, self.api_version, **kwargs)

    def logout(self, **kwargs):
        return self.login_api.logout(self.api_version, **kwargs)

    def token(self, **kwargs):
        return self.login_api.token(self.api_version, **kwargs)

    # UserApi
    def get_profile(self, **kwargs):
        return self.user_api.get_profile(self.api_version, **kwargs)

    def get_user_fund_margin(self, **kwargs):
        return self.user_api.get_user_fund_margin(self.api_version, **kwargs)

    # ChargeApi
    def get_brokerage(self, instrument_token, quantity, product, transaction_type, price, **kwargs):
        return self.charge_api.get_brokerage(instrument_token, quantity, product, transaction_type, price,
                                             self.api_version, **kwargs)

    # OrderApi
    def cancel_order(self, order_id, **kwargs):
        return self.order_api.cancel_order(order_id, self.api_version, **kwargs)

    def get_order_book(self, **kwargs):
        return self.order_api.get_order_book(self.api_version, **kwargs)

    def get_order_details(self, **kwargs):
        return self.order_api.get_order_details(self.api_version, **kwargs)

    def get_trade_history(self, **kwargs):
        return self.order_api.get_trade_history(self.api_version, **kwargs)

    def get_trades_by_order(self, order_id, **kwargs):
        return self.order_api.get_trades_by_order(order_id, self.api_version, **kwargs)

    def modify_order(self, body, **kwargs):
        return self.order_api.modify_order(body, self.api_version, **kwargs)

    def place_order(self, body, **kwargs):
        return self.order_api.place_order(body, self.api_version, **kwargs)

    # PortfolioApi
    def convert_positions(self, body, **kwargs):
        return self.portfolio_api.convert_positions(body, self.api_version, **kwargs)

    def get_holdings(self, **kwargs):
        return self.portfolio_api.get_holdings(self.api_version, **kwargs)

    def get_positions(self, **kwargs):
        return self.portfolio_api.get_positions(self.api_version, **kwargs)

    # TradeProfitAndLossApi
    def get_profit_and_loss_charges(self, segment, financial_year, **kwargs):
        return self.trade_profit_and_loss_api.get_profit_and_loss_charges(segment, financial_year, self.api_version,
                                                                          **kwargs)

    def get_trade_wise_profit_and_loss_data(self, segment, financial_year, page_number, page_size, **kwargs):
        return self.trade_profit_and_loss_api.get_trade_wise_profit_and_loss_data(segment, financial_year, page_number,
                                                                                  page_size, self.api_version, **kwargs)

    def get_trade_wise_profit_and_loss_meta_data(self, segment, financial_year, **kwargs):
        return self.trade_profit_and_loss_api.get_trade_wise_profit_and_loss_meta_data(segment, financial_year,
                                                                                       self.api_version, **kwargs)

    # HistoryApi
    def get_historical_candle_data(self, instrument_key, interval, to_date, **kwargs):
        return self.history_api.get_historical_candle_data(instrument_key, interval, to_date, self.api_version,
                                                           **kwargs)

    def get_historical_candle_data1(self, instrument_key, interval, to_date, from_date, **kwargs):
        return self.history_api.get_historical_candle_data1(instrument_key, interval, to_date, from_date,
                                                            self.api_version, **kwargs)

    def get_intra_day_candle_data(self, instrument_key, interval, **kwargs):
        return self.history_api.get_intra_day_candle_data(instrument_key, interval, self.api_version, **kwargs)

    # MarketQuoteApi
    def get_full_market_quote(self, symbol, **kwargs):
        return self.market_quote_api.get_full_market_quote(symbol, self.api_version, **kwargs)

    def get_market_quote_ohlc(self, symbol, interval, **kwargs):
        return self.market_quote_api.get_market_quote_ohlc(symbol, interval, self.api_version, **kwargs)

    def ltp(self, symbol, **kwargs):
        return self.market_quote_api.ltp(symbol, self.api_version, **kwargs)

    # WebsocketApi
    def get_market_data_feed(self, **kwargs):
        return self.websocket_api.get_market_data_feed(self.api_version, **kwargs)

    def get_market_data_feed_authorize(self, **kwargs):
        return self.websocket_api.get_market_data_feed_authorize(self.api_version, **kwargs)

    def get_portfolio_stream_feed(self, **kwargs):
        return self.websocket_api.get_portfolio_stream_feed(self.api_version, **kwargs)

    def get_portfolio_stream_feed_authorize(self, **kwargs):
        return self.websocket_api.get_portfolio_stream_feed_authorize(self.api_version, **kwargs)