Skip to content

Commit e712214

Browse files
authored
Allows for custom/any HTTP method (other than POST) (#76)
1 parent eb770a9 commit e712214

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

gql/transport/requests.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def __init__(
2929
timeout=None, # type: int
3030
verify=True, # type: bool
3131
retries=0, # type: int
32+
method="POST", # type: str
3233
**kwargs # type: Any
3334
):
3435
"""Initialize the transport with the given request parameters.
@@ -43,6 +44,7 @@ def __init__(
4344
the server's TLS certificate, or a string, in which case it must be a path
4445
to a CA bundle to use. (Default: True).
4546
:param retries: Pre-setup of the requests' Session for performing retries
47+
:param method: HTTP method used for requests. (Default: POST).
4648
:param kwargs: Optional arguments that ``request`` takes. These can be seen at the :requests_: source code
4749
or the official :docs_:
4850
@@ -56,6 +58,7 @@ def __init__(
5658
self.use_json = use_json
5759
self.default_timeout = timeout
5860
self.verify = verify
61+
self.method = method
5962
self.kwargs = kwargs
6063

6164
# Creating a session that can later be re-use to configure custom mechanisms
@@ -101,7 +104,7 @@ def execute(self, document, variable_values=None, timeout=None):
101104
post_args.update(self.kwargs)
102105

103106
# Using the created session to perform requests
104-
response = self.session.post(self.url, **post_args) # type: ignore
107+
response = self.session.request(self.method, self.url, **post_args) # type: ignore
105108
try:
106109
result = response.json()
107110
if not isinstance(result, dict):

tests/test_client.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,37 @@ def test_http_transport_verify_error(http_transport_query):
167167
assert "Unverified HTTPS request is being made to host" in str(record[0].message)
168168

169169

170+
def test_http_transport_specify_method_valid(http_transport_query):
171+
client = Client(
172+
transport=RequestsHTTPTransport(
173+
url="https://countries.trevorblades.com/",
174+
use_json=True,
175+
headers={"Content-type": "application/json"},
176+
method="POST",
177+
)
178+
)
179+
180+
result = client.execute(http_transport_query)
181+
client.close()
182+
assert result is not None
183+
184+
185+
def test_http_transport_specify_method_invalid(http_transport_query):
186+
client = Client(
187+
transport=RequestsHTTPTransport(
188+
url="https://countries.trevorblades.com/",
189+
use_json=True,
190+
headers={"Content-type": "application/json"},
191+
method="GET",
192+
)
193+
)
194+
195+
with pytest.raises(Exception) as exc_info:
196+
client.execute(http_transport_query)
197+
client.close()
198+
assert "400 Client Error: Bad Request for url" in str(exc_info.value)
199+
200+
170201
def test_gql():
171202
sample_path = os.path.join(
172203
os.path.dirname(os.path.abspath(__file__)),

0 commit comments

Comments
 (0)