xyzspaces.curl module¶
This module contains functionality related to curl commands.
It provides methods for creating/executing curl commands which mimic
the requests signatures.
This module has been mainly designed to be used in the Api module for
logging purposes.
The curl module could be also used as stand alone:
Example:
>>> import xyzspaces.curl as curl
>>> command = curl.get(url='https://xkcd.com/552/info.0.json')
['curl', '--request', 'GET', 'https://xkcd.com/552/info.0.json']
>>> curl.execute(command)
b'{"month": "3", "num": 552, "link": "", "year": "2009", "news": "", "safe_title": "Correlation", "transcript": "[[A man is talking to a woman]]\\nMan: I used to think correlation implied causation.\\nMan: Then I took a statistics class. Now I don\'t.\\nWoman: Sounds like the class helped.\\nMan: Well, maybe.\\n{{Title text: Correlation doesn\'t imply causation, but it does waggle its eyebrows suggestively and gesture furtively while mouthing \'look over there\'.}}", "alt": "Correlation doesn\'t imply causation, but it does waggle its eyebrows suggestively and gesture furtively while mouthing \'look over there\'.", "img": "https://imgs.xkcd.com/comics/correlation.png", "title": "Correlation", "day": "6"}' # noqa: E501
[...]
- xyzspaces.curl.get(url, params=None, **kwargs)[source]¶
Run
curlGETmimicking therequests.get()signature.This completes a
curlGETcommand.- Parameters
url – The URL of the server including the path.
params – The query string params.
kwargs – Further keyword arguments that should match those expected by
requests.
- Returns
The
List[str]representation of thecurlGETcommand.- Return type
List[str]
Example:
>>> import xyzspaces.curl as curl >>> curl.get(url="https://xkcd.com/552/info.0.json") ['curl', '--request', 'GET', 'ttps://xkcd.com/552/info.0.json']
- xyzspaces.curl.put(url, data=None, **kwargs)[source]¶
Run
curlPUTcommand mimicking therequests.put()signature.This completes a
curlPUTcommand.- Parameters
url – The URL of the server including the path for the new object.
data – (optional) Dictionary, list of tuples, bytes, or file-like object.
kwargs – Further keyword arguments that should match those expected by
requests.
- Returns
The
List[str]representation of thecurlPUTcommand.- Return type
List[str]
- xyzspaces.curl.patch(url, data=None, **kwargs)[source]¶
Run
curlPATCHcommand mimicking therequests.patch()signature.This completes a
curlPATCHcommand.- Parameters
url – The URL of the server including the path for the new object.
data – (optional) Dictionary, list of tuples, bytes, or file-like object.
kwargs – Further keyword arguments that should match those expected by
requests.
- Returns
The
List[str]representation of thecurlPATCHcommand.- Return type
List[str]
- xyzspaces.curl.post(url, data=None, json=None, **kwargs)[source]¶
Run
curlPOSTcommand mimicking therequests.post()signature.This completes a
curlPOSTcommand.- Parameters
url – The URL of the server including the path for the new object.
data – (optional) Dictionary, list of tuples, bytes, or file-like object.
json – (optional) json data.
kwargs – Further keyword arguments that should match those expected by
requests.
- Returns
The
List[str]representation of thecurlPOSTcommand.- Return type
List[str]
- xyzspaces.curl.delete(url, **kwargs)[source]¶
Run
curlDELETEcommand mimicking therequests.delete()signature.This completes a
curlDELETEcommand.- Parameters
url – The URL of the server including the path.
kwargs – Further keyword arguments that should match those expected by
requests.
- Returns
The
List[str]representation of thecurlDELETEcommand.- Return type
List[str]
- xyzspaces.curl.command(url, method, params=None, **kwargs)[source]¶
Return a
curlcommand equivalent from the params forrequests.This builds and returns a list of strings representing a
curlcommand that can be directly passed to functions likesubprocess.check_output(). When joined with blanks into a single string it can also be used for logging or pasting to a terminal.To be used like
requests.get(), passing the same params for headers, cookies, etc. So the function signature is similar torequests.get(), with additionalmethodparameter.- Parameters
url (str) – The URL of the server including the path.
method (str) – The HTTP method name, e.g. “GET”, “PUT”, etc.
params (Optional[dict]) – The query string params.
kwargs (Optional[Mapping]) – Further keyword arguments that should match those expected by
requests.
- Returns
The generated
curlcommand (a list of strings).- Return type
List[str]
Example:
>>> import xyzspaces.curl as curl >>> curl.command(method="get", url="https://xkcd.com/552/info.0.json") ['curl', '--request', 'GET', 'https://xkcd.com/552/info.0.json']
- xyzspaces.curl.execute(command)[source]¶
Execute a
commandand create therequests.models.Responseobject.The Python’s
subprocessmodule will be used for the executing acommand.In the
requests.models.Responseobject will be initialized following attributes: _content: The response data from thestdoutstatus_code: Simplified conversion from thesubprocess.CompletedProcess.returncodetoHTTPones 200 ~ 0, 500 ~ >0.- Parameters
command (List[str]) – The
curlto be executed in theList[str]type.- Returns
The generated
requests.models.Responseobject.- Return type
requests.models.Response
Example:
>>> import xyzspaces.curl as curl >>> command = curl.get(url='https://xkcd.com/552/info.0.json') ['curl', '--request', 'GET', 'ttps://xkcd.com/552/info.0.json'] >>> curl.execute(command) b'{"month": "3", "num": 552, "link": "", "year": "2009", "news": "", "safe_title": "Correlation", "transcript": "[[A man is talking to a woman]]\\nMan: I used to think correlation implied causation.\\nMan: Then I took a statistics class. Now I don\'t.\\nWoman: Sounds like the class helped.\\nMan: Well, maybe.\\n{{Title text: Correlation doesn\'t imply causation, but it does waggle its eyebrows suggestively and gesture furtively while mouthing \'look over there\'.}}", "alt": "Correlation doesn\'t imply causation, but it does waggle its eyebrows suggestively and gesture furtively while mouthing \'look over there\'.", "img": "https://imgs.xkcd.com/comics/correlation.png", "title": "Correlation", "day": "6"}' # noqa: E501 [...]