コード例 #1
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)
コード例 #2
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))
コード例 #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))
コード例 #4
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({})
コード例 #5
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, {})
コード例 #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})
コード例 #7
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)
コード例 #8
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)
コード例 #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)
コード例 #10
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)
コード例 #11
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)
コード例 #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)
コード例 #13
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)
コード例 #14
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({})
コード例 #15
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})
コード例 #16
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, {})
コード例 #17
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})
コード例 #18
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})
コード例 #19
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)
コード例 #20
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)
コード例 #21
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)
コード例 #22
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)
コード例 #23
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()
コード例 #24
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/")
コード例 #25
0
 def test_get_categories(self):
     client = Client("key", "value")
     client.get_categories()
     client._get.assert_called()
     client._get.assert_called_with("/categories/")
コード例 #26
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/")
コード例 #27
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/")
コード例 #28
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)
コード例 #29
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"),
        ])
コード例 #30
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/")
コード例 #31
0
 def test_get_cart(self):
     client = Client("key", "value")
     client.get_cart()
     client._get.assert_called()
     client._get.assert_called_with("/orders/cart/")
コード例 #32
0
 def test_get_cart(self):
     client = Client("key", "value")
     client.get_cart()
     client._get.assert_called()
     client._get.assert_called_with("/orders/cart/")
コード例 #33
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/")
コード例 #34
0
 def test_get_materials(self):
     client = Client("key", "value")
     client.get_materials()
     client._get.assert_called()
     client._get.assert_called_with("/materials/")
コード例 #35
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/")
コード例 #36
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/")
コード例 #37
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)
コード例 #38
0
 def test_get_materials(self):
     client = Client("key", "value")
     client.get_materials()
     client._get.assert_called()
     client._get.assert_called_with("/materials/")
コード例 #39
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/")
コード例 #40
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()
コード例 #41
0
 def test_get_categories(self):
     client = Client("key", "value")
     client.get_categories()
     client._get.assert_called()
     client._get.assert_called_with("/categories/")
コード例 #42
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
コード例 #43
0
from shapeways.client import Client
import keys
from urlparse import parse_qs
__author__ = 'rob'

client = Client(keys.__consumer_key__, keys.__consumer_secret__)
url = client.connect()
data = parse_qs(url.split('?')[1])
token = data.get("oauth_token", [None])[0]
print url
verifierCode = raw_input("Verifier Code: ")
url = url + "&oauth_verifier=" + verifierCode
client.verify_url(url)
print 'Token: ' + client.oauth_token
print 'Secret: ' + client.oauth_secret
コード例 #44
0
from shapeways.client import Client
import keys
import base64

__author__ = 'rob'

client = Client(consumer_key=keys.__consumer_key__,
                consumer_secret=keys.__consumer_secret__,
                oauth_token=keys.__oauth_token__,
                oauth_secret=keys.__oauth_secret__)
model_file = open("cube.stl", "rb")
file_contents = base64.b64encode(model_file.read())
params = {
    "file": file_contents,
    "fileName": "cube.stl",
    "hasRightsToModel": True,
    "acceptTermsAndConditions": True,
}
print client.add_model(params)