Skip to content

Commit 0722243

Browse files
committed
added tests for render api
1 parent 97a720e commit 0722243

File tree

4 files changed

+162
-10
lines changed

4 files changed

+162
-10
lines changed

opentok/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
from .streamlist import StreamList
1515
from .sip_call import SipCall
1616
from .broadcast import Broadcast, BroadcastStreamModes
17-
from .render import Render
17+
from .render import Render, RenderList

opentok/opentok.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from .endpoints import Endpoints
2929
from .session import Session
3030
from .archives import Archive, ArchiveList, OutputModes, StreamModes
31-
from .render import Render
31+
from .render import Render, RenderList
3232
from .stream import Stream
3333
from .streamlist import StreamList
3434
from .sip_call import SipCall
@@ -1709,7 +1709,7 @@ def stop_render(self, render_id):
17091709
)
17101710

17111711
if response.status_code == 200:
1712-
return "Render has been stopped successfully."
1712+
return response
17131713
elif response.status_code == 400:
17141714
raise RequestError(
17151715
"Invalid request. This response may indicate that data in your request is invalid JSON. Or it may indicate that you do not pass in a session ID."
@@ -1740,23 +1740,21 @@ def list_renders(self, offset=0, count=50):
17401740
)
17411741

17421742
response = requests.get(
1743-
self.endpoints.get_render_url() + "&count:",
1743+
self.endpoints.get_render_url(),
17441744
headers=self.get_headers(),
17451745
params=query_params,
17461746
proxies=self.proxies,
17471747
timeout=self.timeout,
17481748
)
17491749

17501750
if response.status_code == 200:
1751-
return Render(response.json())
1751+
return RenderList(self, response.json())
17521752
elif response.status_code == 400:
17531753
raise RequestError(
17541754
"Invalid request. This response may indicate that data in your request is invalid JSON. Or it may indicate that you do not pass in a session ID."
17551755
)
17561756
elif response.status_code == 403:
17571757
raise AuthError("You passed in an invalid OpenTok API key or JWT token.")
1758-
elif response.status_code == 404:
1759-
raise NotFoundError("No render matching the specified render ID was found.")
17601758
else:
17611759
raise RequestError("An unexpected error occurred", response.status_code)
17621760

opentok/render.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from six import iteritems
23

34
class Render:
45
"""Represents a render of an OpenTok session."""
@@ -18,3 +19,21 @@ def __init__(self, kwargs):
1819
def json(self):
1920
"""Returns a JSON representation of the render."""
2021
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4)
22+
23+
def attrs(self):
24+
"""
25+
Returns a dictionary of the render's attributes.
26+
"""
27+
return dict((k, v) for k, v in iteritems(self.__dict__))
28+
29+
class RenderList:
30+
"""Object that represents a list of renders."""
31+
def __init__(self, sdk, values):
32+
self.count = values.get("count")
33+
self.items = list(map(lambda x: Render(x), values.get("items", [])))
34+
35+
def attrs(self):
36+
return {"count": self.count, "items": map(Render.attrs, self.items)}
37+
38+
def json(self):
39+
return json.dumps(self.attrs(), indent=4)

tests/test_render.py

Lines changed: 138 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
from lib2to3.pgen2 import token
21
import unittest
32
import textwrap
43
import httpretty
54
import json
5+
import requests
66
from sure import expect
77

88
from six import u, PY2, PY3
99
from expects import *
10-
from opentok import Client, Render, __version__
10+
from opentok import Client, Render, RenderList, __version__
1111
from .validate_jwt import validate_jwt_header
1212

1313

