Skip to content

Commit 7e88ffe

Browse files
committed
Removing request level initializatino
1 parent bb11676 commit 7e88ffe

File tree

4 files changed

+77
-161
lines changed

4 files changed

+77
-161
lines changed

runpod/endpoint/asyncio/asyncio_runner.py

Lines changed: 14 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,25 @@
1212
class Job:
1313
"""Class representing a job for an asynchronous endpoint"""
1414

15-
def __init__(self, endpoint_id: str, job_id: str, session: ClientSession,
16-
api_key: Optional[str] = None):
15+
def __init__(self, endpoint_id: str, job_id: str, session: ClientSession, headers: dict):
1716
"""
18-
Initialize a Job instance with optional API key.
17+
Initialize a Job instance.
1918
2019
Args:
2120
endpoint_id: The identifier for the endpoint.
2221
job_id: The identifier for the job.
2322
session: The aiohttp ClientSession.
24-
api_key: Optional API key for this specific job.
23+
headers: Headers to use for requests.
2524
"""
2625
from runpod import ( # pylint: disable=import-outside-toplevel,cyclic-import
27-
api_key as global_api_key,
2826
endpoint_url_base,
2927
)
3028

3129
self.endpoint_id = endpoint_id
3230
self.job_id = job_id
3331
self.session = session
3432
self.endpoint_url_base = endpoint_url_base
35-
36-
# Use provided API key or fall back to global
37-
effective_api_key = api_key or global_api_key
38-
39-
if effective_api_key is None:
40-
raise RuntimeError("API key must be provided or set globally")
41-
42-
self.headers = {
43-
"Content-Type": "application/json",
44-
"Authorization": f"Bearer {effective_api_key}",
45-
"X-Request-ID": job_id,
46-
}
33+
self.headers = headers
4734

