|
| 1 | +from gradient import config |
| 2 | +from .common import CreateResource, DeleteResource, ListResources, GetResource |
| 3 | +from .. import serializers |
| 4 | + |
| 5 | + |
| 6 | +class GetNotebookApiUrlMixin(object): |
| 7 | + def _get_api_url(self, use_vpc=False): |
| 8 | + return config.config.CONFIG_HOST |
| 9 | + |
| 10 | + |
| 11 | +class CreateNotebook(GetNotebookApiUrlMixin, CreateResource): |
| 12 | + SERIALIZER_CLS = serializers.NotebookSchema |
| 13 | + |
| 14 | + def get_request_url(self, **kwargs): |
| 15 | + return "notebooks/createNotebook" |
| 16 | + |
| 17 | + def _process_instance_dict(self, instance_dict): |
| 18 | + # the API requires this field but marshmallow does not create it if it's value is None |
| 19 | + instance_dict.setdefault("containerId") |
| 20 | + return instance_dict |
| 21 | + |
| 22 | + |
| 23 | +class DeleteNotebook(GetNotebookApiUrlMixin, DeleteResource): |
| 24 | + def get_request_url(self, **kwargs): |
| 25 | + return "notebooks/v2/deleteNotebook" |
| 26 | + |
| 27 | + def _get_request_json(self, kwargs): |
| 28 | + notebook_id = kwargs["id"] |
| 29 | + d = {"notebookId": notebook_id} |
| 30 | + return d |
| 31 | + |
| 32 | + def _send_request(self, client, url, json_data=None): |
| 33 | + response = client.post(url, json=json_data) |
| 34 | + return response |
| 35 | + |
| 36 | + |
| 37 | +class GetNotebook(GetNotebookApiUrlMixin, GetResource): |
| 38 | + def get_request_url(self, **kwargs): |
| 39 | + notebook_id = kwargs["id"] |
| 40 | + url = "notebooks/{}/getNotebook".format(notebook_id) |
| 41 | + return url |
| 42 | + |
| 43 | + def _parse_object(self, data, **kwargs): |
| 44 | + # this ugly hack is here because marshmallow disallows reading value into `id` field |
| 45 | + # if JSON's field was named differently (despite using load_from in schema definition) |
| 46 | + data["id"] = data["handle"] |
| 47 | + |
| 48 | + serializer = serializers.NotebookSchema() |
| 49 | + notebooks = serializer.get_instance(data) |
| 50 | + return notebooks |
| 51 | + |
| 52 | + |
| 53 | +class ListNotebooks(GetNotebookApiUrlMixin, ListResources): |
| 54 | + def get_request_url(self, **kwargs): |
| 55 | + return "notebooks/getNotebooks" |
| 56 | + |
| 57 | + def _parse_objects(self, data, **kwargs): |
| 58 | + notebook_dicts = data["notebookList"] |
| 59 | + # this ugly hack is here because marshmallow disallows reading value into `id` field |
| 60 | + # if JSON's field was named differently (despite using load_from in schema definition) |
| 61 | + for d in notebook_dicts: |
| 62 | + d["id"] = d["handle"] |
| 63 | + |
| 64 | + serializer = serializers.NotebookSchema() |
| 65 | + notebooks = serializer.get_instance(notebook_dicts, many=True) |
| 66 | + return notebooks |
| 67 | + |
| 68 | + def _get_request_json(self, kwargs): |
| 69 | + json_ = { |
| 70 | + "filter": { |
| 71 | + "filter": { |
| 72 | + "limit": 11, |
| 73 | + "offset": 0, |
| 74 | + "where": { |
| 75 | + "dtDeleted": None, |
| 76 | + }, |
| 77 | + "order": "jobId desc", |
| 78 | + }, |
| 79 | + }, |
| 80 | + } |
| 81 | + return json_ |
0 commit comments