@@ -78,4 +78,139 @@ def test_start_render(self):
7878
expect(render).to(have_property(u("resolution"), u("1280x720")))
7979
expect(render).to(have_property(u("status"), u("started")))
8080
expect(render).to(have_property(u("streamId"), u("e32445b743678c98230f238")))
81-
81+
82+
83+
@httpretty.activate
84+
def test_get_render_status(self):
85+
httpretty.register_uri(
86+
httpretty.GET,
87+
u("https://api.opentok.com/v2/project/{0}/render/{1}").format(self.api_key, self.render_id),
88+
body=textwrap.dedent(
89+
u(
90+
"""\
91+
{
92+
"id": "80abaf0d-25a3-4efc-968f-6268d620668d",
93+
"sessionId": "2_MX4xMDBfjE0Mzc2NzY1NDgwMTJ-TjMzfn4",
94+
"projectId": "e2343f23456g34709d2443a234",
95+
"createdAt": 1437676551000,
96+
"updatedAt": 1437676551000,
97+
"url": "https://webapp.customer.com",
98+
"resolution": "1280x720",
99+
"status": "failed",
100+
"reason":"Could not load URL"
101+
}
102+
"""
103+
)
104+
),
105+
status=200,
106+
content_type=u("application/json"),
107+
)
108+
109+
render = self.opentok.get_render_status(self.render_id)
110+
111+
validate_jwt_header(self, httpretty.last_request().headers[u("x-opentok-auth")])
112+
expect(httpretty.last_request().headers[u("user-agent")]).to(
113+
contain(u("OpenTok-Python-SDK/") + __version__)
114+
)
115+
expect(httpretty.last_request().headers[u("content-type")]).to(
116+
equal(u("application/json"))
117+
118+
)
119+
120+
expect(render).to(be_a(Render))
121+
expect(render).to(
122+
have_property(u("id"), u("80abaf0d-25a3-4efc-968f-6268d620668d"))
123+
)
124+
expect(render).to(
125+
have_property(u("sessionId"), u("2_MX4xMDBfjE0Mzc2NzY1NDgwMTJ-TjMzfn4"))
126+
)
127+
expect(render).to(have_property(u("projectId"), u("e2343f23456g34709d2443a234")))
128+
expect(render).to(have_property(u("createdAt"), 1437676551000))
129+
expect(render).to(have_property(u("updatedAt"), 1437676551000))
130+
expect(render).to(have_property(u("resolution"), u("1280x720")))
131+
expect(render).to(have_property(u("status"), u("failed")))
132+
expect(render).to(have_property(u("reason"), u("Could not load URL")))
133+
134+
@httpretty.activate
135+
def test_stop_render(self):
136+
httpretty.register_uri(
137+
httpretty.DELETE,
138+
u("https://api.opentok.com/v2/project/{0}/render/{1}").format(self.api_key, self.render_id),
139+
body="",
140+
status=200,
141+
)
142+
143+
response = self.opentok.stop_render(self.render_id)
144+
145+
validate_jwt_header(self, httpretty.last_request().headers[u("x-opentok-auth")])
146+
expect(httpretty.last_request().headers[u("user-agent")]).to(
147+
contain(u("OpenTok-Python-SDK/") + __version__)
148+
)
149+
150+
assert isinstance(response, requests.Response)
151+
assert response.status_code == 200
152+
153+
@httpretty.activate
154+
def test_list_renders(self):
155+
httpretty.register_uri(
156+
httpretty.GET,
157+
u("https://api.opentok.com/v2/project/{0}/render").format(self.api_key),
158+
body=textwrap.dedent(
159+
u(
160+
"""\
161+
{
162+
"count":2,
163+
"items":[
164+
{
165+
"id":"80abaf0d-25a3-4efc-968f-6268d620668d",
166+
"sessionId":"1_MX4yNzA4NjYxMn5-MTU0NzA4MDUyMTEzNn5sOXU5ZnlWYXplRnZGblV4RUo3dXJpZk1-fg",
167+
"projectId":"27086612",
168+
"createdAt":1547080532099,
169+
"updatedAt":1547080532099,
170+
"url": "https://webapp.customer.com",
171+
"resolution": "1280x720",
172+
"status": "started",
173+
"streamId": "d2334b35690a92f78945"
174+
},
175+
{
176+
"id":"d95f6496-df6e-4f49-86d6-832e00303602",
177+
"sessionId":"2_MX4yNzA4NjYxMn5-MTU0NzA4MDUwMDc2MH5STWRiSE1jZjVoV3lBQU9nN2JuNElUV3V-fg",
178+
"projectId":"27086612",
179+
"createdAt":1547080511760,
180+
"updatedAt":1547080518965,
181+
"url": "https://webapp2.customer.com",
182+
"resolution": "1280x720",
183+
"status":"stopped",
184+
"streamId": "d2334b35690a92f78945",
185+
"reason":"Maximum duration exceeded"
186+
}
187+
]
188+
}
189+
"""
190+
)
191+
),
192+
status=200,
193+
content_type=u("application/json"),
194+
)
195+
196+
render_list = self.opentok.list_renders()
197+
validate_jwt_header(self, httpretty.last_request().headers[u("x-opentok-auth")])
198+
expect(httpretty.last_request().headers[u("user-agent")]).to(
199+
contain(u("OpenTok-Python-SDK/") + __version__)
200+
)
201+
202+
expect(render_list).to(be_a(RenderList))
203+
expect(render_list).to(have_property(u("count"), 2))
204+
expect(render_list).to(have_property(u("items")))
205+
expect(render_list.items[0]).to(have_property(u("sessionId"), u("1_MX4yNzA4NjYxMn5-MTU0NzA4MDUyMTEzNn5sOXU5ZnlWYXplRnZGblV4RUo3dXJpZk1-fg")))
206+
expect(render_list.items[0]).to(have_property(u("createdAt"), u(1547080532099)))
207+
expect(render_list.items[0]).to(have_property(u("url"), u("https://webapp.customer.com")))
208+
expect(render_list.items[0]).to(have_property(u("status"), u("started")))
209+
expect(render_list.items[0]).to(have_property(u("streamId"), u("d2334b35690a92f78945")))
210+
expect(render_list.items[1]).to(have_property(u("sessionId"), u("2_MX4yNzA4NjYxMn5-MTU0NzA4MDUwMDc2MH5STWRiSE1jZjVoV3lBQU9nN2JuNElUV3V-fg")))
211+
expect(render_list.items[1]).to(have_property(u("createdAt"), u(1547080511760)))
212+
expect(render_list.items[1]).to(have_property(u("updatedAt"), u(1547080518965)))
213+
expect(render_list.items[1]).to(have_property(u("url"), u("https://webapp2.customer.com")))
214+
expect(render_list.items[1]).to(have_property(u("status"), u("stopped")))
215+
expect(render_list.items[1]).to(have_property(u("streamId"), u("d2334b35690a92f78945")))
216+
expect(render_list.items[1]).to(have_property(u("reason"), u("Maximum duration exceeded")))

0 commit comments

Comments
 (0)