Skip to content

Module config.cf

View Source
from typing import Any, Dict, KeysView

import attr

from config.auth import OAuth2

from config.cfenv import CFenv

from config.spring import ConfigClient

@attr.s(slots=True)

class CF:

    cfenv = attr.ib(

        type=CFenv, factory=CFenv, validator=attr.validators.instance_of(CFenv),

    )

    oauth2 = attr.ib(type=OAuth2, default=None)

    client = attr.ib(type=ConfigClient, default=None)

    def __attrs_post_init__(self) -> None:

        if not self.oauth2:

            self.oauth2 = OAuth2(

                access_token_uri=self.cfenv.configserver_access_token_uri(),

                client_id=self.cfenv.configserver_client_id(),

                client_secret=self.cfenv.configserver_client_secret(),

            )

        if not self.client:

            self.client = ConfigClient(

                address=self.cfenv.configserver_uri(),

                app_name=self.cfenv.application_name,

                profile=self.cfenv.space_name.lower(),

            )

        self.oauth2.configure()

    @property

    def vcap_services(self):

        return self.cfenv.vcap_services

    @property

    def vcap_application(self):

        return self.cfenv.vcap_application

    def get_config(self) -> None:

        header = {"Authorization": f"Bearer {self.oauth2.token}"}

        self.client.get_config(headers=header)

    @property

    def config(self) -> Dict:

        return self.client.config

    def get_attribute(self, value: str) -> Any:

        return self.client.get_attribute(value)

    def get_keys(self) -> KeysView:

        return self.client.get_keys()

Classes

CF

class CF(
    cfenv: config.cfenv.CFenv = NOTHING,
    oauth2: config.auth.OAuth2 = None,
    client: config.spring.ConfigClient = None
)
View Source
class CF:

    cfenv = attr.ib(

        type=CFenv, factory=CFenv, validator=attr.validators.instance_of(CFenv),

    )

    oauth2 = attr.ib(type=OAuth2, default=None)

    client = attr.ib(type=ConfigClient, default=None)

    def __attrs_post_init__(self) -> None:

        if not self.oauth2:

            self.oauth2 = OAuth2(

                access_token_uri=self.cfenv.configserver_access_token_uri(),

                client_id=self.cfenv.configserver_client_id(),

                client_secret=self.cfenv.configserver_client_secret(),

            )

        if not self.client:

            self.client = ConfigClient(

                address=self.cfenv.configserver_uri(),

                app_name=self.cfenv.application_name,

                profile=self.cfenv.space_name.lower(),

            )

        self.oauth2.configure()

    @property

    def vcap_services(self):

        return self.cfenv.vcap_services

    @property

    def vcap_application(self):

        return self.cfenv.vcap_application

    def get_config(self) -> None:

        header = {"Authorization": f"Bearer {self.oauth2.token}"}

        self.client.get_config(headers=header)

    @property

    def config(self) -> Dict:

        return self.client.config

    def get_attribute(self, value: str) -> Any:

        return self.client.get_attribute(value)

    def get_keys(self) -> KeysView:

        return self.client.get_keys()

Class variables

cfenv
client
oauth2

Instance variables

cfenv
client
config
oauth2
vcap_application
vcap_services

Methods

get_attribute
def get_attribute(
    self,
    value: str
) -> Any
View Source
    def get_attribute(self, value: str) -> Any:

        return self.client.get_attribute(value)
get_config
def get_config(
    self
) -> None
View Source
    def get_config(self) -> None:

        header = {"Authorization": f"Bearer {self.oauth2.token}"}

        self.client.get_config(headers=header)
get_keys
def get_keys(
    self
) -> KeysView
View Source
    def get_keys(self) -> KeysView:

        return self.client.get_keys()