alpha-xone / xbbg

An intuitive Bloomberg API
https://xbbg.readthedocs.io/
Apache License 2.0
246 stars 52 forks source link

add alt_connect() to core/conn.py to support alternative connection #28

Closed anxl2008 closed 3 years ago

anxl2008 commented 3 years ago

Added alt_connect() function in core/conn.py to support different authentication methods to connect to blpapi. If a user is simply connecting with desktop api (the default method), then there will be no need to call alt_connect(). If a user is using other authentication method (Sever API/ anything else), then user can call alt_connect first to create the connection.

I am only able to tested this with bloomberg Server API authentication and it's working. For completeness, I also adapted other authentication methods listed in blpapi's sample script: https://github.com/msitt/blpapi-python/blob/master/examples/ConnectionAndAuthExample.py (further testing will be required.)

codecov[bot] commented 3 years ago

Codecov Report

Merging #28 (64d3f3e) into master (5a0b5f4) will not change coverage. The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff            @@
##            master       #28   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            9         9           
  Lines          415       415           
=========================================
  Hits           415       415           

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 5a0b5f4...64d3f3e. Read the comment docs.

alpha-xone commented 3 years ago

Is this working in your local environment? Looks like the method not returning anything.

anxl2008 commented 3 years ago

Yes, this works for me. Originally in your code, the call to conn.bbg_session() is wrapped inside bdh/bds/bdp functions to take care of connection when querying the data. alt_connect() essentially is try to doing the same thing but without specifically initiating a data query.

Ultimately, the "globals()[con_sym] = connect_bbg(**kwargs)" line in conn.bbg_session() will assign the blpapi.session object to global namespace, which is then accessible to subsequence call. So there is no need to return anything in alt_connect().

With this change, the alt_connect() call act as a "login" step for alternative authentication method: from xbbg.conn import alt_connect from xbbg import blp alt.connect(auth_method = 'app', app_name = 'some_app_name', server_host = 'xxx.xxx.x.xx') df = blp.bds(...)

Without this change, say I need to make a bds call in for loop, one will either:

  1. make a trivial query to login
  2. or use an if statement to see if this is the first function query
  3. or pass an session object every time when calling the query function (Any of above is not quite pythonic.) df = blp.bds(..., sess = session)
alpha-xone commented 3 years ago

Got you. Will merge your request and do some renaming to make it more convenient to use.

alpha-xone commented 3 years ago

Appreciated for your contribution

anxl2008 commented 3 years ago

You're welcome. Do whatever renaming needed to make it better. This is a really useful package.