Esempio n. 1
0
class AssemblyTest(unittest.TestCase):
    def setUp(self):
        self.transloadit = Transloadit("key", "secret")
        self.assembly = self.transloadit.new_assembly()
        self.json_response = (
            '{"ok": "ASSEMBLY_COMPLETED", "assembly_id": "abcdef45673"}')

    def test_add_file(self):
        with open("LICENSE") as fs, open("README.md") as fs_2, open(
                "CHANGELOG.md") as fs_3:
            self.assembly.add_file(fs, "foo_field")

            self.assertEqual(self.assembly.files["foo_field"], fs)

            self.assembly.add_file(fs_2)
            self.assembly.add_file(fs_3)

            self.assertEqual(self.assembly.files["file"], fs_2)
            self.assertEqual(self.assembly.files["file_1"], fs_3)

    def test_remove_file(self):
        with open("LICENSE") as fs:
            self.assembly.add_file(fs, "foo_field")

            self.assertEqual(self.assembly.files["foo_field"], fs)

            self.assembly.remove_file("foo_field")
            self.assertIsNone(self.assembly.files.get("foo_field"))

    @requests_mock.Mocker()
    def test_save(self, mock):
        url = f"{self.transloadit.service}/assemblies"
        mock.post(
            url,
            text=self.json_response,
            additional_matcher=request_body_matcher(open("LICENSE").read()),
        )

        self.assembly.add_file(open("LICENSE"))
        assembly = self.assembly.create(resumable=False)
        self.assertEqual(assembly.data["ok"], "ASSEMBLY_COMPLETED")
        self.assertEqual(assembly.data["assembly_id"], "abcdef45673")

    @requests_mock.Mocker()
    def test_save_resumable(self, mock):
        url = f"{self.transloadit.service}/assemblies"
        mock.post(
            url,
            text=self.json_response,
            additional_matcher=request_body_matcher(
                "tus_num_expected_upload_files=0"),
        )

        assembly = self.assembly.create()
        self.assertEqual(assembly.data["ok"], "ASSEMBLY_COMPLETED")
        self.assertEqual(assembly.data["assembly_id"], "abcdef45673")
Esempio n. 2
0
class AssemblyTest(unittest.TestCase):
    def setUp(self):
        self.transloadit = Transloadit('key', 'secret')
        self.assembly = self.transloadit.new_assembly()
        self.json_response = '{"ok": "ASSEMBLY_COMPLETED", "assembly_id": "abcdef45673"}'

    def test_add_file(self):
        with open('LICENSE') as fs, open('README.md') as fs_2, open('CHANGELOG.md') as fs_3:
            self.assembly.add_file(fs, 'foo_field')

            self.assertEqual(self.assembly.files['foo_field'], fs)

            self.assembly.add_file(fs_2)
            self.assembly.add_file(fs_3)

            self.assertEqual(self.assembly.files['file'], fs_2)
            self.assertEqual(self.assembly.files['file_1'], fs_3)

    def test_remove_file(self):
        with open('LICENSE') as fs:
            self.assembly.add_file(fs, 'foo_field')

            self.assertEqual(self.assembly.files['foo_field'], fs)

            self.assembly.remove_file('foo_field')
            self.assertIsNone(self.assembly.files.get('foo_field'))

    @requests_mock.Mocker()
    def test_save(self, mock):
        url = '{}/assemblies'.format(self.transloadit.service)
        mock.post(url, text=self.json_response,
                  additional_matcher=request_body_matcher(open('LICENSE').read()))

        self.assembly.add_file(open('LICENSE'))
        assembly = self.assembly.create(resumable=False)
        self.assertEqual(assembly.data['ok'], "ASSEMBLY_COMPLETED")
        self.assertEqual(assembly.data['assembly_id'], "abcdef45673")

    @requests_mock.Mocker()
    def test_save_resumable(self, mock):
        url = '{}/assemblies'.format(self.transloadit.service)
        mock.post(url, text=self.json_response,
                  additional_matcher=request_body_matcher('tus_num_expected_upload_files=0'))

        assembly = self.assembly.create()
        self.assertEqual(assembly.data['ok'], "ASSEMBLY_COMPLETED")
        self.assertEqual(assembly.data['assembly_id'], "abcdef45673")
Esempio n. 3
0
class TemplateTest(unittest.TestCase):
    def setUp(self):
        self.transloadit = Transloadit("key", "secret")
        self.template = self.transloadit.new_template("foo")

    @requests_mock.Mocker()
    def test_save(self, mock):
        url = f"{self.transloadit.service}/templates"
        sub_body = '"robot": "/image/resize"'
        mock.post(
            url,
            text='{"ok":"TEMPLATE_CREATED","template_name":"foo"}',
            additional_matcher=request_body_matcher(
                urllib.parse.quote_plus(sub_body)),
        )

        self.template.add_step("resize", "/image/resize", {
            "width": 70,
            "height": 70
        })
        template = self.template.create()
        self.assertEqual(template.data["ok"], "TEMPLATE_CREATED")
        self.assertEqual(template.data["template_name"], "foo")
