Skip to content

Commit 2224acd

Browse files
committed
Only one api key per request.
1 parent e4a4b37 commit 2224acd

1 file changed

Lines changed: 14 additions & 18 deletions

File tree

stac_planet_api/api.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,16 @@ def get_auth(credentials) -> httpx.BasicAuth:
9999
"""Create a httpx auth for the planet apis."""
100100
# Use the api key if available, otherwise pass through basic credentials from the user.
101101

102-
api_key = ""
103-
if PLANET_API_KEYS is not None:
104-
api_key = next(PLANET_API_KEYS)
105-
auth = httpx.BasicAuth(username=api_key, password="")
106-
elif credentials is not None:
102+
if credentials is not None:
103+
api_key = credentials.username
107104
auth = httpx.BasicAuth(
108105
username=credentials.username, password=credentials.password
109106
)
107+
108+
elif PLANET_API_KEYS is not None:
109+
api_key = next(PLANET_API_KEYS)
110+
auth = httpx.BasicAuth(username=api_key, password="")
111+
110112
else:
111113
raise fastapi.HTTPException(
112114
status_code=401, detail="Credentials were not provided."
@@ -115,11 +117,9 @@ def get_auth(credentials) -> httpx.BasicAuth:
115117
return auth, api_key
116118

117119

118-
def get_authenticated_client(credentials) -> httpx.Client:
120+
def get_authenticated_client(auth) -> httpx.Client:
119121
"""Create a httpx client with correct auth for the planet apis."""
120122

121-
auth, _ = get_auth(credentials)
122-
123123
return httpx.AsyncClient(
124124
auth=auth,
125125
verify=False,
@@ -269,22 +269,20 @@ async def post_search(
269269

270270
if token := search_request.token:
271271
token_parts = FERNET.decrypt(token).decode("utf-8").split("\\")
272-
print("TOKEN PARTS", token_parts)
273272

274273
credentials = fastapi.security.HTTPBasicCredentials(
275274
username=token_parts[1], password=""
276275
)
277276

278277
auth, api_key = get_auth(credentials)
278+
client = get_authenticated_client(auth=auth)
279279

280-
client = get_authenticated_client(credentials=credentials)
281280
planet_response = await client.get(token_parts[0])
282281

283282
else:
284283

285-
client = get_authenticated_client(credentials)
286-
287284
auth, api_key = get_auth(credentials)
285+
client = get_authenticated_client(auth)
288286

289287
search_request.limit = (
290288
MAX_ITEMS if search_request.limit > MAX_ITEMS else search_request.limit
@@ -395,10 +393,9 @@ async def get_item(
395393
Returns:
396394
Item: The item.
397395
"""
398-
client = get_authenticated_client(credentials)
399-
base_url = get_base_url(request)
400-
401396
auth, _ = get_auth(credentials)
397+
client = get_authenticated_client(auth)
398+
base_url = get_base_url(request)
402399

403400
planet_response = await client.get(
404401
f"https://api.planet.com/data/v1/item-types/{collection_id}/items/{item_id}",
@@ -430,10 +427,9 @@ async def get_item_thumbnail(
430427
Returns:
431428
Response: Thumbnail image
432429
"""
433-
client = get_authenticated_client(credentials)
434-
base_url = get_base_url(request)
435-
436430
auth, _ = get_auth(credentials)
431+
client = get_authenticated_client(auth)
432+
base_url = get_base_url(request)
437433

438434
planet_response = await client.get(
439435
f"https://api.planet.com/data/v1/item-types/{collection_id}/items/{item_id}",

0 commit comments

Comments
 (0)