Example #1
0
    def test_put(self):
        with mock.patch.object(requests, "put"):
            client = Client("key", "secret")
            client._put("/api/")
            requests.put.assert_called()
            args = requests.put.call_args[1]
            self.assertEqual(args["url"], "https://api.shapeways.com/api/v1")
            self.assertIsInstance(args["auth"], OAuth1)
            self.assertEqual(args["params"], None)
            self.assertEqual(args["data"], None)

            requests.put.reset_mock()

            client = Client("key", "secret")
            params = {
                "key": "value",
            }
            client._put("/api/", params=params)
            requests.put.assert_called()
            args = requests.put.call_args[1]
            self.assertEqual(args["url"], "https://api.shapeways.com/api/v1")
            self.assertIsInstance(args["auth"], OAuth1)
            self.assertEqual(args["params"], params)
            self.assertEqual(args["data"], None)

            requests.put.reset_mock()

            client = Client("key", "secret")
            body = "nice body"
            client._put("/api/", body=body)
            requests.put.assert_called()
            args = requests.put.call_args[1]
            self.assertEqual(args["url"], "https://api.shapeways.com/api/v1")
            self.assertIsInstance(args["auth"], OAuth1)
            self.assertEqual(args["params"], None)
            self.assertEqual(args["data"], body)

            requests.put.reset_mock()

            client = Client("key", "secret")
            params = {
                "key": "value",
            }
            body = "nice body"
            client._put("/api/", body=body, params=params)
            requests.put.assert_called()
            args = requests.put.call_args[1]
            self.assertEqual(args["url"], "https://api.shapeways.com/api/v1")
            self.assertIsInstance(args["auth"], OAuth1)
            self.assertEqual(args["params"], params)
            self.assertEqual(args["data"], body)
Example #2
0
    def test_get_price(self):
        client = Client("key", "value")
        params = {
            "volume": 2,
            "area": 2,
            "xBoundMin": 2,
            "xBoundMax": 3,
            "yBoundMin": 2,
            "yBoundMax": 3,
            "zBoundMin": 2,
            "zBoundMax": 3,
        }
        client.get_price(params)
        client._post.assert_called()
        client._post.assert_called_with("/price/", body=json.dumps(params))

        params = {
            "volume": 2,
            "area": 2,
            "xBoundMax": 3,
            "yBoundMax": 3,
            "zBoundMax": 3,
        }
        with self.assertRaises(Exception):
            client.get_price(params)
Example #3
0
 def test_update_model_info(self):
     client = Client("key", "value")
     params = {
         "filkey": "value",
     }
     client.update_model_info(86, params)
     client._put.assert_called()
     client._put.assert_called_with("/models/86/info/",
                                    body=json.dumps(params))
Example #4
0
    def test_add_model_photo(self):
        client = Client("key", "value")
        params = {
            "file": "<FILE DATA>",
        }
        client.add_model_photo(86, params)
        client._post.assert_called()
        client._post.assert_called_with("/models/86/photos/",
                                        body=json.dumps(params))

        with self.assertRaises(Exception):
            client.add_model_photo(86, {})
Example #5
0
    def test_add_to_cart(self):
        client = Client("key", "value")
        params = {
            "modelId": 86,
        }
        client.add_to_cart(params)
        client._post.assert_called()
        client._post.assert_called_with("/orders/cart/",
                                        body=json.dumps(params))

        with self.assertRaises(Exception):
            client.add_to_cart({})
Example #6
0
    def test_get_models(self):
        client = Client("key", "value")

        client.get_models()
        client._get.assert_called()
        client._get.assert_called_with("/models/", params=None)

        client._get.reset_mock()

        client.get_models(page=5)
        client._get.assert_called()
        client._get.assert_called_with("/models/", params={"page": 5})
Example #7
0
    def test_delete(self):
        with mock.patch.object(requests, "delete"):
            client = Client("key", "secret")
            client._delete("/api/")
            requests.delete.assert_called()
            args = requests.delete.call_args[1]
            self.assertEqual(args["url"], "https://api.shapeways.com/api/v1")
            self.assertIsInstance(args["auth"], OAuth1)
            self.assertEqual(args["params"], None)

            requests.delete.reset_mock()

            client = Client("key", "secret")
            params = {
                "key": "value",
            }
            client._delete("/api/", params=params)
            requests.delete.assert_called()
            args = requests.delete.call_args[1]
            self.assertEqual(args["url"], "https://api.shapeways.com/api/v1")
            self.assertIsInstance(args["auth"], OAuth1)
            self.assertEqual(args["params"], params)
