Skip to content

Commit

Permalink
0.9.9.1
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdamusic committed Jul 18, 2022
1 parent 3d2b7e0 commit 5e68103
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- affiliations as a list with GRID links
- generic 'default_transform' method for extra column not in transformations
* Test suite for magic commands
* New feature: `verify_ssl` option for `login()` method

## v 0.9.8

Expand Down
2 changes: 1 addition & 1 deletion dimcli/VERSION.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# !/usr/bin/env python
# -*- coding: UTF-8 -*-

__version__ = "0.9.9" # LATEST? => https://pypi.org/project/dimcli/
__version__ = "0.9.9.1" # LATEST? => https://pypi.org/project/dimcli/
__copyright__ = "CopyRight (C) 2018-2022 by Digital Science"
__license__ = "MIT"
__author__ = "Michele Pasin"
Expand Down
13 changes: 10 additions & 3 deletions dimcli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def login( username="",
endpoint="",
instance="",
key="",
verify_ssl=True,
verbose=True):
"""Login into the Dimensions API and store the query token in memory.
Expand All @@ -68,6 +69,8 @@ def login( username="",
The instance name, from the local dsl.ini credentials file. Default: 'live'
key: str, optional
The API key (available to some users instead of username/password)
verify_ssl: bool, optional
Verify SSL certificates for HTTPS requests. Default: True.
verbose: bool, optional
Verbose mode. Default: True.
Expand All @@ -81,6 +84,10 @@ def login( username="",
* `https://app.dimensions.ai/api/dsl/v2`
About SSL verification:
Dimcli internally uses the Requests library, which verifies SSL certificates for HTTPS requests, just like a web browser. For some users, it is necessary to turn off SSL verification in order to connect to the API. This can be achieved by passing `verify_ssl=False` at login time. All subsequent API queries will not use SSL verification. NOTE This setting can also be added to the `dsl.ini` file with the following line: `verify_ssl=false`.
Example
-------
Expand All @@ -94,11 +101,11 @@ def login( username="",
You can specify endpoint, which by default is set to "https://app.dimensions.ai"
>>> dimcli.login(key="my-secret-key", ednpoint="https://nannies-research.dimensions.ai")
>>> dimcli.login(key="my-secret-key", endpoint="https://nannies-research.dimensions.ai")
Legacy authentication mechanisms with username/password are also supported
>>> dimcli.login(username="mary.poppins", password="chimneysweeper", ednpoint="https://nannies-research.dimensions.ai")
>>> dimcli.login(username="mary.poppins", password="chimneysweeper", endpoint="https://nannies-research.dimensions.ai")
See Also
---------------
Expand All @@ -109,7 +116,7 @@ def login( username="",
from .core.auth import do_global_login, get_global_connection

try:
do_global_login(instance, username, password, key, endpoint)
do_global_login(instance, username, password, key, endpoint, verify_ssl)
except Exception as e:
printDebug("Login failed: please ensure your credentials are correct.")
raise(e)
Expand Down
5 changes: 4 additions & 1 deletion dimcli/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def __init__(self, show_results=False, verbose=True, auth_session=False):
self._verbose = verbose
self._url = None
self._headers = None
self.verify_ssl = True
if auth_session:
self._CONNECTION = auth_session
else:
Expand All @@ -88,6 +89,7 @@ def __init__(self, show_results=False, verbose=True, auth_session=False):
# if already logged in, reuse connection
self._url = self._CONNECTION.url
self._headers = {'Authorization': "JWT " + self._CONNECTION.token}
self.verify_ssl = self._CONNECTION.verify_ssl
else:
self._print_please_login()

Expand All @@ -104,6 +106,7 @@ def _refresh_login(self):
self._CONNECTION.refresh_login()
self._url = self._CONNECTION.url
self._headers = {'Authorization': "JWT " + self._CONNECTION.token}
self.verify_ssl = self._CONNECTION.verify_ssl
else:
printDebug("Warning: please login first.")

Expand Down Expand Up @@ -143,7 +146,7 @@ def query(self, q, show_results=None, retry=0, verbose=None):

# Execute DSL query.
start = time.time()
response = requests.post(self._url, data=q.encode(), headers=self._headers)
response = requests.post(self._url, data=q.encode(), headers=self._headers, verify=self.verify_ssl)
if response.status_code == 429:
# Too Many Requests
printDebug(
Expand Down
18 changes: 14 additions & 4 deletions dimcli/core/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def __init__(self, verbose=True):
self.username = None
self.password = None
self.key = None
self.verify_ssl = None
self.token = None
# self._verbose = verbose

Expand All @@ -83,6 +84,7 @@ def login(self,
password="",
key="",
endpoint="",
verify_ssl=True,
verbose=True):
"""Login into Dimensions API endpoint and get a query token.
Expand All @@ -97,7 +99,8 @@ def login(self,
username="{username}",
password="{password}",
key="{key}",
endpoint="{endpoint}")""", fg="red")
endpoint="{endpoint}",
verify_ssl="{verify_ssl}")""", fg="red")

