Skip to content

Commit

Permalink
feat(python): allow setting arbitrary headers (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
markphelps committed Apr 11, 2024
1 parent ee75fc1 commit a1eb181
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package example;
package examples;

import io.flipt.api.FliptClient;
import io.flipt.api.evaluation.models.*;
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions flipt-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"author": "Flipt Devs <dev@flipt.io>",
"license": "MIT",
"scripts": {
"fmt": "prettier --config .prettierrc 'src/**/*.ts' 'example/index.ts' --write",
"lint": "prettier --check src/**/*.ts example/index.ts",
"fmt": "prettier --config .prettierrc 'src/**/*.ts' 'examples/index.ts' --write",
"lint": "prettier --check src/**/*.ts examples/index.ts",
"test": "jest",
"build": "tsc"
},
Expand Down
3 changes: 1 addition & 2 deletions flipt-python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ print(variant_flag)

There is a more detailed example in the [examples](./examples) directory.


## For developers

After adding new code, please don't forget to add unit tests for new features.
To format the code, check it with linters and run tests, use the `make check` command.
To format the code, check it with linters and run tests, use the `make check` command.

Please keep the Python [PEP8](https://peps.python.org/pep-0008/) in mind while adding new code.
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import asyncio
import base64
import os

from flipt import AsyncFliptClient
from flipt.evaluation import BatchEvaluationRequest, EvaluationRequest


async def main():
flipt_client = AsyncFliptClient()
# Set up the headers with the basic auth credentials
# for example if Flipt is behind a reverse proxy

headers = {}
username = os.getenv("FLIPT_USERNAME") or "admin"
password = os.getenv("FLIPT_PASSWORD") or "admin"

b64_creds = base64.b64encode(f"{username}:{password}".encode("utf-8")).decode("utf-8")
headers["Authorization"] = f"Basic {b64_creds}')"

flipt_client = AsyncFliptClient(headers=headers)

variant_flag = await flipt_client.evaluation.variant(
EvaluationRequest(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
from flipt import FliptClient
from flipt.evaluation import BatchEvaluationRequest, EvaluationRequest
import base64
import os

flipt_client = FliptClient()
# Set up the headers with the basic auth credentials
# for example if Flipt is behind a reverse proxy
headers = {}

username = os.getenv("FLIPT_USERNAME") or "admin"
password = os.getenv("FLIPT_PASSWORD") or "admin"

b64_creds = base64.b64encode(f"{username}:{password}".encode("utf-8")).decode("utf-8")
headers["Authorization"] = f"Basic {b64_creds}')"

flipt_client = FliptClient(headers=headers)

variant_flag = flipt_client.evaluation.variant(
EvaluationRequest(
Expand Down
5 changes: 3 additions & 2 deletions flipt-python/flipt/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ class AsyncFliptClient:
def __init__(
self,
url: str = "http://localhost:8080",
headers: dict[str, str] | None = None,
timeout: int = 60,
authentication: AuthenticationStrategy | None = None,
):
self.httpx_client = httpx.AsyncClient(timeout=timeout)

self.evaluation = AsyncEvaluation(url, authentication, self.httpx_client)
self.flag = AsyncFlag(url, authentication, self.httpx_client)
self.evaluation = AsyncEvaluation(url, headers, authentication, self.httpx_client)
self.flag = AsyncFlag(url, headers, authentication, self.httpx_client)

async def close(self) -> None:
await self.httpx_client.aclose()
6 changes: 5 additions & 1 deletion flipt-python/flipt/evaluation/async_evaluation_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ class AsyncEvaluation:
def __init__(
self,
url: str,
headers: dict[str, str] | None = None,
authentication: AuthenticationStrategy | None = None,
httpx_client: httpx.AsyncClient | None = None,
):
self.url = url
self.headers: dict[str, str] = {}

if headers is None:
headers = {}
self.headers = headers

self._client = httpx_client or httpx.AsyncClient()

Expand Down
6 changes: 5 additions & 1 deletion flipt-python/flipt/evaluation/sync_evaluation_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ class Evaluation:
def __init__(
self,
url: str,
headers: dict[str, str] | None = None,
authentication: AuthenticationStrategy | None = None,
httpx_client: httpx.Client | None = None,
):
self.url = url
self.headers: dict[str, str] = {}

if headers is None:
headers = {}
self.headers = headers

self._client = httpx_client or httpx.Client()

Expand Down
6 changes: 5 additions & 1 deletion flipt-python/flipt/flags/async_flag_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ class AsyncFlag:
def __init__(
self,
url: str,
headers: dict[str, str] | None = None,
authentication: AuthenticationStrategy | None = None,
httpx_client: httpx.AsyncClient | None = None,
):
self.url = url
self.headers: dict[str, str] = {}

if headers is None:
headers = {}
self.headers = headers

self._client = httpx_client or httpx.AsyncClient()

Expand Down
6 changes: 5 additions & 1 deletion flipt-python/flipt/flags/sync_flag_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ class SyncFlag:
def __init__(
self,
url: str,
headers: dict[str, str] | None = None,
authentication: AuthenticationStrategy | None = None,
httpx_client: httpx.Client | None = None,
):
self.url = url
self.headers: dict[str, str] = {}

if headers is None:
headers = {}
self.headers = headers

self._client = httpx_client or httpx.Client()

Expand Down
5 changes: 3 additions & 2 deletions flipt-python/flipt/sync_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ class FliptClient:
def __init__(
self,
url: str = "http://localhost:8080",
headers: dict[str, str] | None = None,
timeout: int = 60,
authentication: AuthenticationStrategy | None = None,
):
self.httpx_client = httpx.Client(timeout=timeout)

self.evaluation = Evaluation(url, authentication, self.httpx_client)
self.flag = SyncFlag(url, authentication, self.httpx_client)
self.evaluation = Evaluation(url, headers, authentication, self.httpx_client)
self.flag = SyncFlag(url, headers, authentication, self.httpx_client)

def close(self) -> None:
self.httpx_client.close()
2 changes: 1 addition & 1 deletion flipt-python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ skip-string-normalization = true

[tool.coverage.report]
precision = 1
fail_under = 95
fail_under = 90
exclude_also = [
"raise NotImplementedError",
"pragma: no cover",
Expand Down

0 comments on commit a1eb181

Please sign in to comment.