Coder Social home page Coder Social logo

flagsmith / flagsmith-python-client Goto Github PK

View Code? Open in Web Editor NEW
20.0 15.0 10.0 280 KB

Flagsmith Python Client. Flagsmith lets you manage features flags across web, mobile and server side applications. Get builds out faster. Control who has access to new features.

Home Page: https://www.flagsmith.com/

License: BSD 3-Clause "New" or "Revised" License

Python 100.00%
feature-flags remote-config continuous-integration continuous-deployment ci cd feature-toggles feature-toggle feature-flag

flagsmith-python-client's Introduction

Flagsmith Python SDK

Flagsmith allows you to manage feature flags and remote config across multiple projects, environments and organisations.

The SDK for Python applications for https://www.flagsmith.com/.

Adding to your project

For full documentation visit https://docs.flagsmith.com/clients/server-side?language=python.

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests

Getting Help

If you encounter a bug or feature request we would like to hear about it. Before you submit an issue please search existing issues in order to prevent duplicates.

Get in touch

If you have any questions about our projects you can email [email protected].

Useful links

Website

Documentation

flagsmith-python-client's People

Contributors

benrometsch avatar bne avatar dabeeeenster avatar dependabot[bot] avatar emiliocarrion avatar gagantrivedi avatar goncalossilva avatar khvn26 avatar kyle-ssg avatar matthewelwell avatar merijnbol avatar nogoodusername avatar rolodato avatar sjdines avatar tushar5526 avatar zachaysan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

flagsmith-python-client's Issues

Support transient identities and traits

Update the get_identity_flags interface:

    def get_identity_flags(
        self,
        identifier: str,
        traits: typing.Optional[typing.Mapping[str, TraitValue]] = None,
+       *,
+       transient: bool = False,
+       transient_traits: list[str] | None = None,
    ) -> Flags:

Update the generate_identities_data utility to add values according to updated schema here Flagsmith/flagsmith#4279

`enable_local_evaluation` and AWS Lambda

When using enable_local_evaluation in a Lambda, it doesn't work as expected.

I think that the sleep() inside the polling thread run() method is paused when the Lambda finish executing a call.
So, if we put environment_refresh_interval_seconds=60 and we have a lambda executed every minute with a duration of 100 ms, I think – I didn't confirm – that the environment would only be refreshed after 600 minutes instead of the expected 1 minute...

Maybe the run() should be implemented using a timestamp comparison with datetime.now() instead of sleep()?

Local evaluation with multiprocessing environment

What

When running the client with local evaluation in a gunicorn server (or any multiprocessed environment), the workers environment is never updated

Why

Gunicorn is designed around the pre-fork worker model (see docs). When instantiating the Flagsmith client, the polling updates the _environment variable in the master worker, but these changes will not affect the other workers. We would need to run a polling thread for each worker, which is not ideal

Sharing the _environment between workers

Would you be open to a new feature that would allow pickling and sharing the data between processes using a multiprocessing.Manager or a redis?

Async Client

Hey!

Any future plans for an AsyncFlagsmith client? Just curious as we are using FastAPI async code and would like to integrate it into our stack.

Thanks!

feature result and value are not properly resolved for multiple segments

Both feature_enabled and get_value wrongly resolve the result when there are multiple segments defined for a feature. The reason is that they loop over the _get_flags_response results, and they return the first match.

Expected result:
the last match should be returned, which is what the UI shows and other clients implement, which makes logical sense (all segments are applied and they override each other in sequence).

Method for getting Segments in the a Project

Hi Team!

Is there any way to get Segment information through the Python SDK?
With using APIs like

  • GET ​/api​/v1​/projects​/{project_pk}​/segments​/
  • GET ​/api​/v1​/projects​/{project_pk}​/segments​/{id}/

I can get the list of Segments in a Project and detailed information of a Segment like traits
I found that those APIs are sufficient for my usage but I'm wondering if I can get those information also through the SDK.

Thanks!

Have a way to differenciate errors due to requests errors vs missing flags

When doing:

flags = flagsmith.get_environment_flags()
environment_flags.is_feature_enabled("foo")
  • If the feature flag does not exist, the following error is raised:
    FlagsmithClientError("Feature does not exist: <my_feature>")
  • If the environment failed to do an API request, the following error is raised:
    FlagsmithClientError("Feature does not exist: <my_feature>")

I would like to raise different alerts in those cases, because the fix to the problems are not the same (i.e., waiting for it to resolve itself vs adding the flag or removing the code using the flag).

Could we have a way to differentiate them?

Suggestions

  • Flags could have an attribute to store the fact that the Flags were created following a FlagsmithAPIError, then, different errors could be raised in Flags.get_flag() using this attribute.