Example #8
0
    def test_verify_url(self):
        # full url
        with mock.patch.object(Client, "verify"):
            client = Client("key", "secret")

            # full url
            client.verify_url(
                "http://example.org?oauth_token=TOKEN&oauth_verifier=VERIFIER")
            client.verify.assert_called()
            args = client.verify.call_args[0]
            self.assertEqual(args[0], "TOKEN")
            self.assertEqual(args[1], "VERIFIER")

            client.verify.reset_mock()

            # query string only
            client.verify_url("oauth_token=TOKEN&oauth_verifier=VERIFIER")
            client.verify.assert_called()
            args = client.verify.call_args[0]
            self.assertEqual(args[0], "TOKEN")
            self.assertEqual(args[1], "VERIFIER")

            client.verify.reset_mock()

            # no query string
            client.verify_url("http://example.org/")
            client.verify.assert_called()
            args = client.verify.call_args[0]
            self.assertEqual(args[0], None)
            self.assertEqual(args[1], None)

            client.verify.reset_mock()

            # missing verifier parameter
            client.verify_url("http://example.org?oauth_token=TOKEN")
            client.verify.assert_called()
            args = client.verify.call_args[0]
            self.assertEqual(args[0], "TOKEN")
            self.assertEqual(args[1], None)

            client.verify.reset_mock()

            # missing token parameter
            client.verify_url("http://example.org?oauth_verifier=VERIFIER")
            client.verify.assert_called()
            args = client.verify.call_args[0]
            self.assertEqual(args[0], None)
            self.assertEqual(args[1], "VERIFIER")

            client.verify.reset_mock()
Example #9
0
    def test_url_adds_slashes(self):
        client = Client("key", "secret")

        # trailing
        url = client.url("/models")
        self.assertEqual("https://api.shapeways.com/models/v1", url)

        # leading
        url = client.url("models/")
        self.assertEqual("https://api.shapeways.com/models/v1", url)

        # bounding
        url = client.url("models")
        self.assertEqual("https://api.shapeways.com/models/v1", url)

        # multi-part paths
        url = client.url("models/86")
        self.assertEqual("https://api.shapeways.com/models/86/v1", url)
Example #10
0
    def test_add_model(self):
        client = Client("key", "value")
        params = {
            "file": "<FILE DATA>",
            "fileName": "file.ext",
            "hasRightsToModel": True,
            "acceptTermsAndConditions": True,
        }
        client.add_model(params)
        client._post.assert_called()
        client._post.assert_called_with("/models/", body=json.dumps(params))

        params = {
            "file": "<FILE DATA>",
            "hasRightsToModel": True,
        }
        with self.assertRaises(Exception):
            client.add_model(params)
Example #11
0
    def test_verify(self):
        return_value = MockResponse()
        return_value.text = "oauth_token=052f70be42a6fe0971d4056eb4492c31115353f3&oauth_token_secret=7e412bef15092d4ed06a529b60d3a8c6925cbcf3"

        with mock.patch.object(requests, "post", return_value=return_value):
            client = Client("key", "secret")
            client.oauth_secret = None
            client.verify("TOKEN", "VERIFIER")
            requests.post.assert_called()
            self.assertEqual(client.oauth_token,
                             "052f70be42a6fe0971d4056eb4492c31115353f3")
            self.assertEqual(client.oauth_secret,
                             "7e412bef15092d4ed06a529b60d3a8c6925cbcf3")
            args = requests.post.call_args[1]
            self.assertEqual(
                args["url"],
                "https://api.shapeways.com/oauth1/access_token/v1")
            self.assertIsInstance(args["auth"], OAuth1)
