simon-weber / gmusicapi

An unofficial client library for Google Music.
https://unofficial-google-music-api.readthedocs.io
BSD 3-Clause "New" or "Revised" License
2.48k stars 258 forks source link

Expose Requests session to library user #366

Open jodal opened 8 years ago

jodal commented 8 years ago

gmusic.session wraps a single Requests session that is used for all HTTP requests. As a gmusicapi library user, there's currently no way to get hold of the Requests session object without fiddling with gmusicapi internals. Getting hold of the Requests sessions is useful for e.g. configuring an HTTP proxy.

The following is a somewhat fragile (and untested) workaround:

import gmusicapi
import gmusicapi.session

def my_rsession_setup(rsession):
    # Do stuff with Requests session here

def my_rsession_setup_wrapper(original_rsession_setup):
    def rsession_setup(rsession):
        if original_rsession_setup is not None:
            original_rsession_setup(rsession)
        my_rsession_setup(rsession)
    return rsession_setup

class MyMobileClientSession(gmusicapi.session.Mobileclient):
    def __init__(self, rsession_setup=None):
        super(MyMobileClientSession, self).__init__(
            rsession_setup=my_rsession_setup_wrapper(rsession_setup))

class MyMobileClient(gmusicapi.Mobileclient):
    _session_class = MyMobileClientSession

api = MyMobileClient(...)
api.login(...)
# ...and so on
simon-weber commented 8 years ago

Agree this would be useful.

Is exposing resession_setup what we want? I'm not actually sure if it's used internally anymore, so if there's a better interface that comes at the cost of some refactoring, I'd be happy to look into that too.

belak commented 8 years ago

Has there been any more thought put towards this? It would be really nice to have access to the underlying requests session for mopidy-gmusic, specifically for proxy settings.

simon-weber commented 8 years ago

Hey, sorry, I haven't looked at this in a while. I'd be happy to look over a PR for it, though.

J-tt commented 7 years ago

Hey is this issue still active? It would be awesome to be able to use gmusicapi at school (need to specify a proxy), and messing with the internals is something I would rather not do 👍

simon-weber commented 7 years ago

Haha, kind of: active in the sense that it seems useful, but I don't expect to find the time for it myself.

I think you may be able to work around this by specifying setting the https_proxy environment variable, though. Recent versions of requests will handle it without client changes, and I don't think gmusicapi does any non-requests http interactions.

J-tt commented 7 years ago

It will fetch the song information, but streaming throws an error (with https_proxy)

simon-weber commented 7 years ago

What about if you set http_proxy as well? Steaming probably redirects to an http url eventually.