or

  • The default_flag_handler() could have a parameter that would indicate the type of error.

Enable to set proxy

Enable to set proxy on the client. Guessing all that needs to be done is adding another attribute 'proxy', to the Flagsmith class, and then passing it to the Session object.

Implement Real-Time Functionality

The Python client should support our real-time functionality via SSE. When using local evaluation mode, the client should listen for events on the stream and update its local cache of data if the value if the value of updated_at that was provided in the stream message is more recent than the last one received (and stored) from the X-Flagsmith-Document-Updated-At header received from the relevant API endpoint.

We should start by implementing this for local evaluation, since we already maintain a local cache of the environment document in this instance.

With regards to remote evaluation modes, we will need to implement a caching interface that stores the Flags class from the latest response from the relevant endpoint, and retrieves the flags again if it receives an event on the SSE stream. For now, this cache interface only needs to support a local memory cache, but we should make the interface generic to support other options.

Openfeature Provider

Are there any plans to implement an Openfeature provider for the python sdk? We would prefer to keep our flagging implementation consistent across applications with using open feature.

Provide typing stubs

The following is mypy output when using Flagsmith SDK in a strictly typed project:

src/example_service/components/flagsmith.py:3: error: Skipping analyzing "flagsmith": module is installed, but missing library stubs or py.typed marker  [import]
src/example_service/components/flagsmith.py:4: error: Skipping analyzing "flagsmith.flagsmith": module is installed, but missing library stubs or py.typed marker  [import]

Hopefully, shouldn't be too hard to fix. (Famous last words before jumping in the Python typing rabbit hole.)

Identity overrides don't show up when running in local evaluation mode

I'm running the same snipped of code.
First case: local evaluation turned off.

In [3]: flagsmith.get_identity_flags("91b0797d-a489-478b-9721-53d3913d73ad")
Out[3]: Flags(flags={'shopify2_graphql_fulfillment': Flag(enabled=False, value='TEST', feature_id=99751, feature_name='shopify2_graphql_fulfillment', is_default=False)}, default_flag_handler=<function default_flag_handler at 0x7f5e45f01260>, _analytics_processor=<flagsmith.analytics.AnalyticsProcessor object at 0x7f5e42d94350>)

Notice the flag has enabled=False and value='TEST' which is exactly how it was set in Flagsmith UI.

Second case: local evaluation turned on.

In [3]: flagsmith.get_identity_flags("91b0797d-a489-478b-9721-53d3913d73ad")
Out[3]: Flags(flags={'shopify2_graphql_fulfillment': Flag(enabled=True, value=None, feature_id=99751, feature_name='shopify2_graphql_fulfillment', is_default=False)}, default_flag_handler=<function default_flag_handler at 0x7f26c5451260>, _analytics_processor=<flagsmith.analytics.AnalyticsProcessor object at 0x7f26c272a450>)

Notice the flag has enabled=True and value=None which is the default for the environment. This is not what I expect, because there's an override for this user (as shown above).

Digging a bit further, the background update process still works but Flagsmith._environment always looks like that

{
    'identity_overrides': [],
    ...
}

At the same time, updating the value for the whole environment correctly updates the local client state. Only the per-identity state is not updated.

If it helps:

flagsmith==3.6.0
flagsmith-flag-engine==5.1.1

Make the SDK Async-friendly

If folk want to use this SDK in an async/non-blocking environment like FastAPI they will not have a good time. We should move to using async-friendly libraries.

Feature request: load build time-cached flags

I'd love to be able to do https://docs.flagsmith.com/guides-and-examples/integration-approaches#build-time-flag-retrieval for our backend, but it seems to be unsupported by the Flagsmith Python SDK.

I've also read https://docs.flagsmith.com/guides-and-examples/integration-approaches#caching-flags-on-a-server but would prefer to cache at build time because there are less moving parts (I don't have to worry about being able to receive webhooks from Flagsmith, and I can reuse the SDK's background update thread to retrieve from Flagsmith, instead of having to wire that up myself).

Issue: Flagsmith Automatically Adds '/v1' Prefix

Referencing https://github.com/Flagsmith/flagsmith-python-client/blob/main/flagsmith/flagsmith.py, all endpoints are automatically prefixed with /v1. This differs from other Flagsmith clients which instead require you to set the full URL such as:

https://flagsmith.mydomain.tld/api/v1

As such, anyone using multiple clients for Flagsmith has to create redundant settings with and without the /v1 to account for this difference in clients and consume them properly. If instead the /v1 were not automatically added, the same URL value could be used for all clients uniformly.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.