Skip to content

Commit

Permalink
Merge pull request #57 from quantmind/master
Browse files Browse the repository at this point in the history
0.7.0
  • Loading branch information
lsbardel authored Aug 12, 2019
2 parents ab680c9 + 2ed5370 commit 6602e6d
Show file tree
Hide file tree
Showing 25 changed files with 310 additions and 315 deletions.
30 changes: 16 additions & 14 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,32 @@ jobs:
- checkout
- run: docker run --name postgres -d postgres:10
- run: docker run --link postgres:postgres
-e KONG_DATABASE=postgres
-e KONG_PG_HOST=postgres
-e KONG_PG_USER=postgres
-e KONG_PG_DATABASE=postgres
kong:1.0.3 kong migrations bootstrap
-e KONG_DATABASE=postgres
-e KONG_PG_HOST=postgres
-e KONG_PG_USER=postgres
-e KONG_PG_DATABASE=postgres
kong:1.2.1 kong migrations bootstrap
- run: docker run --name kong
--link postgres:postgres
-e KONG_DATABASE=postgres
-e KONG_PG_HOST=postgres
-e KONG_PG_USER=postgres
-e KONG_PG_DATABASE=postgres
-e "KONG_ADMIN_LISTEN=0.0.0.0:8001"
-p 8001:8001 -d kong:1.0.3
--link postgres:postgres
-e KONG_DATABASE=postgres
-e KONG_PG_HOST=postgres
-e KONG_PG_USER=postgres
-e KONG_PG_DATABASE=postgres
-e "KONG_ADMIN_LISTEN=0.0.0.0:8001"
-p 8001:8001 -d kong:1.2.1
# - run: git clone git://github.com/pyenv/pyenv-update.git $(pyenv root)/plugins/pyenv-update
# - run: pyenv update && pyenv install -l
# - run: pyenv install 3.7.2 && pyenv global 3.7.2
# - run: pyenv install 3.7.3 && pyenv global 3.7.3
- run: pyenv install 3.6.3 && pyenv global 3.6.3
- run:
name: install
command: ./dev/install.sh
- run:
name: flake8
command: flake8
- run:
name: black
command: make black
- run:
name: test
command: pytest --cov
Expand Down Expand Up @@ -58,7 +61,6 @@ jobs:
name: create tag
command: agilekit git release --yes


workflows:
version: 2
build-deploy:
Expand Down
7 changes: 1 addition & 6 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
{
"python.pythonPath": "${workspaceFolder}/venv/bin/python",
"python.linting.pylintEnabled": false,
"python.linting.flake8Enabled": true,
"[yaml]": {
"editor.tabSize": 2
}
"python.pythonPath": "${workspaceFolder}/venv/bin/python"
}
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ clean: ## remove python cache files

version: ## dipsplay software version
@python3 -c "import kong; print(kong.__version__)"

