Skip to content

Commit

Permalink
update the way we handle contexts so that we just have a single clien…
Browse files Browse the repository at this point in the history
…t imported once in a import statement
  • Loading branch information
eak24 committed Sep 22, 2018
1 parent f954698 commit 1de555a
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 78 deletions.
5 changes: 2 additions & 3 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ Configuration From Python

.. code-block:: python
from onshapepy.client import Client
from onshapepy.client import c
configuration = {"creds" :{
"access_key" : "*******YOUR API KEY*******",
"secret_key" : "*******YOUR API SECRET****"}}
Client(configuration)
c.update(configuration)
.. _configuration_options:

Expand Down
3 changes: 1 addition & 2 deletions onshapepy/assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"""

from onshapepy.uri import Uri
from onshapepy.core.context import Context
import json
from onshapepy.core.client import c


class Assembly:
Expand Down Expand Up @@ -34,6 +34,5 @@ def insert(self, part):
"""
params = {k: str(v) for k,v in part.params.items()}
c = Context().client
res=c.create_assembly_instance(self.uri.as_dict(), part.uri.as_dict(), params)
return res
11 changes: 5 additions & 6 deletions onshapepy/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

yaml = YAML()


class Client():
class Client:
'''
Defines methods for testing the Onshape API. Comes with several methods:
Expand Down Expand Up @@ -60,10 +59,6 @@ def __init__(self, conf=None, conf_file=".onshapepy"):
if conf:
default_conf.update(conf)

if not default_conf['creds']:
raise AssertionError("Please specify your OnShape acces_key and secret_key within the onshapepy "
"configuration file, by default located at 'home/.onshapepy'.")

self.conf = default_conf

self._stack = default_conf['stack']
Expand Down Expand Up @@ -431,3 +426,7 @@ def element_list(self, uri):

def get_versions(self, uri):
return self._api.request('get', '/api/documents/d/' + uri["did"] + '/versions')


# Start the default client. To change the client, just change this value.
c=Client()
32 changes: 0 additions & 32 deletions onshapepy/core/context.py

This file was deleted.

8 changes: 6 additions & 2 deletions onshapepy/core/onshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ def __init__(self, stack, creds, logging):
'''

self._url = stack
self._access_key = creds['access_key'].encode('utf-8')
self._secret_key = creds['secret_key'].encode('utf-8')
try:
self._access_key = creds['access_key'].encode('utf-8')
self._secret_key = creds['secret_key'].encode('utf-8')
except TypeError as e:
raise UserWarning("Specify a correct access key and secret key for the client")

self._logging = logging

if self._logging:
Expand Down
6 changes: 1 addition & 5 deletions onshapepy/document.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Represents an Onshape document"""
from onshapepy.core.context import Context
from onshapepy.uri import Uri
from onshapepy.core.utils import ElementType
from onshapepy.core.client import c


class Document:
Expand All @@ -16,7 +16,6 @@ def __init__(self, url, copy=False, name=None, version=None):
- version (str): The Onshape version name of the version to be copied or referenced.
"""
c = Context().client

self.json = None
self.e_list = None
Expand Down Expand Up @@ -50,12 +49,10 @@ def __init__(self, url, copy=False, name=None, version=None):

def delete(self):
"""Delete the Onshape document"""
c = Context().client
c.del_document(self.uri.did)

def update(self):
"""All client calls to update this instance with Onshape."""
c = Context().client
self.json = c.get_document(self.uri.did).json()
self.e_list = c.element_list(self.uri.as_dict()).json()

Expand All @@ -73,7 +70,6 @@ def find_element(self, name, type=ElementType.ANY):
Returns:
- onshapepy.uri of the element
"""
c = Context().client

for e in self.e_list:
# if a type is specified and this isn't it, move to the next loop.
Expand Down
12 changes: 4 additions & 8 deletions onshapepy/part.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
OnShape part that maps to a part studio
'''

from onshapepy.core.client import Client
from onshapepy.core.context import Context
from onshapepy.core.client import c
from onshapepy.core.utils import parse_quantity
import json
from onshapepy.uri import Uri
Expand Down Expand Up @@ -70,10 +69,9 @@ def __init__(self, parent):
def payload(self):
return json.loads(self.res.content.decode("utf-8"))

def update(self, params=None):
def update(self, params=None, client=c):
"""Push params to OnShape and synchronize the local copy
"""
c = Context().client
uri = self.parent.uri
if not params or not self.res:
self.get_params()
Expand All @@ -89,7 +87,7 @@ def update(self, params=None):
except KeyError:
m["value"] = str(v)

res = c.update_configuration(uri.did, uri.wvm, uri.eid, json.dumps(d))
res = client.update_configuration(uri.did, uri.wvm, uri.eid, json.dumps(d))

# If it was a good request, update config to be consistent with online.
if res.status_code == 200:
Expand All @@ -99,8 +97,7 @@ def get_params(self):
"""Manually pull params defined in config from OnShape and return a python representation of the params.
Quantities are converted to pint quantities, Bools are converted to python bools and Enums are converted
to strings. Note that Enum names are autogenerated by OnShape and do not match the name on the OnShape UI."""
client = Context().client
self.res = client.get_configuration(self.parent.uri.as_dict())
self.res = c.get_configuration(self.parent.uri.as_dict())

@property
def params(self):
Expand Down Expand Up @@ -148,7 +145,6 @@ def __init__(self, parent):

def update(self):
""" Update all local variable names to match OnShape. """
c = Context().client
uri = self.parent.uri
script = r"""
function(context, queries) {
Expand Down
5 changes: 2 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from onshapepy.part import Part
from onshapepy.assembly import Assembly
from onshapepy.document import Document
from onshapepy.core.context import Context
from onshapepy.uri import Uri
from datetime import datetime
from onshapepy.core.client import c


@pytest.fixture
Expand All @@ -25,8 +25,7 @@ def assembly(document):

@pytest.fixture(scope="module")
def client():
return Context().client

return c

@pytest.fixture(scope="module")
def document(client, onshape_version, onshape_document):
Expand Down
16 changes: 0 additions & 16 deletions tests/test_context.py

This file was deleted.

2 changes: 1 addition & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_get_configuration(client):
assert res.status_code == 200

def test_client_without_file():
with pytest.raises(AssertionError):
with pytest.raises(UserWarning):
client = Client(conf_file="This file doesn't exist")

def test_document_setup(document, client):
Expand Down

0 comments on commit 1de555a

Please sign in to comment.