Skip to content

Commit

Permalink
Use httpx (#431)
Browse files Browse the repository at this point in the history
* Replace requests with httpx

* Use httpx instead of requests

* Use httpx in notebooks

* Add tox to dev requirements

* Revert change and remove unused import

* update changelog

Co-authored-by: Vincent Sarago <[email protected]>
  • Loading branch information
Rodrigo Almeida and vincentsarago authored Oct 12, 2021
1 parent 1b30e41 commit 2028c42
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ with COGReader(
```

* compare dataset bounds and tile bounds in TMS crs in `rio_tiler.io.base.SpatialMixin.tile_exists` method to allow dataset and TMS not compatible with WGS84 crs (https://github.com/cogeotiff/rio-tiler/pull/429)
* use `httpx` package instead of requests (author @rodrigoalmeida94, https://github.com/cogeotiff/rio-tiler/pull/431)

**breaking changes**

Expand Down
4 changes: 2 additions & 2 deletions docs/examples/Using-rio-tiler-mosaic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"source": [
"import json\n",
"import datetime\n",
"import requests\n",
"import httpx\n",
"\n",
"import morecantile\n",
"\n",
Expand Down Expand Up @@ -208,7 +208,7 @@
"}\n",
"\n",
"\n",
"data = requests.post(stac_endpoint, headers=headers, json=query).json()\n",
"data = httpx.post(stac_endpoint, headers=headers, json=query).json()\n",
"print(data[\"context\"])\n",
"print()\n",
"print(\"Example:\")\n",
Expand Down
3 changes: 0 additions & 3 deletions docs/examples/Using-tms.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,8 @@
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import morecantile\n",
"import requests\n",
"from ipyleaflet import (\n",
" Map,\n",
" basemaps,\n",
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ morecantile>=3.0,<3.1
pydantic
pystac>=0.5.4
rasterio>=1.1.7
requests
httpx
rio-color
importlib_resources>=1.1.0;python_version<'3.9'
4 changes: 2 additions & 2 deletions rio_tiler/io/stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from urllib.parse import urlparse

import attr
import httpx
import pystac
import requests
from morecantile import TileMatrixSet

from ..constants import WEB_MERCATOR_TMS, WGS84_CRS
Expand Down Expand Up @@ -51,7 +51,7 @@ def fetch(filepath: str, **kwargs: Any) -> Dict:
return json.loads(aws_get_object(bucket, key, **kwargs))

elif parsed.scheme in ["https", "http", "ftp"]:
return requests.get(filepath, **kwargs).json()
return httpx.get(filepath, **kwargs).json()

else:
with open(filepath, "r") as f:
Expand Down
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"pydantic",
"pystac>=0.5.4",
"rasterio>=1.1.7",
"requests",
"httpx",
"rio-color",
"importlib_resources>=1.1.0;python_version<'3.9'",
]
Expand All @@ -27,6 +27,7 @@
"pytest-benchmark",
"pytest-cov",
"rio-cogeo>=3.0.0a0",
"tox",
],
"dev": [
"pytest",
Expand All @@ -35,6 +36,7 @@
"pytest-asyncio",
"rio-cogeo>=3.0.0a0",
"pre-commit",
"tox",
],
"docs": ["nbconvert", "mkdocs", "mkdocs-material", "pygments", "mkdocs-jupyter"],
}
Expand Down
40 changes: 20 additions & 20 deletions tests/test_io_stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ def mock_rasterio_open(asset):


@patch("rio_tiler.io.stac.aws_get_object")
@patch("rio_tiler.io.stac.requests")
def test_fetch_stac(requests, s3_get):
@patch("rio_tiler.io.stac.httpx")
def test_fetch_stac(httpx, s3_get):
# Local path
with STACReader(STAC_PATH) as stac:
assert stac.minzoom == 0
assert stac.maxzoom == 24
assert stac.bounds
assert stac.filepath == STAC_PATH
assert stac.assets == ["red", "green", "blue"]
requests.assert_not_called()
httpx.assert_not_called()
s3_get.assert_not_called()

# Load from dict
Expand All @@ -49,25 +49,25 @@ def test_fetch_stac(requests, s3_get):
assert stac.maxzoom == 24
assert not stac.filepath
assert stac.assets == ["red", "green", "blue"]
requests.assert_not_called()
httpx.assert_not_called()
s3_get.assert_not_called()

# Exclude red
with STACReader(STAC_PATH, exclude_assets={"red"}) as stac:
assert stac.assets == ["green", "blue"]
requests.assert_not_called()
httpx.assert_not_called()
s3_get.assert_not_called()

# Only include red asset
with STACReader(STAC_PATH, include_assets={"red"}) as stac:
assert stac.assets == ["red"]
requests.assert_not_called()
httpx.assert_not_called()
s3_get.assert_not_called()

# Only include png
with STACReader(STAC_PATH, include_asset_types={"image/png"}) as stac:
assert "thumbnail" in stac.assets
requests.assert_not_called()
httpx.assert_not_called()
s3_get.assert_not_called()

# Include assets/types
Expand All @@ -77,14 +77,14 @@ def test_fetch_stac(requests, s3_get):
include_asset_types={"image/png"},
) as stac:
assert stac.assets == ["thumbnail"]
requests.assert_not_called()
httpx.assert_not_called()
s3_get.assert_not_called()

# No valid assets
with pytest.raises(MissingAssets):
with STACReader(STAC_PATH, include_assets={"B1"}) as stac:
pass
requests.assert_not_called()
httpx.assert_not_called()
s3_get.assert_not_called()

# HTTP
Expand All @@ -96,21 +96,21 @@ def json(self):
return json.loads(self.data)

with open(STAC_PATH, "r") as f:
requests.get.return_value = MockResponse(f.read())
httpx.get.return_value = MockResponse(f.read())

with STACReader("http://somewhereovertherainbow.io/mystac.json") as stac:
assert stac.assets == ["red", "green", "blue"]
requests.get.assert_called_once()
httpx.get.assert_called_once()
s3_get.assert_not_called()
requests.mock_reset()
httpx.mock_reset()

# S3
with open(STAC_PATH, "r") as f:
s3_get.return_value = f.read()

with STACReader("s3://somewhereovertherainbow.io/mystac.json") as stac:
assert stac.assets == ["red", "green", "blue"]
requests.assert_not_called()
httpx.assert_not_called()
s3_get.assert_called_once()
assert s3_get.call_args[0] == ("somewhereovertherainbow.io", "mystac.json")

Expand Down Expand Up @@ -387,8 +387,8 @@ def test_relative_assets():


@patch("rio_tiler.io.stac.aws_get_object")
@patch("rio_tiler.io.stac.requests")
def test_fetch_stac_client_options(requests, s3_get):
@patch("rio_tiler.io.stac.httpx")
def test_fetch_stac_client_options(httpx, s3_get):
# HTTP
class MockResponse:
def __init__(self, data):
Expand All @@ -398,17 +398,17 @@ def json(self):
return json.loads(self.data)

with open(STAC_PATH, "r") as f:
requests.get.return_value = MockResponse(f.read())
httpx.get.return_value = MockResponse(f.read())

with STACReader(
"http://somewhereovertherainbow.io/mystac.json",
fetch_options={"auth": ("user", "pass")},
) as stac:
assert stac.assets == ["red", "green", "blue"]
requests.get.assert_called_once()
assert requests.get.call_args[1]["auth"] == ("user", "pass")
httpx.get.assert_called_once()
assert httpx.get.call_args[1]["auth"] == ("user", "pass")
s3_get.assert_not_called()
requests.mock_reset()
httpx.mock_reset()

# S3
with open(STAC_PATH, "r") as f:
Expand All @@ -419,7 +419,7 @@ def json(self):
fetch_options={"request_pays": True},
) as stac:
assert stac.assets == ["red", "green", "blue"]
requests.assert_not_called()
httpx.assert_not_called()
s3_get.assert_called_once()
assert s3_get.call_args[1]["request_pays"]
assert s3_get.call_args[0] == ("somewhereovertherainbow.io", "mystac.json")

0 comments on commit 2028c42

Please sign in to comment.