Esempio n. 4
0
from __future__ import print_function

from transloadit.client import Transloadit

tl = Transloadit('TRANSLOADIT_KEY', 'TRANSLOADIT_SECRET')
ass = tl.new_assembly()
ass.add_file(open('fixtures/lol_cat.jpg', 'rb'))
ass.add_step('resize', '/image/resize', {'width': 70, 'height': 70})
response = ass.create(wait=True)

result_url = response.data.get('results').get('resize')[0].get('ssl_url')
print('Your result:', result_url)
Esempio n. 5
0
class ClientTest(unittest.TestCase):
    def setUp(self):
        self.transloadit = Transloadit('key', 'secret')

    @requests_mock.Mocker()
    def test_get_assembly(self, mock):
        id_ = 'abcdef12345'
        url = '{}/assemblies/{}'.format(self.transloadit.service, id_)
        mock.get(
            url,
            text='{"ok": "ASSEMBLY_COMPLETED", "assembly_id": "abcdef12345"}')

        response = self.transloadit.get_assembly(assembly_id=id_)
        self.assertEqual(response.data['ok'], 'ASSEMBLY_COMPLETED')
        self.assertEqual(response.data['assembly_id'], 'abcdef12345')

    @requests_mock.Mocker()
    def test_list_assemblies(self, mock):
        url = '{}/assemblies'.format(self.transloadit.service)
        mock.get(url, text='{"items":[],"count":0}')

        response = self.transloadit.list_assemblies()
        self.assertEqual(response.data['items'], [])
        self.assertEqual(response.data['count'], 0)

    @requests_mock.Mocker()
    def test_cancel_assembly(self, mock):
        id_ = 'abcdef12345'
        url = '{}/assemblies/{}'.format(self.transloadit.service, id_)
        mock.delete(
            url,
            text='{"ok": "ASSEMBLY_CANCELED", "assembly_id": "abcdef12345"}')

        response = self.transloadit.cancel_assembly(id_)
        self.assertEqual(response.data['ok'], 'ASSEMBLY_CANCELED')
        self.assertEqual(response.data['assembly_id'], 'abcdef12345')

    @requests_mock.Mocker()
    def test_get_template(self, mock):
        id_ = 'abcdef12345'
        url = '{}/templates/{}'.format(self.transloadit.service, id_)
        mock.get(url,
                 text='{"ok": "TEMPLATE_FOUND", "template_id": "abcdef12345"}')

        response = self.transloadit.get_template(id_)
        self.assertEqual(response.data['ok'], 'TEMPLATE_FOUND')
        self.assertEqual(response.data['template_id'], 'abcdef12345')

    @requests_mock.Mocker()
    def test_list_templates(self, mock):
        url = '{}/templates'.format(self.transloadit.service)
        mock.get(url, text='{"items":[],"count":0}')

        response = self.transloadit.list_templates()
        self.assertEqual(response.data['items'], [])
        self.assertEqual(response.data['count'], 0)

    @requests_mock.Mocker()
    def test_update_template(self, mock):
        id_ = 'abcdef12345'
        url = '{}/templates/{}'.format(self.transloadit.service, id_)
        sub_body = '"name": "foo_bar"'
        mock.put(
            url,
            text='{"ok": "TEMPLATE_UPDATED", "template_id": "abcdef12345"}',
            additional_matcher=request_body_matcher(
                urllib.parse.quote_plus(sub_body)))

        response = self.transloadit.update_template(id_, {'name': 'foo_bar'})
        self.assertEqual(response.data['ok'], 'TEMPLATE_UPDATED')
        self.assertEqual(response.data['template_id'], 'abcdef12345')

    @requests_mock.Mocker()
    def test_delete_tempalte(self, mock):
        id_ = 'abcdef12345'
        url = '{}/templates/{}'.format(self.transloadit.service, id_)
        mock.delete(url, text='{"ok": "TEMPLATE_DELETED"}')

        response = self.transloadit.delete_template(id_)
        self.assertEqual(response.data['ok'], 'TEMPLATE_DELETED')

    @requests_mock.Mocker()
    def test_get_bill(self, mock):
        year = 2017
        month = 9
        url = '/bill/{}-0{}'.format(year, month)
        mock.get(url, text='{"ok":"BILL_FOUND"}')

        response = self.transloadit.get_bill(month, year)
        self.assertEqual(response.data['ok'], 'BILL_FOUND')
Esempio n. 6
0
 def setUp(self):
     self.transloadit = Transloadit('key', 'secret')
Esempio n. 7
0
 def setUp(self):
     self.transloadit = Transloadit("key", "secret")
     self.assembly = self.transloadit.new_assembly()
     self.json_response = (
         '{"ok": "ASSEMBLY_COMPLETED", "assembly_id": "abcdef45673"}')