black: ## check black formatting
black --check kong tests
5 changes: 5 additions & 0 deletions dev/requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# testing and linting
black
isort[requirements]
mypy
pytest
pytest-cov
pytest-aiohttp
flake8
twine
agile-toolkit
flake8-blind-except
# flake8-builtins
flake8-commas
2 changes: 1 addition & 1 deletion kong/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Asynchronous Kong client"""

__version__ = '0.6.0'
__version__ = "0.7.0"
39 changes: 22 additions & 17 deletions kong/auths.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@


def auth_factory(consumer, auth_type):
known_types = {'basic-auth': BasicAuth,
'key-auth': KeyAuth}
known_types = {"basic-auth": BasicAuth, "key-auth": KeyAuth}
constructor = known_types.get(auth_type, ConsumerAuth)
return constructor(consumer, auth_type)

Expand All @@ -14,17 +13,21 @@ class ConsumerAuth(CrudComponent):

@property
def url(self) -> str:
return f'{self.root.url}/{self.name}'
return f"{self.root.url}/{self.name}"

async def get_existing_id(self, creds_config):
if not self.unique_field:
raise NotImplementedError('Existence check not implemented for this type of\
authentication')
raise NotImplementedError(
"Existence check not implemented for this type of\
authentication"
)
cur_unique = creds_config[self.unique_field]
try:
return next(cred for cred
in await self.get_list()
if cred[self.unique_field] == cur_unique)['id']
return next(
cred
for cred in await self.get_list()
if cred[self.unique_field] == cur_unique
)["id"]
except StopIteration:
return None

Expand All @@ -36,21 +39,23 @@ async def create_or_update_credentials(self, creds_config):
await self.create_credentials(data=creds_config)

async def update_credentials(self, id_, **kw):
url = f'{self.url}/{id_}'
url = f"{self.url}/{id_}"

return await self.cli.execute(
url, 'patch',
headers={'Content-Type': 'application/x-www-form-urlencoded'},
url,
"patch",
headers={"Content-Type": "application/x-www-form-urlencoded"},
wrap=self.wrap,
**kw
**kw,
)

async def create_credentials(self, **kw):
return await self.cli.execute(
self.url, 'post',
headers={'Content-Type': 'application/x-www-form-urlencoded'},
self.url,
"post",
headers={"Content-Type": "application/x-www-form-urlencoded"},
wrap=self.wrap,
**kw
**kw,
)

async def get_or_create(self):
Expand All @@ -59,8 +64,8 @@ async def get_or_create(self):


class BasicAuth(ConsumerAuth):
unique_field = 'username'
unique_field = "username"


class KeyAuth(ConsumerAuth):
unique_field = 'key'
unique_field = "key"
2 changes: 1 addition & 1 deletion kong/certificates.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@


class Certificate(KongEntity):

@property
def snis(self):
return Snis(self)


class Certificates(CrudComponent):
"""Kong TLS certificate component"""

Entity = Certificate
33 changes: 7 additions & 26 deletions kong/cli.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@

import json
import asyncio
import json

import click
import dotenv

dotenv.load_dotenv() # noqa

import yaml as _yaml

from . import __version__
Expand All @@ -15,26 +10,12 @@


@click.command()
@click.option("--version", is_flag=True, default=False, help="Display version and exit")
@click.option("--ip", is_flag=True, default=False, help="Show local IP address")
@click.option(
'--version',
is_flag=True,
default=False,
help='Display version and exit'
)
@click.option(
'--ip',
is_flag=True,
default=False,
help='Show local IP address'
)
@click.option(
'--key-auth',
help='Create or display an authentication key for a consumer'
)
@click.option(
'--yaml', type=click.File('r'),
help='Yaml configuration to upload'
"--key-auth", help="Create or display an authentication key for a consumer"
)
@click.option("--yaml", type=click.File("r"), help="Yaml configuration to upload")
@click.pass_context
def kong(ctx, version, ip, key_auth, yaml):
if version:
Expand All @@ -56,7 +37,7 @@ def _run(coro):
async def _yml(ctx, yaml):
async with Kong() as cli:
try:
result = await cli.apply_json(_yaml.load(yaml))
result = await cli.apply_json(_yaml.load(yaml, Loader=_yaml.FullLoader))
click.echo(json.dumps(result, indent=4))
except KongError as exc:
raise click.ClickException(str(exc))
Expand All @@ -76,5 +57,5 @@ async def _auth_key(ctx, consumer):
raise click.ClickException(str(exc))


def main(): # pragma nocover
def main(): # pragma nocover
kong()
55 changes: 32 additions & 23 deletions kong/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,41 @@

import aiohttp

from .components import KongError, KongResponseError, CrudComponent
from .services import Services
from .plugins import Plugins
from .consumers import Consumers
from .certificates import Certificates
from .components import CrudComponent, KongError, KongResponseError
from .consumers import Consumers
from .plugins import Plugins
from .services import Services
from .snis import Snis


__all__ = [
'Kong',
'KongError',
'KongResponseError'
]
__all__ = ["Kong", "KongError", "KongResponseError"]


class Kong:
"""Kong client
"""
url = os.environ.get('KONG_URL', 'http://127.0.0.1:8001')

def __init__(self, url: str = None, session: typing.Any = None) -> None:
url = os.environ.get("KONG_URL", "http://127.0.0.1:8001")

def __init__(
self,
url: str = None,
session: typing.Any = None,
request_kwargs: typing.Dict = None,
) -> None:
self.url = url or self.url
self.session = session
self.request_kwargs = request_kwargs or {}
self.services = Services(self)
self.plugins = Plugins(self)
self.consumers = Consumers(self)
self.certificates = Certificates(self)
self.acls = CrudComponent(self, 'acls')
self.acls = CrudComponent(self, "acls")
self.snis = Snis(self)

def __repr__(self) -> str:
return self.url

__str__ = __repr__

@property
Expand All @@ -52,17 +55,23 @@ async def __aenter__(self) -> object:
async def __aexit__(self, exc_type, exc_val, exc_tb) -> None:
await self.close()

async def execute(self, url, method=None, headers=None,
callback=None, wrap=None, timeout=None,
**kw):
async def execute(
self,
url,
method=None,
headers=None,
callback=None,
wrap=None,
timeout=None,
**kw,
):
if not self.session:
self.session = aiohttp.ClientSession()
method = method or 'GET'
method = method or "GET"
headers = headers or {}
headers['Accept'] = 'application/json, text/*; q=0.5'
response = await self.session.request(
method, url, headers=headers, **kw
)
headers["Accept"] = "application/json, text/*; q=0.5"
kw.update(self.request_kwargs)
response = await self.session.request(method, url, headers=headers, **kw)
if callback:
return await callback(response)
if response.status == 204:
Expand All @@ -80,14 +89,14 @@ async def execute(self, url, method=None, headers=None,
async def apply_json(self, config):
config = copy.deepcopy(config)
if not isinstance(config, dict):
raise KongError('Expected a dict got %s' % type(config).__name__)
raise KongError("Expected a dict got %s" % type(config).__name__)
result = {}
for name, data in config.items():
if not isinstance(data, list):
data = [data]
o = getattr(self, name)
if not o:
raise KongError('Kong object %s not available' % name)
raise KongError("Kong object %s not available" % name)
result[name] = await o.apply_json(data)
return result

Expand Down
Loading

0 comments on commit 6602e6d

Please sign in to comment.