jrxFive / python-nomad

Client library Hashicorp Nomad
https://python-nomad.readthedocs.io/en/latest/
MIT License
139 stars 73 forks source link

Add support for pre-populated Sessions #132

Closed minor-fixes closed 1 year ago

minor-fixes commented 2 years ago

This PR allows the caller to inject a requests.Session object that the Nomad client should use to make all requests, in case the client is used in an environment where cookies/headers/etc. unrelated to Nomad communication must be added to each request.


Tested - both examples below with this change:

Something like this in my environment fails, as it redirects to an auth server: (URLs and other bits changed/redacted below)

import nomad

def main():
    n = nomad.Nomad(host="nomad.corp.example.com", port=443, secure=True, verify=True)
    print("num jobs:", len(n.jobs.get_jobs()))

if __name__ == "__main__":
    main()

prints:

Traceback (most recent call last):
  File "client_test_fail.py", line 8, in <module>
    main()
  File "client_test_fail.py", line 5, in main
    print("num jobs:", len(n.jobs.get_jobs()))
  File "/home/scott/dev/python-nomad/nomad/api/jobs.py", line 84, in get_jobs
    return self.request(method="get", params=params).json()
  File "/usr/lib/python3/dist-packages/requests/models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

whereas preparing and supplying a session:

import requests
import nomad

def main():
    token = "<redacted>"
    s = requests.session()
    s.cookies.set("Creds", token, domain=".corp.example.com")
    with s:
        n = nomad.Nomad(host="nomad.corp.example.com", port=443, secure=True, verify=True, session=s)
        print("num jobs:", len(n.jobs.get_jobs()))

if __name__ == "__main__":
    main()

prints: num jobs: 20

codecov-commenter commented 2 years ago

Codecov Report

Merging #132 (a0c6907) into master (1cc1077) will decrease coverage by 0.22%. The diff coverage is 92.78%.

@@            Coverage Diff             @@
##           master     #132      +/-   ##
==========================================
- Coverage   90.61%   90.38%   -0.23%     
==========================================
  Files          26       27       +1     
  Lines        1065     1103      +38     
==========================================
+ Hits          965      997      +32     
- Misses        100      106       +6     
Impacted Files Coverage Δ
nomad/api/base.py 96.55% <85.71%> (-1.04%) :arrow_down:
nomad/api/event.py 86.66% <86.66%> (ø)
nomad/__init__.py 97.89% <100.00%> (-0.34%) :arrow_down:
nomad/api/__init__.py 100.00% <100.00%> (ø)
nomad/api/exceptions.py 100.00% <100.00%> (ø)
nomad/api/job.py 94.59% <100.00%> (+0.22%) :arrow_up:
nomad/api/jobs.py 90.00% <100.00%> (+0.41%) :arrow_up:
nomad/api/agent.py 92.85% <0.00%> (-7.15%) :arrow_down:
nomad/api/node.py 87.69% <0.00%> (+3.07%) :arrow_up:
... and 1 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

jonathanrcross commented 2 years ago

Hey @minor-fixes, thanks for the PR! The tests are passing, seems like some issue with the secret to the test pypi (even though the secret looks correct) thats causing the failure. I'll have to figure that out to get this out for a release.

jrxFive commented 1 year ago

Thanks @minor-fixes!