def test_simple_text(self): req = req_t('GET', 'http://www.example.com') resp = MockResponse(404, 'Not Found', request=req, text='errText') exc = RequestFailedException(resp) assert exc.response == resp assert exc.status_code == 404 assert exc.reason == 'Not Found' assert str( exc ) == 'GET http://www.example.com returned HTTP 404 Not Found: errText'
def test_simple_error_json(self): req = req_t('GET', 'http://www.example.com') resp = MockResponse(404, 'Not Found', request=req, _json={'error': { 'message': 'errMsg' }}) exc = RequestFailedException(resp) assert exc.response == resp assert exc.status_code == 404 assert exc.reason == 'Not Found' assert str( exc ) == 'GET http://www.example.com returned HTTP 404 Not Found: errMsg'
def upload_image(self, img_data, img_filename, content_type): """ Upload a new Image resource to be stored on the server as a temporary image, i.e. for embedding in an upcoming Document, Post, etc. Returns Image object and the user-facing URI for the image itself, i.e. ``https://sandbox.jiveon.com/api/core/v3/images/601174?a=1522503578891`` . This is the low-level direct API call that corresponds to `Upload New Image <https://developers.jivesoftware.com/api/v3/cloud/rest/ImageServic e.html#uploadImage%28MultipartBody%29>`_. **Note:** Python's ``requests`` lacks streaming file support. As such, images sent using this method will be entirely read into memory and then sent. This may not work very well for extremely large images. **Warning:** As far as I can tell, the user-visible URI to an image can *only* be retrieved when the image is uploaded. There does not seem to be a way to get it from the API for an existing image. :param img_data: The binary image data. :type img_data: bytes :param img_filename: The filename for the image. This is purely for display purposes. :type img_filename: str :param content_type: The MIME Content Type for the image data. :type content_type: str :return: 2-tuple of (string user-facing URI to the image i.e. for use in HTML, dict Image object representation) :rtype: tuple """ # Testing Note: betamax==0.8.1 and/or betamax-serializers==0.2.0 cannot # handle testing the binary response content from this method. url = self.abs_url('core/v3/images') files = { 'file': (img_filename, img_data, content_type) } logger.debug('POST to %s (length %d)', url, len(img_data)) res = self._requests.post(url, files=files, allow_redirects=False) logger.debug( 'POST %s returned %d %s', url, res.status_code, res.reason ) if res.status_code != 201: raise RequestFailedException(res) logger.debug( 'Uploaded image with Location: %s', res.headers['Location'] ) return res.headers['Location'], res.json()
def _put_json(self, path, data): """ Execute a PUT request against the Jive API, sending JSON. :param path: path or full URL to PUT to :type path: str :param data: Data to POST. :type data: ``dict`` or ``list`` :return: deserialized response JSON. Usually dict or list. """ if path.startswith('http://') or path.startswith('https://'): # likely a pagination link url = path else: url = self.abs_url(path) logger.debug('PUT to %s (length %d)', url, len(json.dumps(data))) res = self._requests.put(url, json=data) logger.debug('PUT %s returned %d %s', url, res.status_code, res.reason) if res.status_code not in [200, 201]: raise RequestFailedException(res) return res.json()
def get_image(self, image_id): """ GET the image specified by ``image_id`` as binary content. This method currently can only retrieve the exact original image. This is the low-level direct API call that corresponds to `Get Image <https://devel opers.jivesoftware.com/api/v3/cloud/rest/ImageService.html#getImage%28S tring%2C%20String%2C%20String%2C%20String%2C%20String%29>`_. :param image_id: Jive Image ID to get. This can be found in a Content (i.e. Document or Post) object's ``contentImages`` list. :type image_id: str :return: binary content of Image :rtype: bytes """ # Testing Note: betamax==0.8.1 and/or betamax-serializers==0.2.0 cannot # handle testing the binary response content from this method. url = self.abs_url('core/v3/images/%s' % image_id) logger.debug('GET (binary) %s', url) res = self._requests.get(url) logger.debug('GET %s returned %d %s (%d bytes)', url, res.status_code, res.reason, len(res.content)) if res.status_code > 299: raise RequestFailedException(res) return res.content