Skip to main content

config service client for pcf config-server3

Project description

Build Status Maintainability codecov PyPI version

config-client

config-client package for spring cloud config.

Installing

Install and update using pip:

pip install -U config-client

Dependencies

Setup

The default URL pattern is:

  • CONFIGSERVER_ADDRESS/BRANCH/APP_NAME-PROFILE.json
# expected environment variables:
#
CONFIGSERVER_ADDRESS=http://localhost:8888
BRANCH=master
PROFILE=development
APP_NAME=myapp

will result in:

http://localhost:8888/master/myapp-development.json

The url pattern can be customize on constructor with parameter url.

from config import spring

c = spring.ConfigClient(
        app_name='myapp',
        url="{address}/{branch}/{profile}-{app_name}.json"
    )
c.url
# output: 'http://localhost:8888/master/development-myapp.json'

Default values

if no value was adjusted for the environment variables below, the default value will be assumed, as:

CONFIGSERVER_ADDRESS=http://localhost:8888
BRANCH=master
PROFILE=development
APP_NAME=

Supported response format

  • JSON

Just add the .json extension to the end of the URL parameter. For example:

c = ConfigClient(
    app_name='foo',
    profile='development',
    address='http://localhost:8000',
    branch='master',
    url='{address}/{branch}/{app_name}-{profile}.json' # <
)

It will result in URL: http://localhost:8000/master/foo-development.json .

Notice .yaml it's not supported, all extensions will be converted to .json internally.

Usage Example

using standard client

from config.spring import ConfigClient

config_client = ConfigClient(app_name='my_app')
config_client.get_config()

# option 1: dict like with direct access
config_client.config['spring']['cloud']['consul']['host']

# option 2: dict like using get
config_client.config.get('spring').get('cloud').get('consul').get('port')

# option 3: using get_attribute method
config_client.get_attribute('spring.cloud.consul.port')

standard client with @decorator

For use cases where environment variables are set.

from config import spring

@spring.config_client()
def my_test(config_client=None):
    config_client.config['spring']['cloud']['consul']['host']
    config_client.config.get('spring').get('cloud').get('consul').get('port')
    config_client.get_attribute('spring.cloud.consul.port')

For use cases where environment variables are not set can you use decorator parameters, as example below:

from config import spring

@spring.config_client(app_name='myapp', branch="dev")
def my_test(config_client=None):
    config_client.config['spring']['cloud']['consul']['host']
    config_client.config.get('spring').get('cloud').get('consul').get('port')
    config_client.get_attribute('spring.cloud.consul.port')

Integration with Flask.

from config.spring import ConfigClient
from flask import Flask


config_client = ConfigClient(app_name="myapp")
config_client.get_config()
app = Flask(__name__)
app.run(host='0.0.0.0',
        port=config_client.config.get('app').get('port')

using asyncio

client using asyncio

import asyncio
from config.spring import ConfigClient


loop = asyncio.get_event_loop()
config_client = ConfigClient(app_name='myapp')
config_client.get_config()

async def service_discovery():
    await discovery_client.register(
        config_client.config['app']['name'],
        config_client.config.get('app').get('port')
    )

discovery_client = Consul(
    config_client.config.get('spring').get('cloud').get('consul').get('host'),
    config_client.get_attribute('spring.cloud.consul.port'],
    loop
)
loop.run_until_complete(service_discovery)

create singleton instance

Assuming default values.

from config.spring import create_config_client


c = create_config_client()
d = create_config_client()

print(id(c))
print(id(d))

With custom values.

from config.spring import create_config_client


c = create_config_client(address='http://localhost:8888/configuration', app_name='autosprocessuais-pecas-txtos', branch="ft-sdintegracoes-591")
d = create_config_client(address='http://localhost:8888/configuration', app_name='autosprocessuais-pecas-txtos', branch="ft-sdintegracoes-591")

print(id(c))
print(id(d))

cloudfoundry integration

Default usage

from config.cf import CF

cf = CF()
cf.get_config()

It's necessary bind Config Server with the application first.

A example application it's available on:

Custom usage

from config.cf import CF
from config.spring import ConfigClient

cf = CF(client=ConfigClient(app_name='simpleweb000', url="{address}/{branch}/{app_name}.json"))
cf.get_config()

Development

Running Tests

Install development dependencies.

pip install -r requirements-dev.txt

To execute tests just run:

python -m pytest -v --cov-report term --cov=config tests

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

config-client-pcf-c3-0.0.1.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

config_client_pcf_c3-0.0.1-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file config-client-pcf-c3-0.0.1.tar.gz.

File metadata

  • Download URL: config-client-pcf-c3-0.0.1.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.8.1

File hashes

Hashes for config-client-pcf-c3-0.0.1.tar.gz
Algorithm Hash digest
SHA256 4a27b09fb3d8a4ecb8a76d4736e87ff293d482d282b95478251b61527d814bab
MD5 16d200d4dc0a5cf4dc3c0ab39cf1db00
BLAKE2b-256 6ce844a3e4ce855a736874f59933a172763df10a2201f166439b4b02bcd89aae

See more details on using hashes here.

File details

Details for the file config_client_pcf_c3-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: config_client_pcf_c3-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.8.1

File hashes

Hashes for config_client_pcf_c3-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3428b59bc98c326e1e31cd0ab6d4ac2704e16e2198ae8e9e4b01272c75ce108d
MD5 8fd81f1f5c1fe92c5b02cfcff48338c1
BLAKE2b-256 a63e7b5f3d184e4c7e2c92913042217f72588380a045951b4201e0aa709517db

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page