Plex plugin to to play various online streams (mostly Latvian).

request.py 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. from __future__ import absolute_import
  2. try:
  3. from urllib.parse import urlencode
  4. except ImportError:
  5. from urllib import urlencode
  6. from .filepost import encode_multipart_formdata
  7. __all__ = ['RequestMethods']
  8. class RequestMethods(object):
  9. """
  10. Convenience mixin for classes who implement a :meth:`urlopen` method, such
  11. as :class:`~urllib3.connectionpool.HTTPConnectionPool` and
  12. :class:`~urllib3.poolmanager.PoolManager`.
  13. Provides behavior for making common types of HTTP request methods and
  14. decides which type of request field encoding to use.
  15. Specifically,
  16. :meth:`.request_encode_url` is for sending requests whose fields are
  17. encoded in the URL (such as GET, HEAD, DELETE).
  18. :meth:`.request_encode_body` is for sending requests whose fields are
  19. encoded in the *body* of the request using multipart or www-form-urlencoded
  20. (such as for POST, PUT, PATCH).
  21. :meth:`.request` is for making any kind of request, it will look up the
  22. appropriate encoding format and use one of the above two methods to make
  23. the request.
  24. Initializer parameters:
  25. :param headers:
  26. Headers to include with all requests, unless other headers are given
  27. explicitly.
  28. """
  29. _encode_url_methods = set(['DELETE', 'GET', 'HEAD', 'OPTIONS'])
  30. def __init__(self, headers=None):
  31. self.headers = headers or {}
  32. def urlopen(self, method, url, body=None, headers=None,
  33. encode_multipart=True, multipart_boundary=None,
  34. **kw): # Abstract
  35. raise NotImplemented("Classes extending RequestMethods must implement "
  36. "their own ``urlopen`` method.")
  37. def request(self, method, url, fields=None, headers=None, **urlopen_kw):
  38. """
  39. Make a request using :meth:`urlopen` with the appropriate encoding of
  40. ``fields`` based on the ``method`` used.
  41. This is a convenience method that requires the least amount of manual
  42. effort. It can be used in most situations, while still having the
  43. option to drop down to more specific methods when necessary, such as
  44. :meth:`request_encode_url`, :meth:`request_encode_body`,
  45. or even the lowest level :meth:`urlopen`.
  46. """
  47. method = method.upper()
  48. if method in self._encode_url_methods:
  49. return self.request_encode_url(method, url, fields=fields,
  50. headers=headers,
  51. **urlopen_kw)
  52. else:
  53. return self.request_encode_body(method, url, fields=fields,
  54. headers=headers,
  55. **urlopen_kw)
  56. def request_encode_url(self, method, url, fields=None, headers=None,
  57. **urlopen_kw):
  58. """
  59. Make a request using :meth:`urlopen` with the ``fields`` encoded in
  60. the url. This is useful for request methods like GET, HEAD, DELETE, etc.
  61. """
  62. if headers is None:
  63. headers = self.headers
  64. extra_kw = {'headers': headers}
  65. extra_kw.update(urlopen_kw)
  66. if fields:
  67. url += '?' + urlencode(fields)
  68. return self.urlopen(method, url, **extra_kw)
  69. def request_encode_body(self, method, url, fields=None, headers=None,
  70. encode_multipart=True, multipart_boundary=None,
  71. **urlopen_kw):
  72. """
  73. Make a request using :meth:`urlopen` with the ``fields`` encoded in
  74. the body. This is useful for request methods like POST, PUT, PATCH, etc.
  75. When ``encode_multipart=True`` (default), then
  76. :meth:`urllib3.filepost.encode_multipart_formdata` is used to encode
  77. the payload with the appropriate content type. Otherwise
  78. :meth:`urllib.urlencode` is used with the
  79. 'application/x-www-form-urlencoded' content type.
  80. Multipart encoding must be used when posting files, and it's reasonably
  81. safe to use it in other times too. However, it may break request
  82. signing, such as with OAuth.
  83. Supports an optional ``fields`` parameter of key/value strings AND
  84. key/filetuple. A filetuple is a (filename, data, MIME type) tuple where
  85. the MIME type is optional. For example::
  86. fields = {
  87. 'foo': 'bar',
  88. 'fakefile': ('foofile.txt', 'contents of foofile'),
  89. 'realfile': ('barfile.txt', open('realfile').read()),
  90. 'typedfile': ('bazfile.bin', open('bazfile').read(),
  91. 'image/jpeg'),
  92. 'nonamefile': 'contents of nonamefile field',
  93. }
  94. When uploading a file, providing a filename (the first parameter of the
  95. tuple) is optional but recommended to best mimick behavior of browsers.
  96. Note that if ``headers`` are supplied, the 'Content-Type' header will
  97. be overwritten because it depends on the dynamic random boundary string
  98. which is used to compose the body of the request. The random boundary
  99. string can be explicitly set with the ``multipart_boundary`` parameter.
  100. """
  101. if headers is None:
  102. headers = self.headers
  103. extra_kw = {'headers': {}}
  104. if fields:
  105. if 'body' in urlopen_kw:
  106. raise TypeError(
  107. "request got values for both 'fields' and 'body', can only specify one.")
  108. if encode_multipart:
  109. body, content_type = encode_multipart_formdata(fields, boundary=multipart_boundary)
  110. else:
  111. body, content_type = urlencode(fields), 'application/x-www-form-urlencoded'
  112. extra_kw['body'] = body
  113. extra_kw['headers'] = {'Content-Type': content_type}
  114. extra_kw['headers'].update(headers)
  115. extra_kw.update(urlopen_kw)
  116. return self.urlopen(method, url, **extra_kw)