diff --git a/python/setup.cfg b/python/setup.cfg index 559751011c73..e9d6f80a9d0d 100644 --- a/python/setup.cfg +++ b/python/setup.cfg @@ -45,6 +45,8 @@ python_requires = >=3.7 install_requires = mmh3 singledispatch + pyyaml +include_package_data = true [options.extras_require] arrow = pyarrow @@ -53,3 +55,6 @@ dev= pytest [options.packages.find] where = src +[options.package_data] +""= + ../src/iceberg/assets/rest-catalog-open-api.yaml \ No newline at end of file diff --git a/python/src/iceberg/assets/__init__.py b/python/src/iceberg/assets/__init__.py new file mode 100644 index 000000000000..13a83393a912 --- /dev/null +++ b/python/src/iceberg/assets/__init__.py @@ -0,0 +1,16 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. diff --git a/python/src/iceberg/assets/rest-catalog-open-api.yaml b/python/src/iceberg/assets/rest-catalog-open-api.yaml new file mode 120000 index 000000000000..fea96fe625d6 --- /dev/null +++ b/python/src/iceberg/assets/rest-catalog-open-api.yaml @@ -0,0 +1 @@ +../../../../open-api/rest-catalog-open-api.yaml \ No newline at end of file diff --git a/python/src/iceberg/utils/openapi.py b/python/src/iceberg/utils/openapi.py new file mode 100644 index 000000000000..b0d32b4f4d1d --- /dev/null +++ b/python/src/iceberg/utils/openapi.py @@ -0,0 +1,32 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +import os +from functools import lru_cache + +import yaml + +from iceberg import assets + + +@cache +def read_yaml_file(path: str): + return yaml.safe_load(open(path)) + + +def load_openapi_spec(): + return read_yaml_file(os.path.join(list(assets.__path__)[0], "rest-catalog-open-api.yaml")) diff --git a/python/tests/utils/test_openapi.py b/python/tests/utils/test_openapi.py new file mode 100644 index 000000000000..19158ffd4407 --- /dev/null +++ b/python/tests/utils/test_openapi.py @@ -0,0 +1,37 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +import os +import tempfile + +import yaml + +from iceberg.utils import openapi + + +def test_reading_a_yaml_file(): + with tempfile.TemporaryDirectory() as tmpdirname: + data = {"foo": "bar"} + file_location = os.path.join(tmpdirname, "foo.yaml") + with open(file_location, "w") as f: + yaml.dump(data, f) + assert openapi.read_yaml_file(file_location) == {"foo": "bar"} + + +def test_loading_rest_catalog_openapi_spec(): + rest_catalog_openapi_spec = openapi.load_openapi_spec() + assert rest_catalog_openapi_spec["info"]["title"] == "Apache Iceberg REST Catalog API"