Esempio n. 8
0
 def setUp(self):
     self.transloadit = Transloadit('key', 'secret')
     self.request = Request(self.transloadit)
Esempio n. 9
0
class ClientTest(unittest.TestCase):
    def setUp(self):
        self.transloadit = Transloadit("key", "secret")

    @requests_mock.Mocker()
    def test_get_assembly(self, mock):
        id_ = "abcdef12345"
        url = f"{self.transloadit.service}/assemblies/{id_}"
        mock.get(
            url,
            text='{"ok": "ASSEMBLY_COMPLETED", "assembly_id": "abcdef12345"}')

        response = self.transloadit.get_assembly(assembly_id=id_)
        self.assertEqual(response.data["ok"], "ASSEMBLY_COMPLETED")
        self.assertEqual(response.data["assembly_id"], "abcdef12345")

    @requests_mock.Mocker()
    def test_list_assemblies(self, mock):
        url = f"{self.transloadit.service}/assemblies"
        mock.get(url, text='{"items":[],"count":0}')

        response = self.transloadit.list_assemblies()
        self.assertEqual(response.data["items"], [])
        self.assertEqual(response.data["count"], 0)

    @requests_mock.Mocker()
    def test_cancel_assembly(self, mock):
        id_ = "abcdef12345"
        url = f"{self.transloadit.service}/assemblies/{id_}"
        mock.delete(
            url,
            text='{"ok": "ASSEMBLY_CANCELED", "assembly_id": "abcdef12345"}')

        response = self.transloadit.cancel_assembly(id_)
        self.assertEqual(response.data["ok"], "ASSEMBLY_CANCELED")
        self.assertEqual(response.data["assembly_id"], "abcdef12345")

    @requests_mock.Mocker()
    def test_get_template(self, mock):
        id_ = "abcdef12345"
        url = f"{self.transloadit.service}/templates/{id_}"
        mock.get(url,
                 text='{"ok": "TEMPLATE_FOUND", "template_id": "abcdef12345"}')

        response = self.transloadit.get_template(id_)
        self.assertEqual(response.data["ok"], "TEMPLATE_FOUND")
        self.assertEqual(response.data["template_id"], "abcdef12345")

    @requests_mock.Mocker()
    def test_list_templates(self, mock):
        url = f"{self.transloadit.service}/templates"
        mock.get(url, text='{"items":[],"count":0}')

        response = self.transloadit.list_templates()
        self.assertEqual(response.data["items"], [])
        self.assertEqual(response.data["count"], 0)

    @requests_mock.Mocker()
    def test_update_template(self, mock):
        id_ = "abcdef12345"
        url = f"{self.transloadit.service}/templates/{id_}"
        sub_body = '"name": "foo_bar"'
        mock.put(
            url,
            text='{"ok": "TEMPLATE_UPDATED", "template_id": "abcdef12345"}',
            additional_matcher=request_body_matcher(
                urllib.parse.quote_plus(sub_body)),
        )

        response = self.transloadit.update_template(id_, {"name": "foo_bar"})
        self.assertEqual(response.data["ok"], "TEMPLATE_UPDATED")
        self.assertEqual(response.data["template_id"], "abcdef12345")

    @requests_mock.Mocker()
    def test_delete_tempalte(self, mock):
        id_ = "abcdef12345"
        url = f"{self.transloadit.service}/templates/{id_}"
        mock.delete(url, text='{"ok": "TEMPLATE_DELETED"}')

        response = self.transloadit.delete_template(id_)
        self.assertEqual(response.data["ok"], "TEMPLATE_DELETED")

    @requests_mock.Mocker()
    def test_get_bill(self, mock):
        year = 2017
        month = 9
        url = f"/bill/{year}-0{month}"
        mock.get(url, text='{"ok":"BILL_FOUND"}')

        response = self.transloadit.get_bill(month, year)
        self.assertEqual(response.data["ok"], "BILL_FOUND")
Esempio n. 10
0
 def setUp(self):
     self.transloadit = Transloadit("key", "secret")
Esempio n. 11
0
 def setUp(self):
     self.transloadit = Transloadit("key", "secret")
     self.request = Request(self.transloadit)
Esempio n. 12
0
 def setUp(self):
     self.transloadit = Transloadit("key", "secret")
     self.template = self.transloadit.new_template("foo")
Esempio n. 13
0
from transloadit.client import Transloadit

client = Transloadit("TRANSLOADIT_KEY", "TRANSLOADIT_SECRET")
assembly = client.new_assembly()
assembly.add_file(open("fixtures/lol_cat.jpg", "rb"))
assembly.add_step("resize", "/image/resize", {"width": 70, "height": 70})
response = assembly.create(wait=True)

result_url = response.data.get("results").get("resize")[0].get("ssl_url")
print("Your result:", result_url)