Skip to content

Commit

Permalink
switched update_to() and upload_to() to use named parameters and adde…
Browse files Browse the repository at this point in the history
…d client named parameter. Fix for issues #80 and #81
  • Loading branch information
coleslaw481 committed May 1, 2021
1 parent b960d90 commit 2cd781f
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 16 deletions.
108 changes: 92 additions & 16 deletions ndex2/nice_cx_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -1474,66 +1474,142 @@ def to_cx_stream(self):

return return_bytes

def upload_to(self, server, username, password,
user_agent=''):
def upload_to(self, server=None, username=None, password=None,
user_agent='', client=None):
"""
Upload this network as a new network to the specified
NDEx `server` using account credentials set via `username`
and `password.` Note, networks with duplicate names are allowed.
Upload this network as a new network on NDEx server.
.. versionchanged:: 3.4.0
This method was switched to named arguments and the server and account
credentials can be passed in one of two ways.
Option 1) Set **username** and **password** parameters.
Option 2) Set **client** parameter with valid :py:class:`~ndex2.client.Ndex2` object
.. note::
If **client** parameter is set, **username**, **password**, and **server**
parameters are ignored
Example:
.. code-block:: python
nice_cx.upload_to('public.ndexbio.org', username, password)
import ndex2
nice_cx = ndex2.nice_cx_network.NiceCXNetwork()
nice_cx.create_node('foo')
# using production NDEx server
nice_cx.update_to(username=user_var,
password=password_var)
:param server: The NDEx server to upload the network to.
# if one needs to use alternate NDEx server
nice_cx.update_to(server='public.ndexbio.org',
username=username_var,
password=password_var)
# Option 2, create Ndex2 client object
ndex_client = ndex2.client.Ndex2(username=username_var,
password=password_var)
# using NDEx client object for connection
nice_cx.update_to(client=ndex_client)
:param server: The NDEx server to upload the network to. Leaving unset or `None` will use production
:type server: str
:param username: The username of the account to store the network.
:type username: str
:param password: The password for the account.
:type password: str
:param user_agent: String to append to User-Agent field sent to NDEx REST service
:type user_agent: str
:param client: NDEx2 object with valid credentials. If set **server**, **username**, and **password**
parameters will be ignored.
:type client: :py:class:`~ndex2.client.Ndex2`
:return: The UUID of the network on NDEx.
:rtype: str
"""
ndex = Ndex2(server, username, password, user_agent=user_agent)
if client is not None:
ndex = client
else:
ndex = Ndex2(server, username, password, user_agent=user_agent)
return ndex.save_new_network(self.to_cx())

def upload_new_network_stream(self, server, username, password):
raise Exception('upload_new_network_stream() is no longer supported. Please use upload_to()')

def update_to(self, uuid, server, username, password,
user_agent=''):
def update_to(self, uuid, server=None, username=None, password=None,
user_agent='', client=None):
"""
Replace the network on NDEx `server` with matching NDEx `uuid` with
this network using `username` and `password` account credentials
passed in.
Replace the network on NDEx server with matching NDEx `uuid` with
this network.
.. versionchanged:: 3.4.0
This method was switched to named arguments and the server and account
credentials can be passed in one of two ways.
Option 1) Set **username** and **password** parameters.
Option 2) Set **client** parameter with valid :py:class:`~ndex2.client.Ndex2` object
.. note::
If **client** parameter is set, **username**, **password**, and **server**
parameters are ignored.
Example:
.. code-block:: python
import ndex2
nice_cx = ndex2.nice_cx_network.NiceCXNetwork()
nice_cx.create_node('foo')
# using production NDEx server
nice_cx.update_to('2ec87c51-c349-11e8-90ac-525400c25d22',
'public.ndexbio.org', username, password)
username=user_var,
password=password_var)
# if one needs to use alternate NDEx server
nice_cx.update_to('2ec87c51-c349-11e8-90ac-525400c25d22',
server='public.ndexbio.org',
username=username_var,
password=password_var)
# Option 2, create Ndex2 client object
ndex_client = ndex2.client.Ndex2(username=username_var,
password=password_var)
# using NDEx client object for connection
nice_cx.update_to('2ec87c51-c349-11e8-90ac-525400c25d22',
client=ndex_client)
:param uuid: UUID of the network on NDEx.
:type uuid: str
:param server: The NDEx server to upload the network to.
:param server: The NDEx server to upload the network to. Leaving unset or `None` will use production.
:type server: str
:param username: The username of the account to store the network.
:type username: str
:param password: The password for the account.
:type password: str
:param user_agent: String to append to User-Agent field sent to NDEx REST service
:type user_agent: str
:param client: NDEx2 object with valid credentials. If set **server**, **username**, and **password**
parameters will be ignored.
:type client: :py:class:`~ndex2.client.Ndex2`
:return: Empty string
:rtype: str
"""
cx = self.to_cx()
ndex = Ndex2(server, username, password, user_agent=user_agent)
if client is not None:
ndex = client
else:
ndex = Ndex2(server, username, password, user_agent=user_agent)

if len(cx) > 0:
if cx[len(cx) - 1] is not None:
Expand Down
19 changes: 19 additions & 0 deletions tests/test_nice_cx_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sys
import warnings

from unittest.mock import MagicMock, ANY
import requests_mock
from ndex2 import client
from ndex2.nice_cx_network import NiceCXNetwork
Expand Down Expand Up @@ -445,6 +446,24 @@ def test__str__(self):
net.create_edge(edge_source=0, edge_target=1)
self.assertEqual('nodes: 3 \n edges: 1', str(net))

def test_upload_to_with_client_success(self):
mockclient = MagicMock()
fakeurl = 'http://foo/12345'
mockclient.save_new_network = MagicMock(return_value=fakeurl)
net = NiceCXNetwork()
res = net.upload_to(client=mockclient)
self.assertEqual(fakeurl, res)
mockclient.save_new_network.assert_called_with(net.to_cx())

def test_update_to_with_client_success(self):
mockclient = MagicMock()
mockclient.update_cx_network = MagicMock(return_value='')
net = NiceCXNetwork()
res = net.update_to('myuuid', client=mockclient)
self.assertEqual('', res)

mockclient.update_cx_network.assert_called_with(ANY, 'myuuid')

def test_upload_to_success(self):
with requests_mock.mock() as m:
resurl = client.DEFAULT_SERVER + '/v2/network/asdf'
Expand Down

0 comments on commit 2cd781f

Please sign in to comment.