Skip to content

Commit

Permalink
[7.10] Conceal private APIs better
Browse files Browse the repository at this point in the history
  • Loading branch information
sethmlarson committed Nov 5, 2020
1 parent c965807 commit f715889
Show file tree
Hide file tree
Showing 14 changed files with 109 additions and 91 deletions.
120 changes: 60 additions & 60 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion elastic_enterprise_search/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
from elastic_transport import TransportError as TransportError
from elastic_transport import UnauthorizedError as UnauthorizedError

from ._serializer import JSONSerializer
from ._version import __version__ # noqa: F401
from .client import AppSearch, EnterpriseSearch, WorkplaceSearch
from .serializer import JSONSerializer

__all__ = [
"APIError",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from elastic_transport import JSONSerializer as _JSONSerializer

from .utils import format_datetime
from ._utils import format_datetime


class JSONSerializer(_JSONSerializer):
Expand Down
File renamed without changes.
10 changes: 4 additions & 6 deletions elastic_enterprise_search/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@
import jwt
from six import ensure_str

from .app_search import AppSearch as _AppSearch
from .enterprise_search import EnterpriseSearch as _EnterpriseSearch
from .workplace_search import WorkplaceSearch as _WorkplaceSearch
from .._utils import DEFAULT
from ._app_search import AppSearch as _AppSearch
from ._enterprise_search import EnterpriseSearch as _EnterpriseSearch
from ._workplace_search import WorkplaceSearch as _WorkplaceSearch

__all__ = ["AppSearch", "EnterpriseSearch", "WorkplaceSearch"]


DEFAULT = object()


class AppSearch(_AppSearch):
"""Client for Elastic App Search service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# specific language governing permissions and limitations
# under the License.

from ..utils import DEFAULT, SKIP_IN_PATH, make_params, make_path # noqa: F401
from .._utils import DEFAULT, SKIP_IN_PATH, make_params, make_path # noqa: F401
from .base import BaseClient


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
from elastic_transport.utils import create_user_agent, normalize_headers
from six import ensure_binary, ensure_str, ensure_text

from .._serializer import JSONSerializer
from .._utils import DEFAULT
from .._version import __version__
from ..serializer import JSONSerializer
from ..utils import DEFAULT

__all__ = ["BaseClient"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# specific language governing permissions and limitations
# under the License.

from ..utils import DEFAULT, SKIP_IN_PATH, make_params, make_path # noqa: F401
from .._utils import DEFAULT, SKIP_IN_PATH, make_params, make_path # noqa: F401
from .base import BaseClient


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# specific language governing permissions and limitations
# under the License.

from ..utils import DEFAULT, SKIP_IN_PATH, make_params, make_path # noqa: F401
from .._utils import DEFAULT, SKIP_IN_PATH, make_params, make_path # noqa: F401
from .base import BaseClient


Expand Down
2 changes: 1 addition & 1 deletion tests/client/test_http_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import pytest

from elastic_enterprise_search.utils import DEFAULT
from elastic_enterprise_search._utils import DEFAULT
from tests.conftest import DummyConnection


Expand Down
5 changes: 3 additions & 2 deletions tests/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
# under the License.

import elastic_enterprise_search
from elastic_enterprise_search import utils
from elastic_enterprise_search import _utils, client


def test_all_is_sorted():
assert elastic_enterprise_search.__all__ == sorted(
elastic_enterprise_search.__all__
)
assert utils.__all__ == sorted(utils.__all__)
assert _utils.__all__ == sorted(_utils.__all__)
assert client.__all__ == sorted(client.__all__)
24 changes: 12 additions & 12 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import pytest
from dateutil import tz

from elastic_enterprise_search import utils
from elastic_enterprise_search import _utils


def test_format_datetime_tz_naive():
Expand All @@ -30,7 +30,7 @@ def test_format_datetime_tz_naive():
# Should serialize the same as local timezone
dt2 = dt.replace(tzinfo=tz.tzlocal())

assert utils.format_datetime(dt) == utils.format_datetime(dt2)
assert _utils.format_datetime(dt) == _utils.format_datetime(dt2)

# This is the dicey one, utcnow() is very broken and not recommended.
dt = datetime.datetime.utcnow()
Expand All @@ -41,13 +41,13 @@ def test_format_datetime_tz_naive():
# The two are only equal if the local timezone is UTC
# otherwise they are different :(
if tz.tzlocal() == tz.UTC:
assert utils.format_datetime(dt) == utils.format_datetime(dt2)
assert _utils.format_datetime(dt) == _utils.format_datetime(dt2)
else:
assert utils.format_datetime(dt) != utils.format_datetime(dt2)
assert _utils.format_datetime(dt) != _utils.format_datetime(dt2)


def test_make_params():
assert utils.make_params(
assert _utils.make_params(
{},
{
"a": 1,
Expand Down Expand Up @@ -80,12 +80,12 @@ def test_make_params():

def test_make_params_conflict():
with pytest.raises(ValueError) as e:
utils.make_params({"k": "v1"}, {"k": "v2"})
_utils.make_params({"k": "v1"}, {"k": "v2"})
assert str(e.value) == "Conflict between keyword argument and 'params'"


def test_make_params_encode_and_none():
params = utils.make_params(
params = _utils.make_params(
{"k1": ("!@#$%^&*", "(){}[]./"), "k2": None},
{"k2": None, "k3": ",.*", "k4": 10},
)
Expand All @@ -98,7 +98,7 @@ def test_make_params_encode_and_none():

def test_make_path():
assert (
utils.make_path(
_utils.make_path(
"a",
1,
"/&",
Expand Down Expand Up @@ -128,12 +128,12 @@ def test_datetime_with_timezone():
dt = datetime.datetime(
year=2020, month=1, day=1, hour=10, minute=0, second=0, tzinfo=tz.gettz("HST")
)
assert utils.make_params({}, {"dt": dt}) == {"dt": "2020-01-01T10:00:00-10:00"}
assert _utils.make_params({}, {"dt": dt}) == {"dt": "2020-01-01T10:00:00-10:00"}

dt = datetime.datetime(
year=2020, month=1, day=1, hour=10, minute=0, second=0, tzinfo=tz.UTC
)
assert utils.make_params({}, {"dt": dt}) == {"dt": "2020-01-01T10:00:00Z"}
assert _utils.make_params({}, {"dt": dt}) == {"dt": "2020-01-01T10:00:00Z"}


@pytest.mark.parametrize(
Expand Down Expand Up @@ -186,12 +186,12 @@ def test_datetime_with_timezone():
],
)
def test_parse_datetime(value, dt):
assert utils.parse_datetime(value) == dt
assert _utils.parse_datetime(value) == dt


def test_parse_datetime_bad_format():
with pytest.raises(ValueError) as e:
utils.parse_datetime("2020-03-10T10:10:10")
_utils.parse_datetime("2020-03-10T10:10:10")
assert (
str(e.value)
== "Datetime must match format '(YYYY)-(MM)-(DD)T(HH):(MM):(SS)(TZ)' was '2020-03-10T10:10:10'"
Expand Down
21 changes: 20 additions & 1 deletion utils/generator/generate-api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@
import os
import pathlib
import re
from functools import lru_cache
from typing import List, Optional

import jinja2
import urllib3

http = urllib3.PoolManager()
current_branch = "7.10"
base_dir = pathlib.Path(__file__).absolute().parent.parent.parent
schemas_dir = base_dir.parent / "ent-search/swagger/v1"
templates_dir = str(pathlib.Path(__file__).absolute().parent / "templates")
Expand All @@ -44,6 +48,11 @@
}


@lru_cache()
def is_valid_url(url):
return 200 <= http.request("HEAD", url).status < 400


def openapi_type_to_typing(openapi_type, required=True) -> str:
t = None
if openapi_type == "string":
Expand Down Expand Up @@ -195,6 +204,16 @@ def docs_url(self) -> Optional[str]:
match = re.match(r"\[[^]]+?\]\(([^)]+)\)", self.spec.get("description", ""))
if match:
url = match.group(1)
if url:
new_url = re.sub(
r"/guide/en/([a-z\-]+)/current/",
r"/guide/en/\1/%s/" % current_branch,
url,
)
if is_valid_url(new_url):
url = new_url
else:
print(f"{new_url!r} isn't valid, sticking with {url!r}")
return url

@property
Expand Down Expand Up @@ -278,7 +297,7 @@ def expand_refs(x):
return x

schema_data = expand_refs(schema_data)
namespace = filepath.name.replace(".json", "").replace("-", "_")
namespace = "_" + filepath.name.replace(".json", "").replace("-", "_")
apis = []
components = {}
for cat, cat_val in schema_data.get("components", {}).items():
Expand Down
4 changes: 2 additions & 2 deletions utils/generator/templates/client
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
# specific language governing permissions and limitations
# under the License.

from .base import BaseClient
from ..utils import ( # noqa: F401
from ._base import BaseClient
from .._utils import ( # noqa: F401
make_path,
make_params,
DEFAULT,
Expand Down

0 comments on commit f715889

Please sign in to comment.