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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
import io
import json
import logging
import time
import urllib.parse
from functools import cached_property, wraps
from typing import (
    TYPE_CHECKING,
    Any,
    Callable,
    Generator,
    Generic,
    Iterable,
    Type,
    TypeVar,
)

import grpc
import requests
from google.protobuf.message import Message

from . import beaker_pb2 as pb2
from . import beaker_pb2_grpc
from .config import Config
from .exceptions import *
from .exceptions import BeakerQueueNotFound
from .utils import pb2_to_dict

if TYPE_CHECKING:
    from .client import Beaker


T = TypeVar("T")


class RpcMethod(Generic[T]):
    def __init__(self, method: grpc.UnaryUnaryMultiCallable):
        self.method = method

    def __call__(
        self,
        request: Message,
        exceptions_for_status: dict[grpc.StatusCode, Exception] | None = None,
        **kwargs,
    ) -> T:
        try:
            return self.method(request, **kwargs)
        except RpcError as e:
            raise coerce_rpc_error(e, exceptions_for_status)

    @property
    def name(self) -> str:
        return self.method._method.decode()  # type: ignore


class RpcStreamingMethod(Generic[T]):
    def __init__(self, method: grpc.UnaryStreamMultiCallable):
        self.method = method

    def __call__(
        self,
        request: Message,
        exceptions_for_status: dict[grpc.StatusCode, Exception] | None = None,
        **kwargs,
    ) -> Generator[T, None, None]:
        try:
            yield from self.method(request, **kwargs)
        except RpcError as e:
            raise coerce_rpc_error(e, exceptions_for_status)

    @property
    def name(self) -> str:
        return self.method._method.decode()  # type: ignore


class RpcBidirectionalStreamingMethod(Generic[T]):
    def __init__(self, method: grpc.StreamStreamMultiCallable):
        self.method = method

    def __call__(
        self,
        request: Iterable[Message],
        exceptions_for_status: dict[grpc.StatusCode, Exception] | None = None,
        **kwargs,
    ) -> Generator[T, None, None]:
        try:
            yield from self.method(request, **kwargs)
        except RpcError as e:
            raise coerce_rpc_error(e, exceptions_for_status)

    @property
    def name(self) -> str:
        return self.method._method.decode()  # type: ignore


def coerce_rpc_error(
    e: RpcError, exceptions_for_status: dict[grpc.StatusCode, Exception] | None = None
):
    if isinstance(e, grpc.Call):
        status = e.code()
        if exceptions_for_status is not None and status in exceptions_for_status:
            return exceptions_for_status[status]
        elif status == grpc.StatusCode.UNIMPLEMENTED:
            return NotImplementedError(e.details())
        elif status == grpc.StatusCode.PERMISSION_DENIED:
            return BeakerPermissionsError(e.details())
        elif status == grpc.StatusCode.NOT_FOUND:
            return BeakerNotFoundError(e.details())
        elif status == grpc.StatusCode.INVALID_ARGUMENT:
            return BeakerClientError(e.details())
        elif status == grpc.StatusCode.INTERNAL:
            if "RST_STREAM" in e.details():
                return BeakerStreamConnectionClosedError(e.details())
            else:
                return BeakerServerError(e.details())
        elif status == grpc.StatusCode.UNAVAILABLE:
            return BeakerServerUnavailableError(e.details())
    return e


