|
| 1 | +================== |
| 2 | +OpenTok Python SDK |
| 3 | +================== |
| 4 | + |
| 5 | +.. image:: https://travis-ci.org/aoberoi/Opentok-Python-SDK.svg?branch=modernization |
| 6 | + :target: https://travis-ci.org/aoberoi/Opentok-Python-SDK |
| 7 | + |
| 8 | +The OpenTok Python SDK lets you generate |
| 9 | +`sessions <http://tokbox.com/opentok/tutorials/create-session/>`_ and |
| 10 | +`tokens <http://tokbox.com/opentok/tutorials/create-token/>`_ for `OpenTok <http://www.tokbox.com/>`_ |
| 11 | +applications. This version of the SDK also includes support for working with OpenTok 2.0 archives. |
| 12 | + |
| 13 | + |
| 14 | +Installation |
| 15 | +------------ |
| 16 | + |
| 17 | +Pip (recommended): |
| 18 | +~~~~~~~~~~~~~~~~~~ |
| 19 | + |
| 20 | +Pip helps manage dependencies for Python projects using the PyPI index. Find more info here: |
| 21 | +http://www.pip-installer.org/en/latest/ |
| 22 | + |
| 23 | +Add the ``opentok`` package as a dependency in your project. The most common way is to add it to your |
| 24 | +``requirements.txt`` file:: |
| 25 | + |
| 26 | + opentok>=2.2 |
| 27 | + |
| 28 | +Next, install the dependencies:: |
| 29 | + |
| 30 | + $ pip install -r requirements.txt |
| 31 | + |
| 32 | +Manually: |
| 33 | +~~~~~~~~~ |
| 34 | + |
| 35 | +Download the latest package zip from the `Releases page |
| 36 | +<https://github.com/opentok/Opentok-Python-SDK/releases>`_ |
| 37 | + |
| 38 | + |
| 39 | +Usage |
| 40 | +----- |
| 41 | + |
| 42 | +Initializing |
| 43 | +~~~~~~~~~~~~ |
| 44 | + |
| 45 | +Import the package at the top of any file where you will use it. At the very least you will need the |
| 46 | +``OpenTok`` class. Then initialize an OpenTok instance with your own API Key and API Secret. |
| 47 | + |
| 48 | +.. code:: python |
| 49 | + |
| 50 | + from opentok import OpenTok |
| 51 | + |
| 52 | + opentok = OpenTok(api_key, api_secret) |
| 53 | + |
| 54 | +Creating Sessions |
| 55 | +~~~~~~~~~~~~~~~~~ |
| 56 | + |
| 57 | +The create an OpenTok Session, use the ``opentok.create_session()`` method. There are two optional |
| 58 | +keyword parameters for this method: ``location`` which can be set to a string containing an IP |
| 59 | +address, and ``p2p`` which is a boolean. This method returns a ``Session`` object. Its |
| 60 | +``session_id`` attribute is useful when saving to a persistent store (e.g. database). |
| 61 | + |
| 62 | +.. code:: python |
| 63 | + |
| 64 | + # Just a plain Session |
| 65 | + session = opentok.create_session() |
| 66 | + # A p2p Session |
| 67 | + session = opentok.create_session(p2p=True) |
| 68 | + # A Session with a location hint |
| 69 | + session = opentok.create_session(location=u'12.34.56.78') |
| 70 | + |
| 71 | + # Store this session ID in the database |
| 72 | + session_id = session.session_id |
| 73 | + |
| 74 | +Generating Tokens |
| 75 | +~~~~~~~~~~~~~~~~~ |
| 76 | + |
| 77 | +Once a Session is created, you can start generating Tokens for clients to use when connecting to it. |
| 78 | +You can generate a token either by calling the ``opentok.generate_token(session_id)`` method or by |
| 79 | +calling the ``session.generate_token()`` method on a ``Session`` instance after creating it. There |
| 80 | +is a set of optional keyword parameters: ``role``, ``expire_time``, and ``data``. |
| 81 | + |
| 82 | +.. code:: python |
| 83 | + |
| 84 | + # Generate a Token from just a session_id (fetched from a database) |
| 85 | + token = opentok.generate_token(session_id) |
| 86 | + # Generate a Token by calling the method on the Session (returned from create_session) |
| 87 | + token = session.generate_token() |
| 88 | + |
| 89 | + from opentok import Roles |
| 90 | + # Set some options in a token |
| 91 | + token = session.generate_token(role=Roles.moderator, |
| 92 | + expire_time=int(time.time()) + 10, |
| 93 | + data=u'name=Johnny') |
| 94 | + |
| 95 | +Working with Archives |
| 96 | +~~~~~~~~~~~~~~~~~~~~~ |
| 97 | + |
| 98 | +You can start the recording of an OpenTok Session using the ``opentok.start_archive(session_id)`` |
| 99 | +method. This method takes an optional keyword argument ``name`` to assign a name to the archive. |
| 100 | +This method will return an ``Archive`` instance. Note that you can only start an Archive on |
| 101 | +a Session that has clients connection. |
| 102 | + |
| 103 | +.. code:: python |
| 104 | + |
| 105 | + archive = opentok.start_archive(session_id, name=u'Important Presentation') |
| 106 | + |
| 107 | + # Store this archive_id in the database |
| 108 | + archive_id = archive.id |
| 109 | + |
| 110 | +You can stop the recording of a started Archive using the ``opentok.stop_archive(archive_id)`` |
| 111 | +method. You can also do this using the ``archive.stop()`` method of an ``Archive`` instance. |
| 112 | + |
| 113 | +.. code:: python |
| 114 | + |
| 115 | + # Stop an Archive from an archive_id (fetched from database) |
| 116 | + opentok.stop_archive(archive_id) |
| 117 | + # Stop an Archive from an instance (returned from opentok.start_archive) |
| 118 | + archive.stop() |
| 119 | + |
| 120 | +To get an ``Archive`` instance (and all the information about it) from an archive ID, use the |
| 121 | +``opentok.get_archive(archive_id)`` method. |
| 122 | + |
| 123 | +.. code:: python |
| 124 | + |
| 125 | + archive = opentok.get_archive(archive_id) |
| 126 | + |
| 127 | +To delete an Archive, you can call the ``opentok.delete_archive(archive_id)`` method or the |
| 128 | +``archive.delete()`` method of an ``Archive`` instance. |
| 129 | + |
| 130 | +.. code:: python |
| 131 | + |
| 132 | + # Delete an Archive from an archive ID (fetched from database) |
| 133 | + opentok.delete_archive(archive_id) |
| 134 | + # Delete an Archive from an Archive instance (returned from opentok.start_archive or |
| 135 | + opentok.get_archive) |
| 136 | + archive.delete() |
| 137 | + |
| 138 | +You can also get a list of all the Archives you've created (up to 1000) with your API Key. This is |
| 139 | +done using the ``opentok.list_archives()`` method. There are two optional keyword parameters: |
| 140 | +``count`` and ``offset``; they can help you paginate through the results. This method returns an |
| 141 | +instance of the ``ArchiveList`` class. |
| 142 | + |
| 143 | +.. code:: python |
| 144 | + |
| 145 | + archive_list = opentok.list_archive() |
| 146 | + |
| 147 | + # Get a specific Archive from the list |
| 148 | + archive = archive_list.items[i] |
| 149 | + |
| 150 | + # Iterate over items |
| 151 | + for archive in iter(archive_list): |
| 152 | + pass |
| 153 | + |
| 154 | + # Get the total number of Archives for this API Key |
| 155 | + total = archive_list.total |
| 156 | + |
| 157 | +Documentation |
| 158 | +------------- |
| 159 | + |
| 160 | +Reference documentation is available at <http://www.tokbox.com//opentok/libraries/server/python/reference/index.html> and in the |
| 161 | +docs directory of the SDK. |
| 162 | + |
| 163 | +Requirements |
| 164 | +------------ |
| 165 | + |
| 166 | +You need an OpenTok API key and API secret, which you can obtain at https://dashboard.tokbox.com/ |
| 167 | + |
| 168 | +The OpenTok Python SDK requires Python 2.6, 2.7, 3.2, 3.3, or 3.4 |
| 169 | + |
| 170 | +Release Notes |
| 171 | +------------- |
| 172 | + |
| 173 | +See the `Releases <https://github.com/opentok/Opentok-Python-SDK/releases>`_ page for details about |
| 174 | +each release. |
| 175 | + |
| 176 | +Important changes in v2.0 |
| 177 | +~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 178 | + |
| 179 | +This version of the SDK includes support for working with OpenTok 2.0 archives. (This API does not |
| 180 | +work with OpenTok 1.0 archives.) |
| 181 | + |
| 182 | +Development and Contributing |
| 183 | +---------------------------- |
| 184 | + |
| 185 | +Interested in contributing? We <3 pull requests! File a new |
| 186 | +`Issue <https://github.com/opentok/opentok-python-sdk/issues>`_ or take a look at the existing ones. |
| 187 | +If you are going to send us a pull request, please try to run the test suite first and also include |
| 188 | +tests for your changes. |
| 189 | + |
| 190 | +Support |
| 191 | +------- |
| 192 | + |
| 193 | +See http://tokbox.com/opentok/support/ for all our support options. |
| 194 | + |
| 195 | +Find a bug? File it on the `Issues <https://github.com/opentok/opentok-python-sdk/issues>`_ page. |
| 196 | +Hint: test cases are really helpful! |
0 commit comments