Example #12
0
 def test_connect(self):
     return_value = MockResponse()
     return_value.text = "authentication_url=http%3A%2F%2Fapi.shapeways.com%2Flogin%3Foauth_token=052f70be42a6fe0971d4056eb4492c31115353f3&oauth_token_secret=7e412bef15092d4ed06a529b60d3a8c6925cbcf3&oauth_callback_confirmed=true"
     with mock.patch.object(requests, "post", return_value=return_value):
         client = Client("key",
                         "secret",
                         callback_url="http://localhost:3000/callback")
         url = client.connect()
         requests.post.assert_called()
         self.assertEqual(
             url,
             "http://api.shapeways.com/login?oauth_token=052f70be42a6fe0971d4056eb4492c31115353f3"
         )
         self.assertEqual(client.oauth_secret,
                          "7e412bef15092d4ed06a529b60d3a8c6925cbcf3")
         args = requests.post.call_args[1]
         self.assertEqual(
             args["url"],
             "https://api.shapeways.com/oauth1/request_token/v1")
         self.assertIsInstance(args["auth"], OAuth1)
Example #13
0
    def test_get_model_file(self):
        client = Client("key", "value")
        client.get_model_file(86, 24, include_file=False)
        client._get.assert_called()
        client._get.assert_called_with("/models/86/files/24/",
                                       params={"file": 0})

        client._get.reset_mock()

        client.get_model_file(86, 24, include_file=True)
        client._get.assert_called()
        client._get.assert_called_with("/models/86/files/24/",
                                       params={"file": 1})

        client._get.reset_mock()

        client.get_model_file(86, 24)
        client._get.assert_called()
        client._get.assert_called_with("/models/86/files/24/",
                                       params={"file": 0})
Example #14
0
 def test_get_printer(self):
     client = Client("key", "value")
     client.get_printer(86)
     client._get.assert_called()
     client._get.assert_called_with("/printers/86/")
Example #15
0
 def test_get_categories(self):
     client = Client("key", "value")
     client.get_categories()
     client._get.assert_called()
     client._get.assert_called_with("/categories/")
Example #16
0
 def test_get_model_info(self):
     client = Client("key", "value")
     client.get_model_info(86)
     client._get.assert_called()
     client._get.assert_called_with("/models/86/info/")
Example #17
0
 def test_delete_model(self):
     client = Client("key", "value")
     client.delete_model(86)
     client._delete.assert_called()
     client._delete.assert_called_with("/models/86/")
Example #18
0
import json

from shapeways.client import Client

client = Client(consumer_key="<YOUR KEY HERE>",
                consumer_secret="<YOUR SECRET HERE>",
                callback_url="http://localhost:3000/callback")


def application(environ, start_response):
    url = environ["PATH_INFO"]
    if url.startswith("/favicon.ico"):
        start_response("204 No Content", [])
        return [""]
    elif url.startswith("/login"):
        url = client.connect()
        start_response("302 Found", [
            ("Location", str(url)),
        ])
        return [""]
    elif url.startswith("/callback"):
        client.verify_url(environ["QUERY_STRING"])
        start_response("302 Found", [
            ("Location", "http://localhost:3000/"),
        ])
        return [""]
    else:
        response = client.get_api_info()
        start_response("200 Ok", [
            ("Content-Type", "application/json"),
        ])
Example #19
0
 def test_get_materials(self):
     client = Client("key", "value")
     client.get_materials()
     client._get.assert_called()
     client._get.assert_called_with("/materials/")
Example #20
0
 def test_get_cart(self):
     client = Client("key", "value")
     client.get_cart()
     client._get.assert_called()
     client._get.assert_called_with("/orders/cart/")
Example #21
0
 def test_get_api_info(self):
     client = Client("key", "value")
     client.get_api_info()
     client._get.assert_called()
     client._get.assert_called_with("/api/")
Example #22
0
import json
import webbrowser
from credentials import Credentials
from shapeways.client import Client

client = Client(
    consumer_key=Credentials["Consumer_Key"],
    consumer_secret=Credentials["Consumer_Secret"],
    callback_url="http://localhost:3000/callback"
)

def application(environ, start_response):
    url = environ["PATH_INFO"]
    if url.startswith("/favicon.ico"):
        start_response("204 No Content", [])
        return [""]
    elif url.startswith("/login"):
        url = client.connect()
        start_response("302 Found", [
            ("Location", str(url)),
        ])
        return [""]
    elif url.startswith("/callback"):
        client.verify_url(environ["QUERY_STRING"])
        start_response("302 Found", [
            ("Location", "http://localhost:3000/"),
        ])

        return [""]
    else:
        #this is where steps after verification goes