class ServiceClient:
    MAX_PAGE_SIZE = 50

    def __init__(self, beaker: "Beaker"):
        self.beaker = beaker
        self._base_url = f"{self.config.agent_address}/api/{self.beaker.API_VERSION}"

    @property
    def config(self) -> Config:
        return self.beaker.config

    @property
    def logger(self) -> logging.Logger:
        return self.beaker.logger

    @property
    def service(self) -> beaker_pb2_grpc.BeakerStub:
        return self.beaker.service

    def http_request(
        self,
        resource: str,
        method: str = "GET",
        query: dict[str, Any] | None = None,
        data: Any | None = None,
        exceptions_for_status: dict[int, Exception] | None = None,
        headers: dict[str, str] | None = None,
        token: str | None = None,
        base_url: str | None = None,
        stream: bool = False,
        timeout: float | tuple[float, float] | None = None,
    ) -> requests.Response:
        def make_request(session: requests.Session) -> requests.Response:
            # Build URL.
            url = f"{base_url or self._base_url}/{resource}"
            if query is not None:
                url = url + "?" + urllib.parse.urlencode(query)

            # Populate headers.
            default_headers = {
                "Authorization": f"Bearer {token or self.config.user_token}",
                "Content-Type": "application/json",
                "User-Agent": self.beaker.user_agent,
            }
            if headers is not None:
                default_headers.update(headers)

            # Validate request data.
            request_data: str | bytes | io.BufferedReader | io.BytesIO | None = None
            if isinstance(data, Message):
                request_data = json.dumps(pb2_to_dict(data))
            elif isinstance(data, dict):
                request_data = json.dumps(data)
            elif isinstance(data, (str, bytes, io.BufferedReader, io.BytesIO)):
                request_data = data
            elif data is not None:
                raise TypeError(f"Unexpected type for 'data', got {type(data)}")

            # Log request at DEBUG.
            if isinstance(request_data, str):
                self.logger.debug("SEND %s %s - %s", method, url, request_data)
            elif isinstance(request_data, bytes):
                self.logger.debug("SEND %s %s - %d bytes", method, url, len(request_data))
            elif request_data is not None:
                self.logger.debug("SEND %s %s - ? bytes", method, url)
            else:
                self.logger.debug("SEND %s %s", method, url)

            # Make request.
            response = getattr(session, method.lower())(
                url,
                headers=default_headers,
                data=request_data,
                stream=stream,
                timeout=timeout or self.beaker.TIMEOUT,
            )

            # Log response at DEBUG.
            if (
                not stream
                and self.logger.isEnabledFor(logging.DEBUG)
                and len(response.content) < 1024
                and response.text
            ):
                self.logger.debug("RECV %s %s %s - %s", method, url, response, response.text)
            else:
                self.logger.debug("RECV %s %s %s", method, url, response)

            status_code = response.status_code

            try:
                response.raise_for_status()
            except requests.exceptions.HTTPError:
                # Try parsing error message from the response
                msg: str | None = None
                if response.text:
                    try:
                        msg = json.loads(response.text)["message"]
                    except (TypeError, KeyError, json.JSONDecodeError):
                        pass

                # HACK: sometimes Beaker doesn't use the right error code, so we try to guess based
                # on the message.
                if status_code == 400 and msg is not None and "already in use" in msg:
                    status_code = 409

                if exceptions_for_status is not None and status_code in exceptions_for_status:
                    raise exceptions_for_status[status_code]

                if msg is not None and status_code is not None and 400 <= status_code < 500:
                    # Raise a BeakerError if we're misusing the API (4xx error code).
                    if status_code == 403:
                        raise BeakerPermissionsError(msg)
                    else:
                        raise BeakerError(f"[code={status_code}] {msg}")
                elif msg is not None:
                    raise HTTPError(msg, response=response)  # type: ignore
                elif status_code == 405:
                    raise BeakerClientError(f"[code={status_code}] method not allowed")
                else:
                    raise

            return response

        if method in {"HEAD", "GET"}:
            # We assume HEAD and GET calls won't modify state, so they're
            # safe to retry for any recoverable error.
            make_request = self._retriable()(make_request)

        with self.beaker.http_session() as session:
            return make_request(session)

    def rpc_request(
        self,
        method: RpcMethod[T],
        request: Message,
        exceptions_for_status: dict[grpc.StatusCode, Exception] | None = None,
        retriable: bool | None = None,
    ) -> T:
        self.logger.debug("Calling unary-unary RPC method '%s'", method.name)

        if retriable is None:
            request_name = request.__class__.__name__
            retriable = (
                request_name.startswith("Get")
                or request_name.startswith("List")
                or request_name.startswith("Resolve")
            )

        method_to_call = self._retriable()(method) if retriable else method
        return method_to_call(
            request,
            exceptions_for_status=exceptions_for_status,
            metadata=self._rpc_call_metadata,
        )

    def rpc_paged_request(
        self,
        method: RpcMethod[T],
        request: Message,
        exceptions_for_status: dict[grpc.StatusCode, Exception] | None = None,
        retriable: bool = True,
    ) -> Generator[T, None, None]:
        self.logger.debug("Calling paged unary-unary RPC method '%s'", method.name)

        method_to_call = self._retriable()(method) if retriable else method
        response = method_to_call(
            request, exceptions_for_status=exceptions_for_status, metadata=self._rpc_call_metadata
        )
        yield response

        next_page_token = response.next_page_token  # type: ignore
        while next_page_token:
            request.next_page_token = next_page_token  # type: ignore
            response = method_to_call(
                request,
                exceptions_for_status=exceptions_for_status,
                metadata=self._rpc_call_metadata,
            )
            next_page_token = response.next_page_token  # type: ignore
            yield response

    def rpc_streaming_request(
        self,
        method: RpcStreamingMethod[T],
        request: Message,
        exceptions_for_status: dict[grpc.StatusCode, Exception] | None = None,
    ) -> Generator[T, None, None]:
        self.logger.debug("Calling unary-streaming RPC method '%s'", method.name)
        yield from method(
            request,
            exceptions_for_status=exceptions_for_status,
            metadata=self._rpc_call_metadata,
        )

    def rpc_bidirectional_streaming_request(
        self,
        method: RpcBidirectionalStreamingMethod[T],
        request: Iterable[Message],
        exceptions_for_status: dict[grpc.StatusCode, Exception] | None = None,
    ) -> Generator[T, None, None]:
        self.logger.debug("Calling bidirectional-streaming RPC method '%s'", method.name)
        yield from method(
            request,
            exceptions_for_status=exceptions_for_status,
            metadata=self._rpc_call_metadata,
        )

    def resolve_org_id(self, org: str | pb2.Organization | None = None) -> str:
        if isinstance(org, pb2.Organization):
            return org.id

        if org is None:
            if self.config.default_org is not None:
                org = self.config.default_org
            elif self.config.default_workspace is not None and "/" in self.config.default_workspace:
                org = self.config.default_workspace.split("/", 1)[0]

        if org is None:
            raise BeakerOrganizationNotSet(
                "default organization not set, please specify the org name"
            )

        try:
            return self.rpc_request(
                RpcMethod[pb2.ResolveOrganizationNameResponse](
                    self.service.ResolveOrganizationName
                ),
                pb2.ResolveOrganizationNameRequest(organization_name=org),
                exceptions_for_status={grpc.StatusCode.NOT_FOUND: BeakerOrganizationNotFound(org)},
            ).organization_id
        except BeakerOrganizationNotFound:
            return org  # this could be an org ID.

    def resolve_user_id(self, user: str | pb2.User | None = None) -> str:
        if isinstance(user, pb2.User):
            return user.id

        if user is None:
            return self.beaker.user.get().id

        try:
            return self.rpc_request(
                RpcMethod[pb2.ResolveUserNameResponse](self.service.ResolveUserName),
                pb2.ResolveUserNameRequest(user_name=user),
                exceptions_for_status={grpc.StatusCode.NOT_FOUND: BeakerUserNotFound(user)},
            ).user_id
        except BeakerUserNotFound:
            return user  # this could be a user ID

    def resolve_workspace_id(self, workspace: str | pb2.Workspace | None = None) -> str:
        if isinstance(workspace, pb2.Workspace):
            return workspace.id

        if workspace is None:
            if self._default_workspace_id is not None:
                return self._default_workspace_id
            else:
                raise BeakerWorkspaceNotSet(
                    "'workspace' argument required since default workspace not set"
                )

        if "/" not in workspace:
            return workspace

        owner_name, workspace_name = workspace.split("/", 1)
        return self.rpc_request(
            RpcMethod[pb2.ResolveWorkspaceNameResponse](self.service.ResolveWorkspaceName),
            pb2.ResolveWorkspaceNameRequest(owner_name=owner_name, workspace_name=workspace_name),
            exceptions_for_status={grpc.StatusCode.NOT_FOUND: BeakerWorkspaceNotFound(workspace)},
        ).workspace_id

    def resolve_image_id(self, image: str | pb2.Image) -> str:
        if isinstance(image, pb2.Image):
            return image.id

        if "/" not in image:
            return image

        author_name, image_name = image.split("/", 1)
        return self.rpc_request(
            RpcMethod[pb2.ResolveImageNameResponse](self.service.ResolveImageName),
            pb2.ResolveImageNameRequest(author_name=author_name, image_name=image_name),
            exceptions_for_status={grpc.StatusCode.NOT_FOUND: BeakerImageNotFound(image)},
        ).image_id

    def resolve_experiment_id(self, experiment: str | pb2.Experiment | pb2.Workload) -> str:
        if isinstance(experiment, pb2.Experiment):
            return experiment.id

        if isinstance(experiment, pb2.Workload) and not self.beaker.workload.is_experiment(
            experiment
        ):
            raise ValueError(
                f"workload '{self.resolve_workload_id(experiment)}' is not an experiment"
            )

        return self.resolve_workload_id(experiment)

    def resolve_workload_id(self, workload: str | pb2.Workload) -> str:
        if isinstance(workload, pb2.Workload):
            if self.beaker.workload.is_experiment(workload):
                return workload.experiment.id
            elif self.beaker.workload.is_environment(workload):
                return workload.environment.id
            else:
                raise ValueError(f"workload is neither an experiment or an environment: {workload}")

        if "/" not in workload:
            return workload

        author_name, workload_name = workload.split("/", 1)
        return self.rpc_request(
            RpcMethod[pb2.ResolveWorkloadNameResponse](self.service.ResolveWorkloadName),
            pb2.ResolveWorkloadNameRequest(author_name=author_name, workload_name=workload_name),
            exceptions_for_status={grpc.StatusCode.NOT_FOUND: BeakerWorkloadNotFound(workload)},
        ).workload_id

    def resolve_dataset_id(self, dataset: str | pb2.Dataset) -> str:
        if isinstance(dataset, pb2.Dataset):
            return dataset.id

        if "/" not in dataset:
            return dataset

        author_name, dataset_name = dataset.split("/", 1)
        return self.rpc_request(
            RpcMethod[pb2.ResolveDatasetNameResponse](self.service.ResolveDatasetName),
            pb2.ResolveDatasetNameRequest(author_name=author_name, dataset_name=dataset_name),
            exceptions_for_status={grpc.StatusCode.NOT_FOUND: BeakerDatasetNotFound(dataset)},
        ).dataset_id

    def resolve_cluster_id(self, cluster: str | pb2.Cluster) -> str:
        if isinstance(cluster, pb2.Cluster):
            return cluster.id

        if "/" not in cluster:
            return cluster

        owner_name, cluster_name = cluster.split("/", 1)
        return self.rpc_request(
            RpcMethod[pb2.ResolveClusterNameResponse](self.service.ResolveClusterName),
            pb2.ResolveClusterNameRequest(owner_name=owner_name, cluster_name=cluster_name),
            exceptions_for_status={grpc.StatusCode.NOT_FOUND: BeakerClusterNotFound(cluster)},
        ).cluster_id

    def resolve_node_id(self, node: str | pb2.Node) -> str:
        if isinstance(node, pb2.Node):
            return node.id
        # TODO: currently there's no ResolveNodeName method.
        if "/" in node:
            raise ValueError("invalid node ID")
        return node

    def resolve_budget_id(self, budget: str) -> str:
        if "/" not in budget:
            return budget

        org_name, budget_name = budget.split("/", 1)
        return self.rpc_request(
            RpcMethod[pb2.ResolveBudgetNameResponse](self.service.ResolveBudgetName),
            pb2.ResolveBudgetNameRequest(organization_name=org_name, budget_name=budget_name),
            exceptions_for_status={
                grpc.StatusCode.NOT_FOUND: BeakerBudgetNotFound(budget),
            },
        ).budget_id

    def resolve_group_id(self, group: str | pb2.Group) -> str:
        if isinstance(group, pb2.Group):
            return group.id
        # TODO: currently there's no ResolveGroupName method.
        if "/" in group:
            raise ValueError("invalid group ID")
        return group

    def resolve_queue_id(self, queue: str | pb2.Queue) -> str:
        if isinstance(queue, pb2.Queue):
            return queue.id

        if "/" not in queue:
            return queue

        author_name, queue_name = queue.split("/", 1)
        return self.rpc_request(
            RpcMethod[pb2.ResolveQueueNameResponse](self.service.ResolveQueueName),
            pb2.ResolveQueueNameRequest(author_name=author_name, queue_name=queue_name),
            exceptions_for_status={grpc.StatusCode.NOT_FOUND: BeakerQueueNotFound(queue)},
        ).queue_id

    @cached_property
    def _default_workspace_id(self) -> str | None:
        if (workspace_name := self.config.default_workspace) is not None:
            return self.resolve_workspace_id(workspace_name)
        return None

    @property
    def _rpc_call_metadata(self) -> list[tuple[str, str]]:
        return [
            ("authorization", f"bearer {self.config.user_token}"),
            ("user-agent", self.beaker.user_agent),
        ]

    def _validate_beaker_name(self, name: str) -> str:
        if (
            not name
            or name.startswith("-")
            or not name.replace("-", "").replace("_", "").replace(".", "").isalnum()
        ):
            raise ValueError(
                f"Invalid name '{name}'. Beaker names can only contain letters, "
                f"digits, periods, dashes, and underscores, and cannot start with a dash."
            )
        return name

    def _url_quote(self, id: str) -> str:
        return urllib.parse.quote(id, safe="")

    def _log_and_wait(
        self, retries_so_far: int, err: Exception, log_level: int = logging.WARNING
    ) -> None:
        retry_in = min(self.beaker.BACKOFF_FACTOR * (2**retries_so_far), self.beaker.BACKOFF_MAX)
        self.logger.log(
            log_level,
            "Request failed with retriable error: %s: %s\nRetrying in %d second(s)...",
            err.__class__.__name__,
            err,
            retry_in,
        )
        time.sleep(retry_in)

    def _retriable(
        self,
        on_failure: Callable[..., None] | None = None,
        recoverable_errors: tuple[Type[Exception], ...] = (
            RequestException,
            BeakerServerError,
        ),
        expected_errors: tuple[Type[Exception], ...] = (
            BeakerStreamConnectionClosedError,
            BeakerServerUnavailableError,
        ),
    ):
        """
        Use to make a service client method more robust by allowing retries.
        """

        def parametrize_decorator(func: Callable[..., T]) -> Callable[..., T]:
            @wraps(func)
            def retriable_method(*args, **kwargs) -> T:
                retries = 0
                while True:
                    try:
                        return func(*args, **kwargs)
                    except expected_errors as err:
                        if on_failure is not None:
                            on_failure()
                        self._log_and_wait(1, err, log_level=logging.DEBUG)
                    except recoverable_errors as err:
                        if retries < self.beaker.MAX_RETRIES:
                            if on_failure is not None:
                                on_failure()
                            self._log_and_wait(retries, err)
                            retries += 1
                        else:
                            raise

            return retriable_method

        return parametrize_decorator