4835
self.job_status = None
4936
self.job_output = None
@@ -155,82 +142,51 @@ def __init__(self, endpoint_id: str, session: ClientSession,
155142
if self.api_key is None:
156143
raise RuntimeError("API key must be provided or set globally")
157144

158-
# Keep headers attribute for backward compatibility
159145
self.headers = {
160146
"Content-Type": "application/json",
161147
"Authorization": f"Bearer {self.api_key}",
162148
}
163-
164-
def _get_headers(self, api_key: Optional[str] = None) -> dict:
165-
"""
166-
Get headers with the appropriate API key.
167-
168-
Raises:
169-
ValueError: If request API key conflicts with instance API key.
170-
"""
171-
# Check for conflicting API keys
172-
if api_key and self.api_key and api_key != self.api_key:
173-
raise ValueError(
174-
"Conflicting API keys: Request API key differs from instance API key. "
175-
"Use only one API key source to avoid security issues."
176-
)
177-
178-
effective_api_key = api_key or self.api_key
179-
return {
180-
"Content-Type": "application/json",
181-
"Authorization": f"Bearer {effective_api_key}",
182-
}
183149

184-
async def run(self, endpoint_input: dict, api_key: Optional[str] = None) -> Job:
150+
async def run(self, endpoint_input: dict) -> Job:
185151
"""
186152
Runs endpoint with specified input.
187153
188154
Args:
189155
endpoint_input: any dictionary with input
190-
api_key: Optional API key to use for this specific request.
191156
192157
Returns:
193158
Newly created job
194159
"""
195-
headers = self._get_headers(api_key)
196-
197160
async with self.session.post(
198-
self.endpoint_url, headers=headers, json={"input": endpoint_input}
161+
self.endpoint_url, headers=self.headers, json={"input": endpoint_input}
199162
) as resp:
200163
json_resp = await resp.json()
201164

202-
# Pass the API key to the Job instance
203-
return Job(self.endpoint_id, json_resp["id"], self.session,
204-
api_key=api_key or self.api_key)
165+
# Create job with endpoint's headers
166+
job_headers = self.headers.copy()
167+
job_headers["X-Request-ID"] = json_resp["id"]
168+
return Job(self.endpoint_id, json_resp["id"], self.session, job_headers)
205169

206-
async def health(self, api_key: Optional[str] = None) -> dict:
170+
async def health(self) -> dict:
207171
"""
208172
Checks health of endpoint
209173
210-
Args:
211-
api_key: Optional API key to use for this specific request.
212-
213174
Returns:
214175
Health of endpoint
215176
"""
216-
headers = self._get_headers(api_key)
217177
health_url = f"{self.endpoint_url_base}/{self.endpoint_id}/health"
218178

219-
async with self.session.get(health_url, headers=headers) as resp:
179+
async with self.session.get(health_url, headers=self.headers) as resp:
220180
return await resp.json()
221181

222-
async def purge_queue(self, api_key: Optional[str] = None) -> dict:
182+
async def purge_queue(self) -> dict:
223183
"""
224184
Purges queue of endpoint
225185
226-
Args:
227-
api_key: Optional API key to use for this specific request.
228-
229186
Returns:
230187
Purge status
231188
"""
232-
headers = self._get_headers(api_key)
233189
purge_url = f"{self.endpoint_url_base}/{self.endpoint_id}/purge-queue"
234190

235-
async with self.session.post(purge_url, headers=headers) as resp:
191+
async with self.session.post(purge_url, headers=self.headers) as resp:
236192
return await resp.json()

runpod/endpoint/runner.py

Lines changed: 21 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def __init__(self, api_key: Optional[str] = None):
5656

5757
def _request(
5858
self, method: str, endpoint: str, data: Optional[dict] = None,
59-
timeout: int = 10, api_key: Optional[str] = None
59+
timeout: int = 10
6060
):
6161
"""
6262
Make a request to the specified endpoint using the given HTTP method.
@@ -66,33 +66,17 @@ def _request(
6666
endpoint: The endpoint path to which the request will be made.
6767
data: The JSON payload to send with the request.
6868
timeout: The number of seconds to wait for the server to send data before giving up.
69-
api_key: Optional API key to use for this specific request.
7069
7170
Returns:
7271
The JSON response from the server.
7372
7473
Raises:
75-
ValueError: If request API key conflicts with instance API key.
7674
RuntimeError: If the response returns a 401 Unauthorized status.
7775
requests.HTTPError: If the response contains an unsuccessful status code.
7876
"""
79-
# Check for conflicting API keys
80-
if api_key and self.api_key and api_key != self.api_key:
81-
raise ValueError(
82-
"Conflicting API keys: Request API key differs from instance API key. "
83-
"Use only one API key source to avoid security issues."
84-
)
85-
86-
# Use request-specific API key if provided, otherwise use instance API key
87-
effective_api_key = api_key or self.api_key
88-
headers = self.headers if not api_key else {
89-
"Content-Type": "application/json",
90-
"Authorization": f"Bearer {effective_api_key}",
91-
}
92-
9377
url = f"{self.endpoint_url_base}/{endpoint}"
9478
response = self.rp_session.request(
95-
method, url, headers=headers, json=data, timeout=timeout
79+
method, url, headers=self.headers, json=data, timeout=timeout
9680
)
9781

9882
if response.status_code == 401:
@@ -101,13 +85,13 @@ def _request(
10185
response.raise_for_status()
10286
return response.json()
10387

104-
def post(self, endpoint: str, data: dict, timeout: int = 10, api_key: Optional[str] = None):
105-
"""Post to the endpoint with optional API key override."""
106-
return self._request("POST", endpoint, data, timeout, api_key=api_key)
88+
def post(self, endpoint: str, data: dict, timeout: int = 10):
89+
"""Post to the endpoint."""
90+
return self._request("POST", endpoint, data, timeout)
10791

108-
def get(self, endpoint: str, timeout: int = 10, api_key: Optional[str] = None):
109-
"""Get from the endpoint with optional API key override."""
110-
return self._request("GET", endpoint, timeout=timeout, api_key=api_key)
92+
def get(self, endpoint: str, timeout: int = 10):
93+
"""Get from the endpoint."""
94+
return self._request("GET", endpoint, timeout=timeout)
11195

11296

11397
# ---------------------------------------------------------------------------- #
@@ -116,30 +100,26 @@ def get(self, endpoint: str, timeout: int = 10, api_key: Optional[str] = None):
116100
class Job:
117101
"""Represents a job to be run on the Runpod service."""
118102

119-
def __init__(self, endpoint_id: str, job_id: str, client: RunPodClient,
120-
api_key: Optional[str] = None):
103+
def __init__(self, endpoint_id: str, job_id: str, client: RunPodClient):
121104
"""
122105
Initialize a Job instance with the given endpoint ID and job ID.
123106
124107
Args:
125108
endpoint_id: The identifier for the endpoint.
126109
job_id: The identifier for the job.
127110
client: An instance of the RunPodClient to make requests with.
128-
api_key: Optional API key for this specific job.
129111
"""
130112
self.endpoint_id = endpoint_id
131113
self.job_id = job_id
132114
self.rp_client = client
133-
self.api_key = api_key # Store job-specific API key
134115

135116
self.job_status = None
136117
self.job_output = None
137118

138119
def _fetch_job(self, source: str = "status") -> Dict[str, Any]:
139120
"""Returns the raw json of the status, raises an exception if invalid"""
140121
status_url = f"{self.endpoint_id}/{source}/{self.job_id}"
141-
# Pass the job-specific API key if available
142-
job_state = self.rp_client.get(endpoint=status_url, api_key=self.api_key)
122+
job_state = self.rp_client.get(endpoint=status_url)
143123

144124
if is_completed(job_state["status"]):
145125
self.job_status = job_state["status"]
@@ -197,8 +177,7 @@ def cancel(self, timeout: int = 3) -> Any:
197177
return self.rp_client.post(
198178
f"{self.endpoint_id}/cancel/{self.job_id}",
199179
data=None,
200-
timeout=timeout,
201-
api_key=self.api_key
180+
timeout=timeout
202181
)
203182

204183

@@ -225,13 +204,12 @@ def __init__(self, endpoint_id: str, api_key: Optional[str] = None):
225204
self.endpoint_id = endpoint_id
226205
self.rp_client = RunPodClient(api_key=api_key)
227206

228-
def run(self, request_input: Dict[str, Any], api_key: Optional[str] = None) -> Job:
207+
def run(self, request_input: Dict[str, Any]) -> Job:
229208
"""
230209
Run the endpoint with the given input.
231210
232211
Args:
233212
request_input: The input to pass into the endpoint.
234-
api_key: Optional API key to use for this specific request.
235213
236214
Returns:
237215
A Job instance for the run request.
@@ -241,65 +219,57 @@ def run(self, request_input: Dict[str, Any], api_key: Optional[str] = None) -> J
241219

242220
job_request = self.rp_client.post(
243221
f"{self.endpoint_id}/run",
244-
request_input,
245-
api_key=api_key
222+
request_input
246223
)
247-
return Job(self.endpoint_id, job_request["id"], self.rp_client, api_key=api_key)
224+
return Job(self.endpoint_id, job_request["id"], self.rp_client)
248225

249226
def run_sync(
250-
self, request_input: Dict[str, Any], timeout: int = 86400,
251-
api_key: Optional[str] = None
227+
self, request_input: Dict[str, Any], timeout: int = 86400
252228
) -> Dict[str, Any]:
253229
"""
254230
Run the endpoint with the given input synchronously.
255231
256232
Args:
257233
request_input: The input to pass into the endpoint.
258234
timeout: Maximum time to wait for the job to complete.
259-
api_key: Optional API key to use for this specific request.
260235
"""
261236
if not request_input.get("input"):
262237
request_input = {"input": request_input}
263238

264239
job_request = self.rp_client.post(
265240
f"{self.endpoint_id}/runsync",
266241
request_input,
267-
timeout=timeout,
268-
api_key=api_key
242+
timeout=timeout
269243
)
270244

271245
if job_request["status"] in FINAL_STATES:
272246
return job_request.get("output", None)
273247

274248
return Job(
275-
self.endpoint_id, job_request["id"], self.rp_client, api_key=api_key
249+
self.endpoint_id, job_request["id"], self.rp_client
276250
).output(timeout=timeout)
277251

278-
def health(self, timeout: int = 3, api_key: Optional[str] = None) -> Dict[str, Any]:
252+
def health(self, timeout: int = 3) -> Dict[str, Any]:
279253
"""
280254
Check the health of the endpoint (number/state of workers, number/state of requests).
281255
282256
Args:
283257
timeout: The number of seconds to wait for the server to respond before giving up.
284-
api_key: Optional API key to use for this specific request.
285258
"""
286259
return self.rp_client.get(
287260
f"{self.endpoint_id}/health",
288-
timeout=timeout,
289-
api_key=api_key
261+
timeout=timeout
290262
)
291263

292-
def purge_queue(self, timeout: int = 3, api_key: Optional[str] = None) -> Dict[str, Any]:
264+
def purge_queue(self, timeout: int = 3) -> Dict[str, Any]:
293265
"""
294266
Purges the endpoint's job queue and returns the result of the purge request.
295267
296268
Args:
297269
timeout: The number of seconds to wait for the server to respond before giving up.
298-
api_key: Optional API key to use for this specific request.
299270
"""
300271
return self.rp_client.post(
301272
f"{self.endpoint_id}/purge-queue",
302273
data=None,
303-
timeout=timeout,
304-
api_key=api_key
274+
timeout=timeout
305275
)

0 commit comments

Comments
 (0)