Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,8 @@ jobs:
- name: Test
run: pytest -v --cov=./switcher_client --cov-report xml

- name: Fix code coverage path
working-directory: ./
run: sed -i 's@'$GITHUB_WORKSPACE'@/github/workspace/@g' coverage.xml

- name: SonarCloud Scan
uses: sonarsource/sonarcloud-github-action@master
uses: sonarsource/sonarqube-scan-action@v4.1.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
4 changes: 3 additions & 1 deletion switcher_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from .client import Client
from .context import ContextOptions

__all__ = [
'Client'
'Client',
'ContextOptions',
]
18 changes: 11 additions & 7 deletions switcher_client/client.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
from switcher_client.context import Context
from typing import Optional

from switcher_client.context import Context, ContextOptions
from switcher_client.context import DEFAULT_ENVIRONMENT

class Client:
context = None
context: Optional[Context] = None

@staticmethod
def build_context(
domain: str,
url: str,
api_key: str,
component: str,
environment: str | None = DEFAULT_ENVIRONMENT):
url: Optional[str] = None,
api_key: Optional[str] = None,
component: Optional[str] = None,
environment: Optional[str] = DEFAULT_ENVIRONMENT,
options = ContextOptions()):
"""
Build the context for the client

Expand All @@ -19,9 +22,10 @@ def build_context(
:param api_key: Switcher-API key generated for the application/component
:param component: Application/component name
:param environment: Environment name
:param options: Optional parameters

"""
Client.context = Context(domain, url, api_key, component, environment)
Client.context = Context(domain, url, api_key, component, environment, options)

@staticmethod
def clear_context():
Expand Down
11 changes: 9 additions & 2 deletions switcher_client/context.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
DEFAULT_ENVIRONMENT = 'default'
DEFAULT_LOCAL = False

class ContextOptions:
def __init__(self, local = DEFAULT_LOCAL, snapshot_location = None):
self.local = local
self.snapshot_location = snapshot_location

class Context:
def __init__(self, domain, url, api_key, component, environment):
def __init__(self, domain, url, api_key, component, environment, options = ContextOptions()):
self.domain = domain
self.url = url
self.api_key = api_key
self.component = component
self.environment = environment
self.environment = environment
self.options = options
27 changes: 26 additions & 1 deletion tests/test_client_context.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import pytest

from switcher_client import Client
from switcher_client import Client, ContextOptions

def test_context():
""" Test building and verifying context """

Client.build_context(
domain='My Domain',
url='https://api.switcherapi.com',
Expand All @@ -12,10 +14,17 @@ def test_context():

try:
Client.verify_context()

assert Client.context.domain == 'My Domain'
assert Client.context.url == 'https://api.switcherapi.com'
assert Client.context.api_key == '[API_KEY]'
assert Client.context.component == 'MyApp'
except ValueError as e:
pytest.fail(f'Context verification failed: {e}')

def test_clear_context():
""" Test clearing context """

Client.build_context(
domain='My Domain',
url='https://api.switcherapi.com',
Expand All @@ -27,3 +36,19 @@ def test_clear_context():

with pytest.raises(ValueError):
Client.verify_context()

def test_context_with_optionals():
""" Test building context with optional parameters - local and snapshot_location """

Client.build_context(
domain='My Domain',
options=ContextOptions(
local=True,
snapshot_location='./snapshots'
)
)

options = Client.context.options

assert options.local == True
assert options.snapshot_location == './snapshots'