Skip to content

Commit

Permalink
🚀 chore(curlify2): refactor to_curl function to a class method (#14)
Browse files Browse the repository at this point in the history
* 🔥 chore(pyproject.toml): remove responses from dev dependencies
The responses package is no longer needed in the project, so it has been removed from the dev dependencies.

* 🔧 chore(test_to_curl.py): refactor test_returns_curl_string_using_requests_module and test_returns_curl_string_using_httpx_module
The tests have been refactored to use the response object returned by the to_curl function instead of assigning it to a variable and then comparing it to a string. This improves the readability of the tests and makes it easier to identify the source of any failures.

* 🚀 chore(.gitignore): add .venv and dist directories to .gitignore
🚀 chore(curlify2): refactor to_curl function to a class method
🚀 chore(pyproject.toml): bump version to 2.0.0
The .venv and dist directories are now ignored by git. The to_curl function in curlify2 is now a class method, which improves readability and maintainability. The version in pyproject.toml is now 2.0.0 to reflect the changes made.

* 📦 chore(pyproject.toml): add responses package
The responses package is added to the project dependencies in the pyproject.toml file. This package is used for mocking HTTP requests in tests.

* 🐛 fix(curlify.py): fix body decoding
✨ feat(app.py): add endpoint to convert http request to curl command
The body decoding was fixed in curlify.py. The method now returns the decoded body if it is a bytes object. In app.py, a new endpoint was added to convert an HTTP request to a curl command. The endpoint receives a URL via a POST request, sends a GET request to the URL, and converts the request to a curl command using curlify. The command is then returned to the user.

* 🎨 style(README.md): refactor code to use Curlify class instead of to_curl function
The code has been refactored to use the Curlify class instead of the to_curl function from the curlify2 module. This improves readability and makes the code more concise.
  • Loading branch information
marcuxyz committed Jun 9, 2023
1 parent 415380c commit c59ccae
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 107 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
__pycache__
*.egg-info
.vscode
.vscode
.venv
dist
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@ $ poetry add curlify2
using **requests** module:

```python
import curlify2
from curlify2 import Curlify
import requests

URL = "https://run.mocky.io/v3/b0f4ffd8-6696-4f90-8bab-4a3bcad9ef3f"

request = requests.get(URL)
curl = curlify2.to_curl(request.request)

print(curl) # curl -X GET -H "User-Agent: python-requests/2.24.0" -H "Accept-Encoding: gzip, deflate" -H "Accept: */*" -H "Connection: keep-alive" -d 'None' https://run.mocky.io/v3/b0f4ffd8-6696-4f90-8bab-4a3bcad9ef3f
response = Curlify(request.request)

print(response.to_curl()) # curl -X GET -H "User-Agent: python-requests/2.24.0" -H "Accept-Encoding: gzip, deflate" -H "Accept: */*" -H "Connection: keep-alive" -d 'None' https://run.mocky.io/v3/b0f4ffd8-6696-4f90-8bab-4a3bcad9ef3f
```

using **httpx** module:
Expand All @@ -41,8 +40,7 @@ import httpx
URL = "https://run.mocky.io/v3/b0f4ffd8-6696-4f90-8bab-4a3bcad9ef3f"

request = httpx.get(URL)
curl = curlify2.to_curl(request.request)

print(curl) # curl -X GET -H "User-Agent: python-requests/2.24.0" -H "Accept-Encoding: gzip, deflate" -H "Accept: */*" -H "Connection: keep-alive" -d 'None' https://run.mocky.io/v3/b0f4ffd8-6696-4f90-8bab-4a3bcad9ef3f
response = Curlify(request.request)

print(response.to_curl()) # curl -X GET -H "User-Agent: python-requests/2.24.0" -H "Accept-Encoding: gzip, deflate" -H "Accept: */*" -H "Connection: keep-alive" -d 'None' https://run.mocky.io/v3/b0f4ffd8-6696-4f90-8bab-4a3bcad9ef3f
```
4 changes: 2 additions & 2 deletions curlify2/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .curlify import to_curl
from .curlify import Curlify

__all__ = ["to_curl"]
__all__ = ["Curlify"]
60 changes: 45 additions & 15 deletions curlify2/curlify.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,50 @@
def to_curl(request, compressed=False, verify=True) -> str:
"""to_curl function returns a string of curl to execute in shell.
We accept 'requests' and 'httpx' module.
"""
class Curlify:
def __init__(self, request, compressed=False, verify=True):
self.req = request
self.compressed = compressed
self.verify = verify

body = request.body if hasattr(request, "body") else request.read()
headers = [f'"{k}: {v}"' for k, v in request.headers.items()]
headers = " -H ".join(headers)
def to_curl(self) -> str:
"""to_curl function returns a string of curl to execute in shell.
We accept 'requests' and 'httpx' module.
"""
return self.quote()

if body and isinstance(body, bytes):
body = body.decode()
def headers(self) -> str:
"""organize headers
quote = f"curl -X {request.method} -H {headers} -d '{body}' {request.url}"
Returns:
str: return string of set headers
"""
headers = [f'"{k}: {v}"' for k, v in self.req.headers.items()]

if compressed:
quote += " --compressed"
if not verify:
quote += " --insecure"
return " -H ".join(headers)

return quote
def body(self):
if hasattr(self.req, "body"):
return self.req.body

return self.req.read()

def body_decode(self):
body = self.body()

if body and isinstance(body, bytes):
return body.decode()

return body

def quote(self) -> str:
"""build curl command
Returns:
str: string represents curl command
"""
quote = f"curl -X {self.req.method} -H {self.headers()} -d '{self.body_decode()}' {self.req.url}"

if self.compressed:
quote += " --compressed"
if not self.verify:
quote += " --insecure"

return quote
Loading

0 comments on commit c59ccae

Please sign in to comment.