Skip to content

Commit

Permalink
RequestMethod enum
Browse files Browse the repository at this point in the history
  • Loading branch information
dvolodin7 committed Mar 18, 2024
1 parent f03c5cc commit d89f6ef
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 126 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

To see unreleased changes, please see the [CHANGELOG on the main branch guide](https://github.com/gufolabs/gufo_http/blob/main/CHANGELOG.md).

## [Unreleased]

### Added

* `RequestMethod` enum.

### Changed

* `HttpClient.request` accepts `RequestMethod`.

## 0.2.0 - 2024-03-14

### Added
Expand Down
7 changes: 3 additions & 4 deletions src/async_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use crate::auth::{AuthMethod, BasicAuth, BearerAuth, GetAuthMethod};
use crate::error::HttpError;
use crate::headers::Headers;
use crate::method::{get_method, BROTLI, DEFLATE, GZIP};
use crate::method::{RequestMethod, BROTLI, DEFLATE, GZIP};
use crate::response::Response;
use pyo3::{
exceptions::{PyTypeError, PyValueError},
Expand Down Expand Up @@ -110,16 +110,15 @@ impl AsyncClient {
fn request<'a>(
&self,
py: Python<'a>,
method: usize,
method: &RequestMethod,
url: String,
headers: Option<HashMap<&str, &[u8]>>,
body: Option<Vec<u8>>,
) -> PyResult<&'a PyAny> {
// Get method
let method = get_method(method)?;
let req = py.allow_threads(|| -> Result<reqwest::RequestBuilder, HttpError> {
// Build request for method
let mut req = self.client.request(method, url);
let mut req = self.client.request((*method).into(), url);
// Add headers
if let Some(h) = headers {
for (k, v) in h {
Expand Down
4 changes: 3 additions & 1 deletion src/gufo/http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
HttpError,
RedirectError,
RequestError,
RequestMethod,
Response,
)
from .types import RequestMethod

__version__: str = "0.2.0"
__all__ = [
Expand All @@ -42,4 +43,5 @@
"BasicAuth",
"BearerAuth",
"RequestMethod",
"Response",
]
26 changes: 16 additions & 10 deletions src/gufo/http/_fast.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# ---------------------------------------------------------------------

# Python modules
from enum import Enum
from typing import Dict, Iterable, Optional, Tuple

# Exceptions
Expand All @@ -25,14 +26,19 @@ class BasicAuth(AuthBase):
class BearerAuth(AuthBase):
def __init__(self: "BearerAuth", token: str) -> None: ...

# Constants for request methods
GET: int
HEAD: int
OPTIONS: int
DELETE: int
POST: int
PUT: int
PATCH: int
# Request Method
class RequestMethod(Enum):
GET: int
HEAD: int
OPTIONS: int
DELETE: int
POST: int
PUT: int
PATCH: int

# def __getitem__(self: "RequestMethod", name: str) -> "RequestMethod": ...
@staticmethod
def get(name: str) -> Optional["RequestMethod"]: ...

# Constants for compression methods
DEFLATE: int
Expand Down Expand Up @@ -71,7 +77,7 @@ class AsyncClient(object):
) -> None: ...
async def request(
self: "AsyncClient",
method: int,
method: RequestMethod,
url: str,
headers: Optional[Dict[str, bytes]],
body: Optional[bytes],
Expand All @@ -91,7 +97,7 @@ class SyncClient(object):
) -> None: ...
def request(
self: "SyncClient",
method: int,
method: RequestMethod,
url: str,
headers: Optional[Dict[str, bytes]],
body: Optional[bytes],
Expand Down
41 changes: 24 additions & 17 deletions src/gufo/http/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,12 @@
from ._fast import (
BROTLI,
DEFLATE,
DELETE,
GET,
GZIP,
HEAD,
OPTIONS,
PATCH,
POST,
PUT,
AsyncClient,
AuthBase,
RequestMethod,
Response,
)
from .types import RequestMethod
from .util import merge_dict

MAX_REDIRECTS = 10
Expand Down Expand Up @@ -119,7 +112,7 @@ async def request(
Returns:
Response instance.
"""
return await self._client.request(method.value, url, headers, body)
return await self._client.request(method, url, headers, body)

async def get(
self: "HttpClient",
Expand All @@ -137,7 +130,9 @@ async def get(
Returns:
Response instance.
"""
return await self._client.request(GET, url, headers, None)
return await self._client.request(
RequestMethod.GET, url, headers, None
)

async def head(
self: "HttpClient",
Expand All @@ -155,7 +150,9 @@ async def head(
Returns:
Response instance.
"""
return await self._client.request(HEAD, url, headers, None)
return await self._client.request(
RequestMethod.HEAD, url, headers, None
)

async def options(
self: "HttpClient",
Expand All @@ -175,7 +172,9 @@ async def options(
Returns:
Response instance.
"""
return await self._client.request(OPTIONS, url, headers, None)
return await self._client.request(
RequestMethod.OPTIONS, url, headers, None
)

async def delete(
self: "HttpClient",
Expand All @@ -193,7 +192,9 @@ async def delete(
Returns:
Response instance.
"""
return await self._client.request(DELETE, url, headers, None)
return await self._client.request(
RequestMethod.DELETE, url, headers, None
)

async def post(
self: "HttpClient",
Expand All @@ -213,7 +214,9 @@ async def post(
Returns:
Response instance.
"""
return await self._client.request(POST, url, headers, body)
return await self._client.request(
RequestMethod.POST, url, headers, body
)

async def put(
self: "HttpClient",
Expand All @@ -233,7 +236,9 @@ async def put(
Returns:
Response instance.
"""
return await self._client.request(PUT, url, headers, body)
return await self._client.request(
RequestMethod.PUT, url, headers, body
)

async def patch(
self: "HttpClient",
Expand All @@ -253,7 +258,9 @@ async def patch(
Returns:
Response instance.
"""
return await self._client.request(PATCH, url, headers, body)
return await self._client.request(
RequestMethod.PATCH, url, headers, body
)


__all__ = ["HttpClient", "Response"]
__all__ = ["HttpClient"]
27 changes: 10 additions & 17 deletions src/gufo/http/sync_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,12 @@
from ._fast import (
BROTLI,
DEFLATE,
DELETE,
GET,
GZIP,
HEAD,
OPTIONS,
PATCH,
POST,
PUT,
AuthBase,
RequestMethod,
Response,
SyncClient,
)
from .types import RequestMethod
from .util import merge_dict

MAX_REDIRECTS = 10
Expand Down Expand Up @@ -119,7 +112,7 @@ def request(
Returns:
Response instance.
"""
return self._client.request(method.value, url, headers, body)
return self._client.request(method, url, headers, body)

def get(
self: "HttpClient",
Expand All @@ -137,7 +130,7 @@ def get(
Returns:
Response instance.
"""
return self._client.request(GET, url, headers, None)
return self._client.request(RequestMethod.GET, url, headers, None)

def head(
self: "HttpClient",
Expand All @@ -155,7 +148,7 @@ def head(
Returns:
Response instance.
"""
return self._client.request(HEAD, url, headers, None)
return self._client.request(RequestMethod.HEAD, url, headers, None)

def options(
self: "HttpClient",
Expand All @@ -175,7 +168,7 @@ def options(
Returns:
Response instance.
"""
return self._client.request(OPTIONS, url, headers, None)
return self._client.request(RequestMethod.OPTIONS, url, headers, None)

def delete(
self: "HttpClient",
Expand All @@ -193,7 +186,7 @@ def delete(
Returns:
Response instance.
"""
return self._client.request(DELETE, url, headers, None)
return self._client.request(RequestMethod.DELETE, url, headers, None)

def post(
self: "HttpClient",
Expand All @@ -213,7 +206,7 @@ def post(
Returns:
Response instance.
"""
return self._client.request(POST, url, headers, body)
return self._client.request(RequestMethod.POST, url, headers, body)

def put(
self: "HttpClient",
Expand All @@ -233,7 +226,7 @@ def put(
Returns:
Response instance.
"""
return self._client.request(PUT, url, headers, body)
return self._client.request(RequestMethod.PUT, url, headers, body)

def patch(
self: "HttpClient",
Expand All @@ -253,7 +246,7 @@ def patch(
Returns:
Response instance.
"""
return self._client.request(PATCH, url, headers, body)
return self._client.request(RequestMethod.PATCH, url, headers, body)


__all__ = ["HttpClient", "Response"]
__all__ = ["HttpClient"]
35 changes: 0 additions & 35 deletions src/gufo/http/types.py

This file was deleted.

8 changes: 1 addition & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@ fn gufo_http(py: Python, m: &PyModule) -> PyResult<()> {
m.add("ConnectError", py.get_type::<error::PyConnectError>())?;
m.add("RedirectError", py.get_type::<error::PyRedirectError>())?;
// Request methods
m.add("GET", method::GET)?;
m.add("HEAD", method::HEAD)?;
m.add("OPTIONS", method::OPTIONS)?;
m.add("DELETE", method::DELETE)?;
m.add("POST", method::POST)?;
m.add("PUT", method::PUT)?;
m.add("PATCH", method::PATCH)?;
m.add_class::<method::RequestMethod>()?;
// Compression methods
m.add("DEFLATE", method::DEFLATE)?;
m.add("GZIP", method::GZIP)?;
Expand Down
Loading

0 comments on commit d89f6ef

Please sign in to comment.