def _caa_request(mbid, imageid=None, size=None, entitytype="release"): """ Make a CAA request. :param imageid: ``front``, ``back`` or a number from the listing obtained with :meth:`get_image_list`. :type imageid: str :param size: 250, 500 :type size: str or None :param entitytype: ``release`` or ``release-group`` :type entitytype: str """ # Construct the full URL for the request, including hostname and # query string. path = [entitytype, mbid] if imageid and size: path.append("%s-%s" % (imageid, size)) elif imageid: path.append(imageid) url = compat.urlunparse(( 'http', hostname, '/%s' % '/'.join(path), '', '', '' )) musicbrainz._log.debug("GET request for %s" % (url, )) # Set up HTTP request handler and URL opener. httpHandler = compat.HTTPHandler(debuglevel=0) handlers = [httpHandler] opener = compat.build_opener(*handlers) # Make request. req = musicbrainz._MusicbrainzHttpRequest("GET", url, None) # Useragent isn't needed for CAA, but we'll add it if it exists if musicbrainz._useragent != "": req.add_header('User-Agent', musicbrainz._useragent) musicbrainz._log.debug("requesting with UA %s" % musicbrainz._useragent) resp = musicbrainz._safe_read(opener, req, None) # TODO1: The content type declared by the CAA for JSON files is # 'applicaiton/octet-stream'. This is not useful to detect whether the # content is JSON, so default to decoding JSON if no imageid was supplied. # http://tickets.musicbrainz.org/browse/CAA-75 if imageid: # If we asked for an image, return the image return resp else: # Otherwise it's json return json.loads(resp)
def construct_url(url, params): parsed_url = parse_url(url) query = parsed_url.query encoded_params = encode_url_params(params) if encoded_params: if query: query = query + '&' + encoded_params else: query = encoded_params new_parsed_url = parsed_url._replace(query=query) new_url = requote(urlunparse(new_parsed_url)) return new_url
def get_full_url(self): # Only return the response's URL if the user hadn't set the Host # header if not self._r.headers.get('Host'): return self._r.url # If they did set it, retrieve it and reconstruct the expected domain host = self._r.headers['Host'] parsed = urlparse(self._r.url) # Reconstruct the URL as we expect it return urlunparse([ parsed.scheme, host, parsed.path, parsed.params, parsed.query, parsed.fragment ])
def update_uri(uri, construct=True, **kwargs): if isinstance(uri, ParseResult): # noinspection PyProtectedMember uri = dict(uri._asdict()) if type(uri) is not dict: raise ValueError('Unknown URI type') uri.update(kwargs) result = ParseResult(**uri) if not construct: return result return urlunparse(result)
def join_uri(base, uri, construct=True): p_uri = urlparse(uri) # Return if there is nothing to join if not p_uri.path: return base scheme, netloc, path, params, query, fragment = urlparse(base) # Switch to 'uri' parts _, _, _, params, query, fragment = p_uri path = join_path(path, p_uri.path) result = ParseResult(scheme, netloc, path, params, query, fragment) if not construct: return result # Construct from parts return urlunparse(result)
def _mb_request(path, method='GET', auth_required=AUTH_NO, client_required=False, args=None, data=None, body=None): """Makes a request for the specified `path` (endpoint) on /ws/2 on the globally-specified hostname. Parses the responses and returns the resulting object. `auth_required` and `client_required` control whether exceptions should be raised if the username/password and client are left unspecified, respectively. """ global parser_fun if args is None: args = {} else: args = dict(args) or {} if _useragent == "": raise UsageError("set a proper user-agent with " "set_useragent(\"application name\", \"application version\", \"contact info (preferably URL or email for your application)\")") if client_required: args["client"] = _client if ws_format != "xml": args["fmt"] = ws_format # Convert args from a dictionary to a list of tuples # so that the ordering of elements is stable for easy # testing (in this case we order alphabetically) # Encode Unicode arguments using UTF-8. newargs = [] for key, value in sorted(args.items()): if isinstance(value, compat.unicode): value = value.encode('utf8') newargs.append((key, value)) # Construct the full URL for the request, including hostname and # query string. url = compat.urlunparse(( 'http', hostname, '/ws/2/%s' % path, '', compat.urlencode(newargs), '' )) _log.debug("%s request for %s" % (method, url)) # Set up HTTP request handler and URL opener. httpHandler = compat.HTTPHandler(debuglevel=0) handlers = [httpHandler] # Add credentials if required. add_auth = False if auth_required == AUTH_YES: _log.debug("Auth required for %s" % url) if not user: raise UsageError("authorization required; " "use auth(user, pass) first") add_auth = True if auth_required == AUTH_IFSET and user: _log.debug("Using auth for %s because user and pass is set" % url) add_auth = True if add_auth: passwordMgr = _RedirectPasswordMgr() authHandler = _DigestAuthHandler(passwordMgr) authHandler.add_password("musicbrainz.org", (), user, password) handlers.append(authHandler) opener = compat.build_opener(*handlers) # Make request. req = _MusicbrainzHttpRequest(method, url, data) req.add_header('User-Agent', _useragent) _log.debug("requesting with UA %s" % _useragent) if body: req.add_header('Content-Type', 'application/xml; charset=UTF-8') elif not data and not req.has_header('Content-Length'): # Explicitly indicate zero content length if no request data # will be sent (avoids HTTP 411 error). req.add_header('Content-Length', '0') resp = _safe_read(opener, req, body) return parser_fun(resp)
def _mb_request(path, method='GET', auth_required=AUTH_NO, client_required=False, args=None, data=None, body=None): """Makes a request for the specified `path` (endpoint) on /ws/2 on the globally-specified hostname. Parses the responses and returns the resulting object. `auth_required` and `client_required` control whether exceptions should be raised if the username/password and client are left unspecified, respectively. """ global parser_fun if args is None: args = {} else: args = dict(args) or {} if _useragent == "": raise UsageError( "set a proper user-agent with " "set_useragent(\"application name\", \"application version\", \"contact info (preferably URL or email for your application)\")" ) if client_required: args["client"] = _client if ws_format != "xml": args["fmt"] = ws_format # Convert args from a dictionary to a list of tuples # so that the ordering of elements is stable for easy # testing (in this case we order alphabetically) # Encode Unicode arguments using UTF-8. newargs = [] for key, value in sorted(args.items()): if isinstance(value, compat.unicode): value = value.encode('utf8') newargs.append((key, value)) # Construct the full URL for the request, including hostname and # query string. url = compat.urlunparse(('http', hostname, '/ws/2/%s' % path, '', compat.urlencode(newargs), '')) _log.debug("%s request for %s" % (method, url)) # Set up HTTP request handler and URL opener. httpHandler = compat.HTTPHandler(debuglevel=0) handlers = [httpHandler] # Add credentials if required. add_auth = False if auth_required == AUTH_YES: _log.debug("Auth required for %s" % url) if not user: raise UsageError("authorization required; " "use auth(user, pass) first") add_auth = True if auth_required == AUTH_IFSET and user: _log.debug("Using auth for %s because user and pass is set" % url) add_auth = True if add_auth: passwordMgr = _RedirectPasswordMgr() authHandler = _DigestAuthHandler(passwordMgr) authHandler.add_password("musicbrainz.org", (), user, password) handlers.append(authHandler) opener = compat.build_opener(*handlers) # Make request. req = _MusicbrainzHttpRequest(method, url, data) req.add_header('User-Agent', _useragent) _log.debug("requesting with UA %s" % _useragent) if body: req.add_header('Content-Type', 'application/xml; charset=UTF-8') elif not data and not req.has_header('Content-Length'): # Explicitly indicate zero content length if no request data # will be sent (avoids HTTP 411 error). req.add_header('Content-Length', '0') resp = _safe_read(opener, req, body) return parser_fun(resp)
def _mb_request(path, method='GET', auth_required=False, client_required=False, args=None, data=None, body=None): """Makes a request for the specified `path` (endpoint) on /ws/2 on the globally-specified hostname. Parses the responses and returns the resulting object. `auth_required` and `client_required` control whether exceptions should be raised if the client and username/password are left unspecified, respectively. """ if args is None: args = {} else: args = dict(args) or {} if _useragent == "": raise UsageError("set a proper user-agent with " "set_useragent(\"application name\", \"application version\", \"contact info (preferably URL or email for your application)\")") if client_required: args["client"] = _client # Encode Unicode arguments using UTF-8. for key, value in args.items(): if isinstance(value, compat.unicode): args[key] = value.encode('utf8') # Construct the full URL for the request, including hostname and # query string. url = compat.urlunparse(( 'http', hostname, '/ws/2/%s' % path, '', compat.urlencode(args), '' )) _log.debug("%s request for %s" % (method, url)) # Set up HTTP request handler and URL opener. httpHandler = compat.HTTPHandler(debuglevel=0) handlers = [httpHandler] # Add credentials if required. if auth_required: _log.debug("Auth required for %s" % url) if not user: raise UsageError("authorization required; " "use auth(user, pass) first") passwordMgr = _RedirectPasswordMgr() authHandler = _DigestAuthHandler(passwordMgr) authHandler.add_password("musicbrainz.org", (), user, password) handlers.append(authHandler) opener = compat.build_opener(*handlers) # Make request. req = _MusicbrainzHttpRequest(method, url, data) req.add_header('User-Agent', _useragent) _log.debug("requesting with UA %s" % _useragent) if body: req.add_header('Content-Type', 'application/xml; charset=UTF-8') elif not data and not req.has_header('Content-Length'): # Explicitly indicate zero content length if no request data # will be sent (avoids HTTP 411 error). req.add_header('Content-Length', '0') f = _safe_open(opener, req, body) # Parse the response. try: return mbxml.parse_message(f) except UnicodeError as exc: raise ResponseError(cause=exc) except Exception as exc: if isinstance(exc, ETREE_EXCEPTIONS): raise ResponseError(cause=exc) else: raise