18
loading...
This website collects cookies to deliver better user experience
pip3 install flask requests
main.py
file in the directory of your choosing, and populate it with the following text:from flask import Flask, jsonify
import requests
app = Flask(__name__)
@app.route("/")
def get_feature():
text = "The app is up!"
return text
if __name__ == '__main__':
app.run(debug=True)
python3 main.py
$ /usr/local/bin/python3 /Users/codydearkland/python-launchdarkly-demo/main.py
* Serving Flask app 'main' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 597-409-598
http://127.0.0.1:5000/
. If you launch your web browser and navigate to that address, you should see the text The app is up! display on your screen. pip3 install launchdarkly-server-sdk
import ldclient
from ldclient.config import Config
app = Flask(__name__)
line, we’ll want to create a static user object (so we can play with targeting rules in a future post :)). Add the following block of code to create our user object (in JSON format):user = {
"key": "aa0ceb",
"firstName": "Mara",
"lastName": "Jade",
"email": "[email protected]",
"custom": {
"groups": ["Jedi"]
}
}
@app.route
section of the code. Since we’re going to be updating our application with a new set of code, this seems like a fantastic time to show LaunchDarkly in action by deploying a new capability behind a feature flag.requests
module. This module is used for making web requests outbound and is commonly used to retrieve results. Let’s add a feature that when enabled calls out to an API. Since our user, Mara Jade, is a “Star Wars” character, we’ll use the Star Wars API to look up one of her friends (using that term very loosely, depending upon which part of the story you’re at), Luke Skywalker. @app.route("/")
section to use the following code:@app.route("/")
def get_feature():
ldclient.set_config(Config("sdk-7e5424a3-01a1-43fb-93e1-f478f2bf9327"))
show_feature = ldclient.get().variation("textReturn", user, "No Data")
if show_feature == True:
r = requests.get("https://swapi.dev/api/people/1/").json()
ldclient.get().close()
return jsonify(r)
else:
text = "The app is up!"
return text
get_feature()
function, you can see we configure the our ldclient
(LaunchDarkly client) with an SDK key from our project/environment. Below this we create an object that evaluates the flag that we’re targeting, textReturn
in this case. We feed that object a user
object as well as a default flag value (this is useful for times where your application might become disconnected from LaunchDarkly for some reason.) True
or False
. If it's True
(enabled), we have our application make an API call to the Star Wars API and return the result (and close our connection to the SDK afterwards, because cleaning up after yourself is the right thing to do). If it’s False
(disabled), we return the previous code we had used earlier. import requests
import os
import ldclient
from ldclient.config import Config
from flask import Flask, jsonify
app = Flask(__name__)
user = {
"key": "aa0ceb",
"firstName": "Mara",
"lastName": "Jade",
"email": "[email protected]",
"custom": {
"groups": ["Jedi"]
}
}
@app.route("/")
def get_feature():
ldclient.set_config(Config("sdk-7e5424a3-01a1-43fb-93e1-f478f2bf9327"))
show_feature = ldclient.get().variation("textReturn", user, "No Data")
if show_feature == True:
r = requests.get("https://swapi.dev/api/people/1/").json()
ldclient.get().close()
return jsonify(r)
else:
text = "The app is up!"
return text
if __name__ == '__main__':
app.run(debug=True)
The app is up!
). We’re almost there! textReturn
flag we created earlier. Assuming everything went successfully, you should see the following flow occur!