if (username or password) and not (username and password):
raise Exception("Authentication error: you provided only one value for username and password. Both are required.")
Expand Down Expand Up @@ -139,14 +142,18 @@ def login(self,
key = config_section['key']
except:
key = ""
try:
verify_ssl = config_section.getboolean('verify_ssl')
except:
verify_ssl = True

URL_AUTH, URL_QUERY = self._get_endpoint_urls(endpoint)
# printDebug(URL_AUTH, URL_QUERY )

login_data = {'username': username, 'password': password, 'key': key}

# POST AUTH REQUEST
response = requests.post(URL_AUTH, json=login_data)
response = requests.post(URL_AUTH, json=login_data, verify=verify_ssl)
response.raise_for_status()

token = response.json()['token']
Expand All @@ -156,6 +163,7 @@ def login(self,
self.username = username
self.password = password
self.key = key
self.verify_ssl = verify_ssl
self.token = token


Expand Down Expand Up @@ -209,6 +217,7 @@ def refresh_login(self):
password=self.password,
key=self.key,
endpoint=self.url,
verify_ssl=self.verify_ssl,
verbose=False
)

Expand All @@ -221,6 +230,7 @@ def reset_login(self):
self.password = None
self.key = None
self.token = None
self.verify_ssl = True


def is_logged_in(self):
Expand All @@ -244,10 +254,10 @@ def is_logged_in(self):



def do_global_login(instance="", username="", password="", key="", url=""):
def do_global_login(instance="", username="", password="", key="", url="", verify_ssl=True):
"Login into DSL and set the connection object with token"
global CONNECTION
CONNECTION.login(instance, username, password, key, url)
CONNECTION.login(instance, username, password, key, url, verify_ssl)



Expand Down
17 changes: 17 additions & 0 deletions dimcli/tests/quicktest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ def main(test_number=1):
dsl = Dsl()
test_number = int(test_number)

if test_number == 5:

click.secho("\nTEST 005: GLOBAL login/logout using verify_ssl flag.", bg="green")
# ----
logout()
login(instance="key-test", verify_ssl=True, verbose=True)
d = Dsl()
click.secho(""" Dsl(instance="key-test" / verify_ssl=True): ==> url="""+ d._url, fg="magenta")
res = d.query("""search publications where authors="Pasin" return publications""")
print(" ==> res.json.keys(): ", res.json.keys())
logout()
# ----
click.secho("\n--------\nCOMPLETED", fg="green")





if test_number == 4:

Expand Down
12 changes: 12 additions & 0 deletions dimcli/tests/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ def test_004(self):
click.secho("\n--------\nCOMPLETED", fg="green")


def test_005(self):
click.secho("\nTEST 005: GLOBAL login/logout using verify_ssl flag.", bg="green")
# ----
logout()
login(instance="key-test", verify_ssl=True, verbose=True)
d = Dsl()
click.secho(""" Dsl(instance="key-test" / verify_ssl=True): ==> url="""+ d._url, fg="magenta")
res = d.query("""search publications where authors="Pasin" return publications""")
print(" ==> res.json.keys(): ", res.json.keys())
logout()
# ----
click.secho("\n--------\nCOMPLETED", fg="green")



Expand Down
13 changes: 10 additions & 3 deletions docs/_modules/dimcli/__init__.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ <h1>Source code for dimcli.__init__</h1><div class="highlight"><pre>
<span class="n">endpoint</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">,</span>
<span class="n">instance</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">,</span>
<span class="n">key</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">,</span>
<span class="n">verify_ssl</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
<span class="n">verbose</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Login into the Dimensions API and store the query token in memory. </span>

Expand All @@ -135,6 +136,8 @@ <h1>Source code for dimcli.__init__</h1><div class="highlight"><pre>
<span class="sd"> The instance name, from the local dsl.ini credentials file. Default: &#39;live&#39;</span>
<span class="sd"> key: str, optional</span>
<span class="sd"> The API key (available to some users instead of username/password)</span>
<span class="sd"> verify_ssl: bool, optional</span>
<span class="sd"> Verify SSL certificates for HTTPS requests. Default: True.</span>
<span class="sd"> verbose: bool, optional</span>
<span class="sd"> Verbose mode. Default: True.</span>

Expand All @@ -148,6 +151,10 @@ <h1>Source code for dimcli.__init__</h1><div class="highlight"><pre>
<span class="sd"> * `https://app.dimensions.ai/api/dsl/v2`</span>


<span class="sd"> About SSL verification:</span>

<span class="sd"> Dimcli internally uses the Requests library, which verifies SSL certificates for HTTPS requests, just like a web browser. For some users, it is necessary to turn off SSL verification in order to connect to the API. This can be achieved by passing `verify_ssl=False` at login time. All subsequent API queries will not use SSL verification. NOTE This setting can also be added to the `dsl.ini` file with the following line: `verify_ssl=false`.</span>


<span class="sd"> Example</span>
<span class="sd"> -------</span>
Expand All @@ -161,11 +168,11 @@ <h1>Source code for dimcli.__init__</h1><div class="highlight"><pre>

<span class="sd"> You can specify endpoint, which by default is set to &quot;https://app.dimensions.ai&quot;</span>
<span class="sd"> </span>
<span class="sd"> &gt;&gt;&gt; dimcli.login(key=&quot;my-secret-key&quot;, ednpoint=&quot;https://nannies-research.dimensions.ai&quot;)</span>
<span class="sd"> &gt;&gt;&gt; dimcli.login(key=&quot;my-secret-key&quot;, endpoint=&quot;https://nannies-research.dimensions.ai&quot;)</span>

<span class="sd"> Legacy authentication mechanisms with username/password are also supported</span>

<span class="sd"> &gt;&gt;&gt; dimcli.login(username=&quot;mary.poppins&quot;, password=&quot;chimneysweeper&quot;, ednpoint=&quot;https://nannies-research.dimensions.ai&quot;)</span>
<span class="sd"> &gt;&gt;&gt; dimcli.login(username=&quot;mary.poppins&quot;, password=&quot;chimneysweeper&quot;, endpoint=&quot;https://nannies-research.dimensions.ai&quot;)</span>

<span class="sd"> See Also</span>
<span class="sd"> ---------------</span>
Expand All @@ -176,7 +183,7 @@ <h1>Source code for dimcli.__init__</h1><div class="highlight"><pre>
<span class="kn">from</span> <span class="nn">.core.auth</span> <span class="kn">import</span> <span class="n">do_global_login</span><span class="p">,</span> <span class="n">get_global_connection</span>

<span class="k">try</span><span class="p">:</span>
<span class="n">do_global_login</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">username</span><span class="p">,</span> <span class="n">password</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">endpoint</span><span class="p">)</span>
<span class="n">do_global_login</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">username</span><span class="p">,</span> <span class="n">password</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">endpoint</span><span class="p">,</span> <span class="n">verify_ssl</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
<span class="n">printDebug</span><span class="p">(</span><span class="s2">&quot;Login failed: please ensure your credentials are correct.&quot;</span><span class="p">)</span>
<span class="k">raise</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
Expand Down
5 changes: 4 additions & 1 deletion docs/_modules/dimcli/core/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ <h1>Source code for dimcli.core.api</h1><div class="highlight"><pre>
<span class="bp">self</span><span class="o">.</span><span class="n">_verbose</span> <span class="o">=</span> <span class="n">verbose</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_url</span> <span class="o">=</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_headers</span> <span class="o">=</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">verify_ssl</span> <span class="o">=</span> <span class="kc">True</span>
<span class="k">if</span> <span class="n">auth_session</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_CONNECTION</span> <span class="o">=</span> <span class="n">auth_session</span>
<span class="k">else</span><span class="p">:</span>
Expand All @@ -155,6 +156,7 @@ <h1>Source code for dimcli.core.api</h1><div class="highlight"><pre>
<span class="c1"># if already logged in, reuse connection </span>
<span class="bp">self</span><span class="o">.</span><span class="n">_url</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_CONNECTION</span><span class="o">.</span><span class="n">url</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_headers</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;Authorization&#39;</span><span class="p">:</span> <span class="s2">&quot;JWT &quot;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">_CONNECTION</span><span class="o">.</span><span class="n">token</span><span class="p">}</span>
<span class="bp">self</span><span class="o">.</span><span class="n">verify_ssl</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_CONNECTION</span><span class="o">.</span><span class="n">verify_ssl</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_print_please_login</span><span class="p">()</span>

Expand All @@ -171,6 +173,7 @@ <h1>Source code for dimcli.core.api</h1><div class="highlight"><pre>
<span class="bp">self</span><span class="o">.</span><span class="n">_CONNECTION</span><span class="o">.</span><span class="n">refresh_login</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_url</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_CONNECTION</span><span class="o">.</span><span class="n">url</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_headers</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;Authorization&#39;</span><span class="p">:</span> <span class="s2">&quot;JWT &quot;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">_CONNECTION</span><span class="o">.</span><span class="n">token</span><span class="p">}</span>
<span class="bp">self</span><span class="o">.</span><span class="n">verify_ssl</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_CONNECTION</span><span class="o">.</span><span class="n">verify_ssl</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">printDebug</span><span class="p">(</span><span class="s2">&quot;Warning: please login first.&quot;</span><span class="p">)</span>

Expand Down Expand Up @@ -210,7 +213,7 @@ <h1>Source code for dimcli.core.api</h1><div class="highlight"><pre>

<span class="c1"># Execute DSL query.</span>
<span class="n">start</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
<span class="n">response</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_url</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">q</span><span class="o">.</span><span class="n">encode</span><span class="p">(),</span> <span class="n">headers</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_headers</span><span class="p">)</span>
<span class="n">response</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_url</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">q</span><span class="o">.</span><span class="n">encode</span><span class="p">(),</span> <span class="n">headers</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_headers</span><span class="p">,</span> <span class="n">verify</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">verify_ssl</span><span class="p">)</span>
<span class="k">if</span> <span class="n">response</span><span class="o">.</span><span class="n">status_code</span> <span class="o">==</span> <span class="mi">429</span><span class="p">:</span>
<span class="c1"># Too Many Requests</span>
<span class="n">printDebug</span><span class="p">(</span>
Expand Down
Loading

0 comments on commit 5e68103

Please sign in to comment.