Ejemplo n.º 1
0
    def test_empty_response_error(self, client):
        response = _utils.make_request(
            "GET",
            "http://httpbin.org/status/200",
            client._conn,
        )

        with pytest.raises(ValueError) as excinfo:
            _utils.body_to_json(response)
        msg = str(excinfo.value).strip()
        assert msg.startswith("expected JSON response")
        assert "<empty response>" in msg
Ejemplo n.º 2
0
    def test_json_response(self, client):
        response = _utils.make_request(
            "GET",
            "http://httpbin.org/json",
            client._conn,
        )

        assert isinstance(_utils.body_to_json(response), dict)
Ejemplo n.º 3
0
    def _get_workspace_name_by_id(self, workspace_id):
        # getting workspace
        response = _utils.make_request(
            "GET",
            "{}://{}/api/v1/uac-proxy/workspace/getWorkspaceById".format(
                self._conn.scheme, self._conn.socket),
            self._conn,
            params={'id': workspace_id},
        )
        _utils.raise_for_http_error(response)
        response_body = _utils.body_to_json(response)
        if 'user_id' in response_body:
            user_id = response_body['user_id']
            # try getting user
            response = _utils.make_request(
                "GET",
                "{}://{}/api/v1/uac-proxy/uac/getUser".format(
                    self._conn.scheme, self._conn.socket),
                self._conn,
                params={'user_id': user_id},
            )
            _utils.raise_for_http_error(response)

            # workspace is user
            return _utils.body_to_json(response)['verta_info']['username']
        else:
            org_id = response_body['org_id']
            # try getting organization
            response = _utils.make_request(
                "GET",
                "{}://{}/api/v1/uac-proxy/organization/getOrganizationById".
                format(self._conn.scheme, self._conn.socket),
                self._conn,
                params={'org_id': org_id},
            )
            # workspace is organization
            return _utils.body_to_json(response)['organization']['name']
Ejemplo n.º 4
0
    def get_artifact_parts(self, key):
        endpoint = "{}://{}/api/v1/registry/model_versions/{}/getCommittedArtifactParts".format(
            self._conn.scheme,
            self._conn.socket,
            self.id,
        )
        data = {'model_version_id': self.id, 'key': key}
        response = _utils.make_request("GET",
                                       endpoint,
                                       self._conn,
                                       params=data)
        _utils.raise_for_http_error(response)

        committed_parts = _utils.body_to_json(response).get(
            'artifact_parts', [])
        committed_parts = list(
            sorted(
                committed_parts,
                key=lambda part: int(part['part_number']),
            ))
        return committed_parts
Ejemplo n.º 5
0
    def get_code(self):
        """
        Gets the code version.

        Returns
        -------
        dict or zipfile.ZipFile
            Either:
                - a dictionary containing Git snapshot information with at most the following items:
                    - **filepaths** (*list of str*)
                    - **repo_url** (*str*) – Remote repository URL
                    - **commit_hash** (*str*) – Commit hash
                    - **is_dirty** (*bool*)
                - a `ZipFile <https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile>`_
                  containing Python source code files

        """
        # TODO: remove this circular dependency
        from ._project import Project
        from ._experiment import Experiment
        from ._experimentrun import ExperimentRun
        if isinstance(self, Project):  # TODO: not this
            Message = self._service.GetProjectCodeVersion
            endpoint = "getProjectCodeVersion"
        elif isinstance(self, Experiment):
            Message = self._service.GetExperimentCodeVersion
            endpoint = "getExperimentCodeVersion"
        elif isinstance(self, ExperimentRun):
            Message = self._service.GetExperimentRunCodeVersion
            endpoint = "getExperimentRunCodeVersion"
        msg = Message(id=self.id)
        data = _utils.proto_to_json(msg)
        response = _utils.make_request("GET",
                                       self._request_url.format(endpoint),
                                       self._conn,
                                       params=data)
        _utils.raise_for_http_error(response)

        response_msg = _utils.json_to_proto(_utils.body_to_json(response),
                                            Message.Response)
        code_ver_msg = response_msg.code_version
        which_code = code_ver_msg.WhichOneof('code')
        if which_code == 'git_snapshot':
            git_snapshot_msg = code_ver_msg.git_snapshot
            git_snapshot = {}
            if git_snapshot_msg.filepaths:
                git_snapshot['filepaths'] = git_snapshot_msg.filepaths
            if git_snapshot_msg.repo:
                git_snapshot['repo_url'] = git_snapshot_msg.repo
            if git_snapshot_msg.hash:
                git_snapshot['commit_hash'] = git_snapshot_msg.hash
            if git_snapshot_msg.is_dirty != _CommonCommonService.TernaryEnum.UNKNOWN:
                git_snapshot[
                    'is_dirty'] = git_snapshot_msg.is_dirty == _CommonCommonService.TernaryEnum.TRUE
            return git_snapshot
        elif which_code == 'code_archive':
            # download artifact from artifact store
            # pylint: disable=no-member
            # this method should only be called on ExperimentRun, which does have _get_url_for_artifact()
            url = self._get_url_for_artifact(
                "verta_code_archive", "GET",
                code_ver_msg.code_archive.artifact_type).url

            response = _utils.make_request("GET", url, self._conn)
            _utils.raise_for_http_error(response)

            code_archive = six.BytesIO(response.content)
            return zipfile.ZipFile(
                code_archive, 'r')  # TODO: return a util class instead, maybe
        else:
            raise RuntimeError("unable find code in response")