Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client does not return flags - self hosted api server #3

Closed
ukabu opened this issue Dec 17, 2018 · 10 comments · Fixed by #4
Closed

Client does not return flags - self hosted api server #3

ukabu opened this issue Dec 17, 2018 · 10 comments · Fixed by #4

Comments

@ukabu
Copy link
Contributor

ukabu commented Dec 17, 2018

I initialized the client with :

bulletTrain.init({
        api,
        environmentID,
        onChange: handleFlags,
        onError: handleErrors,
        defaultFlags
      })
// api="https://bullet-train-api-experiment...com/api/v1/"
// environmentID="m...G"
// defaultFlags={
//    hello_world: true
// }
  handleFlags = (flags, params) => {
    this.setState({ flags })
    console.log(flags, params, bulletTrain.getAllFlags())
  }

In the console (from the handleFlag listener) I get : {}, {isFromServer: true}, {}

The response I get from the API server is :

[{"id":2,"feature":{"id":1,"name":"hello_world","created_date":"2018-12-11T20:55:38.891097Z","initial_value":null,"description":null,"default_enabled":false,"type":"FLAG","project":1},"feature_state_value":null,"enabled":true,"environment":2,"identity":null}]

if I call bulletTrain.hasFeature("hello_world") I always get false.

@kyle-ssg
Copy link
Member

Can you confirm this is due to using a self hosted api? I feel like this is a duplicate of #2

@ukabu
Copy link
Contributor Author

ukabu commented Dec 18, 2018

It,s not duplicate of #2 , here, I'm loading the client from the CDN (https://cdn.jsdelivr.net/npm/bullet-train-client/lib/index.js) and it loads and connect to the API server properly. The client does receive a response from the API server that contains the feature flag, but the bulletTrain.getAllFlags() call returns an empty object. I also tried with bulletTrain.hasFeature("hello_world") and it always returns false... Even though I specified a default flag value of true` and I receive the flag (set to true) from the API server.

@ukabu
Copy link
Contributor Author

ukabu commented Dec 18, 2018

The code seems wrong, in bullet-train-core, this calls the handleResponse(res) function, the response from the API is an array: https://github.com/SolidStateGroup/bullet-train-js-client/blob/f96cb36aabc8e77dabf790e36b78dbc9f898ec45/bullet-train-core.js#L65

But the handleResponse expect an object with the shape {flags: [], traits: []}: https://github.com/SolidStateGroup/bullet-train-js-client/blob/f96cb36aabc8e77dabf790e36b78dbc9f898ec45/bullet-train-core.js#L31

For reference, the bullet-train-fronend app interpret the response from the API as an array: https://github.com/SolidStateGroup/bullet-train-frontend/blob/e062cfd406e85c0220c2759556129a0776ae9975/web/components/TryIt.js#L22

@ukabu
Copy link
Contributor Author

ukabu commented Dec 18, 2018

Here is the commit that switch the handling from an array to an object: 5c131e0

@kyle-ssg
Copy link
Member

kyle-ssg commented Dec 18, 2018

The response from getting flags is not an array since implementing user traits it is an object. Are you sure your API is set up correctly and you are using the latest version?

curl 'https://api.bullet-train.io/api/v1/identities/bullet_train_sample_user/?format=json' -H 'x-environment-key: QjgYur4LQTwe5HpvbvhpzK' -H 'Referer: http://localhost:3000/' -H 'Origin: http://localhost:3000' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36' -H 'Content-Type: application/json; charset=utf-8' --compressed

@kyle-ssg
Copy link
Member

To demonstrate this, there's a super simple react app that lets you retrieve flags+traits and toggle a user trait.
https://github.com/SolidStateGroup/bullet-train-js-client/tree/master/bullet-train-client/example

@ukabu
Copy link
Contributor Author

ukabu commented Dec 18, 2018

Looks like my api server is not up to date. It's a bummer that the api changes in incompatible ways like that.
I will update my server and test it.

@ukabu
Copy link
Contributor Author

ukabu commented Dec 18, 2018

Now i'm running that latest api server from github and I still get an array

curl 'https://bullet-train-api-experiment.....com/api/v1/flags/?format=json' -H 'x-environment-key: m...G' -H 'Referer: http://localhost:3000/?q=portail' -H 'Origin: http://localhost:3000' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36' -H 'Content-Type: application/json; charset=utf-8' --compressed

response:

[{"id":2,"feature":{"id":1,"name":"hello_world","created_date":"2018-12-11T20:55:38.891097Z","initial_value":null,"description":null,"default_enabled":false,"type":"FLAG","project":1},"feature_state_value":null,"enabled":true,"environment":2,"identity":null}]

@ukabu
Copy link
Contributor Author

ukabu commented Dec 19, 2018

The bug is when bulletTrain.identify(...) has not been called.

if at https://github.com/SolidStateGroup/bullet-train-js-client/blob/f96cb36aabc8e77dabf790e36b78dbc9f898ec45/bullet-train-core.js#L65

you would call handleResponse({flags: res}) instead of handleresponse(res) that would solve the problem.

ukabu added a commit to ukabu/bullet-train-js-client that referenced this issue Dec 19, 2018
kyle-ssg added a commit that referenced this issue Dec 19, 2018
@kyle-ssg
Copy link
Member

bullet-train-client and react-native-bullet-train have been updated to 0.0.42 to incorporate this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants