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 curl GET mimicking the requests.get() signature.

This completes a curl GET command.

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 the curl GET command.

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 curl PUT command mimicking the requests.put() signature.

This completes a curl PUT command.

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 the curl PUT command.

Return type

List[str]

xyzspaces.curl.patch(url, data=None, **kwargs)[source]

Run curl PATCH command mimicking the requests.patch() signature.

This completes a curl PATCH command.

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 the curl PATCH command.

Return type

List[str]

xyzspaces.curl.post(url, data=None, json=None, **kwargs)[source]

Run curl POST command mimicking the requests.post() signature.

This completes a curl POST command.

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 the curl POST command.

Return type

List[str]

xyzspaces.curl.delete(url, **kwargs)[source]

Run curl DELETE command mimicking the requests.delete() signature.

This completes a curl DELETE command.

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 the curl DELETE command.

Return type

List[str]

xyzspaces.curl.command(url, method, params=None, **kwargs)[source]

Return a curl command equivalent from the params for requests.

This builds and returns a list of strings representing a curl command that can be directly passed to functions like subprocess.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 to requests.get(), with additional method parameter.

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 curl command (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 command and create the requests.models.Response object.

The Python’s subprocess module will be used for the executing a command.

In the requests.models.Response object will be initialized following attributes: _content: The response data from the stdout status_code: Simplified conversion from the subprocess.CompletedProcess.returncode to HTTP ones 200 ~ 0, 500 ~ >0.

Parameters

command (List[str]) – The curl to be executed in the List[str] type.

Returns

The generated requests.models.Response object.

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
[...]