Repository: python-gitlab/python-gitlab

Issue:
methods deprecation
I've had a report for deprecation warnings in git-repo when using gitlab. I then looked at the release notes, and found out that a bunch of the `Gitlab` object's API is getting deprecated.

Though, I actually need [those](https://github.com/python-gitlab/python-gitlab/issues/280#issuecomment-310841660) in [git-repo](https://github.com/guyzmo/git-repo/blob/devel/git_repo/services/ext/gitlab.py#L34,L36) where I initialise objects in two steps, first I initialise the instances (and importantly request's session) then I load the configuration, and finally the connect method configures the gitlab object.

So either having a method to configure the object after it has been instanciated (with an API similar to the constructor), or keeping the setters really matter to me! Or I'll end up forced to monkey patched internals of `python-gitlab` and that can end up ugly 😖

cf [this issue](https://github.com/guyzmo/git-repo/issues/166)

Thank you for your lib ♥


Discussion:
Hi @guyzmo :)

Could you explain a little more why you need to instantiate the request session first? I'm trying to understand the use case.

My idea with the change is that it doesn't really make sense to have a gitlab object without its authentication and URL parameters. But maybe I missed something...

Thanks!
the process is the following:

- instanciate the `Gitlab` object (along with the `Session` object)
- read configuration files, use the CLI arguments as well to configure both gitlab and session
- and finally initiate the connection

Fundamentally, the reason I need to have the session the earliest is because of [betamax(https://github.com/sigmavirus24/betamax) which hijacks the requests session, at construction time.

So if you really want to go the way you're thinking about, then add a `requests.Session` dependency injection (if that's not already the case).

However you think about it, you're not supposed to make a request to the gitlab server before I  explicitely issue a `.auth()` request (to control when it happens), so what real change would it make to have setters that configures like the constructor or doing it incrementally between instantiation and first request?

(added a link to the code where I use it in the issue)

https://github.com/guyzmo/git-repo/blob/devel/git_repo/services/ext/gitlab.py#L34,L36
I see! I think you don't need the deprecated methods: https://github.com/gpocentek/git-repo/tree/python-gitlab/280

Let me know if this looks OK.
cf [my comments](https://github.com/gpocentek/git-repo/commit/9ec7a38f9258e20667fae7ac1e346adf04968dbd#diff-6cda266ab3e820bbb573c2ac29a9844eR23) your PR won't pass the tests, as I won't have access to `self.gl.session` anymore at the time the constructor is called, and before the `connect()` method is called.

So it's either dependency injection (with the `session=` parameter to python-gitlab's constructor) or it's the way it was ☺

BTW, see [how the same thing is done](https://github.com/gpocentek/git-repo/blob/devel/git_repo/services/ext/github.py#L22,L37) with sigmavirus24/github3.py.

and [the hack](https://github.com/gpocentek/git-repo/blob/devel/git_repo/services/ext/gogs.py#L20,L25) I had to do to improve the python-gogs lib for my needs, [used there](https://github.com/gpocentek/git-repo/blob/devel/git_repo/services/ext/gogs.py#L78,L91).
So, how do you think shall be the way ahead? Dependency injection of the `requests.Session()` would be Ok?
basically, have something like that, [in the gitlab constructor](https://github.com/python-gitlab/python-gitlab/blob/master/gitlab/__init__.py#L93):

```python
    def __init__(self, url, private_token=None, email=None, password=None,
                 ssl_verify=True, http_username=None, http_password=None,
                 timeout=None, session=None, api_version='3'):
        # …
        #: Create a session object for requests
        self.session = session or requests.Session()
        # …
```

Resolving patch:
diff --git a/gitlab/__init__.py b/gitlab/__init__.py
--- a/gitlab/__init__.py
+++ b/gitlab/__init__.py
@@ -71,7 +71,7 @@ class Gitlab(object):
 
     def __init__(self, url, private_token=None, email=None, password=None,
                  ssl_verify=True, http_username=None, http_password=None,
-                 timeout=None, api_version='3'):
+                 timeout=None, api_version='3', session=None):
 
         self._api_version = str(api_version)
         self._url = '%s/api/v%s' % (url, api_version)
@@ -90,7 +90,7 @@ def __init__(self, url, private_token=None, email=None, password=None,
         self.http_password = http_password
 
         #: Create a session object for requests
-        self.session = requests.Session()
+        self.session = session or requests.Session()
 
         objects = importlib.import_module('gitlab.v%s.objects' %
                                           self._api_version)


Test patch: