Skip to content

Commit

Permalink
Configurable Timeout (#138)
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 08db966ce345de9f68e0b3f08d3eb05946391b94
  • Loading branch information
jjmckee authored and DescartesBuild committed Nov 20, 2023
1 parent 645ef5a commit c66a6dd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
4 changes: 4 additions & 0 deletions descarteslabs/vector/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
import geopandas as gpd
import pandas as pd

from . import __version__

API_HOST = os.getenv("VECTOR_API_HOST", "https://vector.descarteslabs.com")
TYPES = (gpd.GeoDataFrame, pd.DataFrame)
VECTOR_TIMEOUT = int(os.environ.get("VECTOR_TIMEOUT", "600"))
USERAGENT = f"dl-vector/{__version__}"


def get_token() -> str:
Expand Down
20 changes: 8 additions & 12 deletions descarteslabs/vector/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@
import requests
from descarteslabs.utils import Properties

from . import __version__
from .common import API_HOST, TYPES, get_token
from .common import API_HOST, TYPES, USERAGENT, VECTOR_TIMEOUT, get_token
from .util import backoff_wrapper, check_response, response_to_dataframe

REQUEST_TIMEOUT = 600
USERAGENT = f"dl-vector/{__version__}"


class Statistic(str, Enum):
"""
Expand Down Expand Up @@ -69,7 +65,7 @@ def add(
"User-Agent": USERAGENT,
},
files=files,
timeout=REQUEST_TIMEOUT,
timeout=VECTOR_TIMEOUT,
)

check_response(response, "add feature")
Expand Down Expand Up @@ -113,7 +109,7 @@ def query(
"aoi": aoi,
"columns": columns,
},
timeout=REQUEST_TIMEOUT,
timeout=VECTOR_TIMEOUT,
)
check_response(response, "query feature")

Expand Down Expand Up @@ -148,7 +144,7 @@ def _join(params: dict) -> Union[gpd.GeoDataFrame, pd.DataFrame]:
f"{API_HOST}/products/features/join",
headers={"Authorization": get_token(), "User-Agent": USERAGENT},
json=params,
timeout=REQUEST_TIMEOUT,
timeout=VECTOR_TIMEOUT,
)
check_response(response, "join feature")

Expand Down Expand Up @@ -298,7 +294,7 @@ def get(product_id: str, feature_id: str) -> Union[gpd.GeoDataFrame, pd.DataFram
f"{API_HOST}/products/{product_id}/features/{feature_id}",
headers={"Authorization": get_token(), "User-Agent": USERAGENT},
params={"format": "Parquet"},
timeout=REQUEST_TIMEOUT,
timeout=VECTOR_TIMEOUT,
)

check_response(response, "get feature")
Expand Down Expand Up @@ -351,7 +347,7 @@ def update(
"User-Agent": USERAGENT,
},
files=files,
timeout=REQUEST_TIMEOUT,
timeout=VECTOR_TIMEOUT,
)

check_response(response, "update feature")
Expand Down Expand Up @@ -404,7 +400,7 @@ def aggregate(
"aoi": aoi,
"columns": columns,
},
timeout=REQUEST_TIMEOUT,
timeout=VECTOR_TIMEOUT,
)
check_response(response, "aggregate feature")

Expand All @@ -427,7 +423,7 @@ def delete(product_id: str, feature_id: str):
response = requests.delete(
f"{API_HOST}/products/{product_id}/features/{feature_id}",
headers={"Authorization": get_token(), "User-Agent": USERAGENT},
timeout=REQUEST_TIMEOUT,
timeout=VECTOR_TIMEOUT,
)

check_response(response, "delete feature")
18 changes: 7 additions & 11 deletions descarteslabs/vector/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

import requests

from . import __version__
from .common import API_HOST, get_token
from .common import API_HOST, USERAGENT, VECTOR_TIMEOUT, get_token
from .models import GenericFeatureBaseModel, VectorBaseModel
from .util import backoff_wrapper, check_response
from .vector_exceptions import ClientException

REQUEST_TIMEOUT = 120
USERAGENT = f"dl-vector/{__version__}"


def _check_tags(tags: Union[List[str], None] = None):
if tags:
Expand Down Expand Up @@ -101,7 +97,7 @@ def create(
f"{API_HOST}/products/",
headers={"Authorization": get_token(), "User-Agent": USERAGENT},
json=request_json,
timeout=REQUEST_TIMEOUT,
timeout=VECTOR_TIMEOUT,
)
check_response(response, "create product")
return response.json()
Expand All @@ -128,13 +124,13 @@ def list(tags: Union[List[str], None] = None) -> List[dict]:
f"{API_HOST}/products/",
headers={"Authorization": get_token(), "User-Agent": USERAGENT},
params={"tags": ",".join(tags)},
timeout=REQUEST_TIMEOUT,
timeout=VECTOR_TIMEOUT,
)
else:
response = requests.get(
f"{API_HOST}/products/",
headers={"Authorization": get_token(), "User-Agent": USERAGENT},
timeout=REQUEST_TIMEOUT,
timeout=VECTOR_TIMEOUT,
)
check_response(response, "list products")
return response.json()
Expand All @@ -157,7 +153,7 @@ def get(product_id: str) -> dict:
response = requests.get(
f"{API_HOST}/products/{product_id}",
headers={"Authorization": get_token(), "User-Agent": USERAGENT},
timeout=REQUEST_TIMEOUT,
timeout=VECTOR_TIMEOUT,
)
check_response(response, "get product")
return response.json()
Expand Down Expand Up @@ -214,7 +210,7 @@ def update(
"owners": owners,
},
),
timeout=REQUEST_TIMEOUT,
timeout=VECTOR_TIMEOUT,
)
check_response(response, "update product")
return response.json()
Expand All @@ -237,6 +233,6 @@ def delete(product_id: str) -> None:
response = requests.delete(
f"{API_HOST}/products/{product_id}",
headers={"Authorization": get_token(), "User-Agent": USERAGENT},
timeout=REQUEST_TIMEOUT,
timeout=VECTOR_TIMEOUT,
)
check_response(response, "delete product")

0 comments on commit c66a6dd

Please sign in to comment.