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

chore: Use local api for self hosted snippets #4807

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
4 changes: 3 additions & 1 deletion frontend/common/code-help/create-user/create-user-curl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Constants from 'common/constants'

module.exports = (envId, { USER_ID }, userId) => `// Identify/create user

curl -i '${Project.flagsmithClientAPI}identities/?identifier=${
curl -i '${Constants.getFlagsmithSDKUrl()}identities/?identifier=${
userId || USER_ID
}' \\
-H 'x-environment-key: ${envId}'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module.exports = (
userId,
) => `final flagsmithClient = FlagsmithClient(
apiKey: '${envId}',${
Constants.isCustomFlagsmithUrl
? `\n baseURI: '${Project.flagsmithClientAPI}',`
Constants.isCustomFlagsmithUrl()
? `\n baseURI: '${Constants.getFlagsmithSDKUrl()}',`
: ''
}
config: config,
Expand All @@ -18,8 +18,8 @@ module.exports = (
//if you prefer async initialization then you should use
//final flagsmithClient = await FlagsmithClient.init(
// apiKey: '${envId}',${
Constants.isCustomFlagsmithUrl
? `\n// baseURI: '${Project.flagsmithClientAPI}',`
Constants.isCustomFlagsmithUrl()
? `\n// baseURI: '${Constants.getFlagsmithSDKUrl()}',`
: ''
}
// config: config,
Expand Down
4 changes: 2 additions & 2 deletions frontend/common/code-help/create-user/create-user-ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ func application(_ application: UIApplication,
[UIApplication.LaunchOptionsKey: Any]?) -> Bool {

Flagsmith.shared.apiKey = "${envId}"${
Constants.isCustomFlagsmithUrl &&
`\n Flagsmith.shared.baseURL = "${Project.flagsmithClientAPI}"\n`
Constants.isCustomFlagsmithUrl() &&
`\n Flagsmith.shared.baseURL = "${Constants.getFlagsmithSDKUrl()}"\n`
}

// This will create a user in the dashboard if they don't already exist
Expand Down
4 changes: 2 additions & 2 deletions frontend/common/code-help/create-user/create-user-java.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ module.exports = (
) => `${LIB_NAME_JAVA} ${LIB_NAME} = ${LIB_NAME_JAVA}
.newBuilder()
.setApiKey("${envId}")${
Constants.isCustomFlagsmithUrl &&
Constants.isCustomFlagsmithUrl() &&
`\n .withConfiguration(FlagsmithConfig.builder()
.baseUri("${Project.flagsmithClientAPI}")
.baseUri("${Constants.getFlagsmithSDKUrl()}")
.build())`
}
.build();
Expand Down
4 changes: 2 additions & 2 deletions frontend/common/code-help/create-user/create-user-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ module.exports = (
// Option 1: initialise with an identity and traits
${LIB_NAME}.init({
environmentID: "${envId}",${
Constants.isCustomFlagsmithUrl
? `\n api: "${Project.flagsmithClientAPI}",`
Constants.isCustomFlagsmithUrl()
? `\n api: "${Constants.getFlagsmithSDKUrl()}",`
: ''
}
identity: "${userId || USER_ID}",
Expand Down
8 changes: 4 additions & 4 deletions frontend/common/code-help/create-user/create-user-next.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export default function App({ Component, pageProps, flagsmithState } {
serverState={flagsmithState}
options={{
environmentID: "${envId}",${
Constants.isCustomFlagsmithUrl
? `\n api: "${Project.flagsmithClientAPI}",`
Constants.isCustomFlagsmithUrl()
? `\n api: "${Constants.getFlagsmithSDKUrl()}",`
: ''
}
}}
Expand All @@ -42,8 +42,8 @@ MyApp.getInitialProps = async () => {
// calls page's \`getInitialProps\` and fills \`appProps.pageProps\`
await flagsmith.init({ // fetches flags on the server
environmentID: "${envId}",${
Constants.isCustomFlagsmithUrl
? `\n api: "${Project.flagsmithClientAPI}",`
Constants.isCustomFlagsmithUrl()
? `\n api: "${Constants.getFlagsmithSDKUrl()}",`
: ''
}
preventFetch: true
Expand Down
4 changes: 2 additions & 2 deletions frontend/common/code-help/create-user/create-user-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module.exports = (
) => `import Flagsmith from "${NPM_NODE_CLIENT}"; // Add this line if you're using ${LIB_NAME} via npm

const ${LIB_NAME} = new Flagsmith({${
Constants.isCustomFlagsmithUrl &&
`\n apiUrl: '${Project.flagsmithClientAPI}',`
Constants.isCustomFlagsmithUrl() &&
`\n apiUrl: '${Constants.getFlagsmithSDKUrl()}',`
}
environmentKey: '${envId}'
});
Expand Down
2 changes: 1 addition & 1 deletion frontend/common/code-help/create-user/create-user-php.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = (
) => `use Flagsmith\\Flagsmith;

$flagsmith = new Flagsmith('${envId}'${
Constants.isCustomFlagsmithUrl && `,\n '${Project.flagsmithClientAPI}'\n`
Constants.isCustomFlagsmithUrl() && `,\n '${Constants.getFlagsmithSDKUrl()}'\n`
});

// Identify the user
Expand Down
4 changes: 2 additions & 2 deletions frontend/common/code-help/create-user/create-user-python.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module.exports = (
) => `from flagsmith import Flagsmith

flagsmith = Flagsmith(environment_key="${envId}"${
Constants.isCustomFlagsmithUrl &&
`,\n api_url="${Project.flagsmithClientAPI}"\n`
Constants.isCustomFlagsmithUrl() &&
`,\n api_url="${Constants.getFlagsmithSDKUrl()}"\n`
})

# Identify the user
Expand Down
4 changes: 2 additions & 2 deletions frontend/common/code-help/create-user/create-user-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export default function App() {
<FlagsmithProvider
options={{
environmentID: '${envId}',${
Constants.isCustomFlagsmithUrl
? `\n api: '${Project.flagsmithClientAPI}',`
Constants.isCustomFlagsmithUrl()
? `\n api: '${Constants.getFlagsmithSDKUrl()}',`
: ''
}
identity: '${userId || USER_ID}',
Expand Down
4 changes: 2 additions & 2 deletions frontend/common/code-help/create-user/create-user-ruby.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module.exports = (

$flagsmith = Flagsmith::Client.new(
environment_key="${envId}"${
Constants.isCustomFlagsmithUrl &&
`,\n api_url="${Project.flagsmithClientAPI}"\n`
Constants.isCustomFlagsmithUrl() &&
`,\n api_url="${Constants.getFlagsmithSDKUrl()}"\n`
})

// Identify the user
Expand Down
4 changes: 2 additions & 2 deletions frontend/common/code-help/create-user/create-user-rust.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module.exports = (
use flagsmith::{Flag, Flagsmith, FlagsmithOptions};

let options = FlagsmithOptions {${
Constants.isCustomFlagsmithUrl &&
`api_url: "${Project.flagsmithClientAPI}".to_string(),\n`
Constants.isCustomFlagsmithUrl() &&
`api_url: "${Constants.getFlagsmithSDKUrl()}".to_string(),\n`
}..Default::default()};
let flagsmith = Flagsmith::new(
"${envId}".to_string(),
Expand Down
4 changes: 3 additions & 1 deletion frontend/common/code-help/init/init-curl.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Constants from 'common/constants'

module.exports = (envId) => `
curl -i '${Project.flagsmithClientAPI}flags/' \\
curl -i '${Constants.getFlagsmithSDKUrl()}flags/' \\
-H 'x-environment-key: ${envId}'
`
6 changes: 5 additions & 1 deletion frontend/common/code-help/init/init-dotnet.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Constants from 'common/constants'

module.exports = (
envId,
{ FEATURE_NAME, FEATURE_NAME_ALT },
Expand All @@ -6,7 +8,9 @@ module.exports = (

static FlagsmithClient _flagsmithClient;

_flagsmithClient = new("${envId}");
_flagsmithClient = new("${envId}"${
Constants.isCustomFlagsmithUrl() ? `, apiUrl: ${Constants.getFlagsmithSDKUrl()}` : ''
});

var flags = await _flagsmithClient.GetEnvironmentFlags(); # This method triggers a network request

Expand Down
8 changes: 4 additions & 4 deletions frontend/common/code-help/init/init-flutter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module.exports = (

final flagsmithClient = FlagsmithClient(
apiKey: '${envId}',${
Constants.isCustomFlagsmithUrl
? `\n baseURI: '${Project.flagsmithClientAPI}',`
Constants.isCustomFlagsmithUrl()
? `\n baseURI: '${Constants.getFlagsmithSDKUrl()}',`
: ''
}
config: config,
Expand All @@ -19,8 +19,8 @@ final flagsmithClient = FlagsmithClient(
//if you prefer async initialization then you should use
//final flagsmithClient = await FlagsmithClient.init(
// apiKey: '${envId}',${
Constants.isCustomFlagsmithUrl
? `\n// baseURI: '${Project.flagsmithClientAPI}',`
Constants.isCustomFlagsmithUrl()
? `\n// baseURI: '${Constants.getFlagsmithSDKUrl()}',`
: ''
}
// config: config,
Expand Down
4 changes: 2 additions & 2 deletions frontend/common/code-help/init/init-go.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ defer cancel()

// Initialise the Flagsmith client
client := flagsmith.NewClient('${envId}',${
Constants.isCustomFlagsmithUrl &&
`\nflagsmith.WithBaseURL("${Project.flagsmithClientAPI}"),\n`
Constants.isCustomFlagsmithUrl() &&
`\nflagsmith.WithBaseURL("${Constants.getFlagsmithSDKUrl()}"),\n`
}flagsmith.WithContext(ctx))

// The method below triggers a network request
Expand Down
4 changes: 2 additions & 2 deletions frontend/common/code-help/init/init-ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

Flagsmith.shared.apiKey = "${envId}"${
Constants.isCustomFlagsmithUrl &&
`\n Flagsmith.shared.baseURL = "${Project.flagsmithClientAPI}"\n`
Constants.isCustomFlagsmithUrl() &&
`\n Flagsmith.shared.baseURL = "${Constants.getFlagsmithSDKUrl()}"\n`
}
// Check for a feature
Flagsmith.shared
Expand Down
4 changes: 2 additions & 2 deletions frontend/common/code-help/init/init-java.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ module.exports = (
) => `${LIB_NAME_JAVA} ${LIB_NAME} = ${LIB_NAME_JAVA}
.newBuilder()
.setApiKey("${envId}")${
Constants.isCustomFlagsmithUrl
Constants.isCustomFlagsmithUrl()
? `\n .withConfiguration(FlagsmithConfig.newBuilder()
.baseUri("${Project.flagsmithClientAPI}")
.baseUri("${Constants.getFlagsmithSDKUrl()}")
.build())`
: ''
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/common/code-help/init/init-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = (
) => `import ${LIB_NAME} from "${NPM_CLIENT}"; // Add this line if you're using ${LIB_NAME} via npm

${LIB_NAME}.init({
environmentID: "${envId}",${Constants.isCustomFlagsmithUrl ? `\n api: "${Project.flagsmithClientAPI}",` : ''}
environmentID: "${envId}",${Constants.isCustomFlagsmithUrl() ? `\n api: "${Constants.getFlagsmithSDKUrl()}",` : ''}
onChange: (oldFlags, params) => { // Occurs whenever flags are changed
// Determines if the update came from the server or local cached storage
const { isFromServer } = params;
Expand Down
8 changes: 4 additions & 4 deletions frontend/common/code-help/init/init-next.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export default function App({ Component, pageProps, flagsmithState } {
serverState={flagsmithState}
options={{
environmentID: "${envId}",${
Constants.isCustomFlagsmithUrl
? `\n api: "${Project.flagsmithClientAPI}",`
Constants.isCustomFlagsmithUrl()
? `\n api: "${Constants.getFlagsmithSDKUrl()}",`
: ''
}
}}
Expand All @@ -26,8 +26,8 @@ export default function App({ Component, pageProps, flagsmithState } {
App.getInitialProps = async () => {
await flagsmith.init({ // fetches flags on the server and passes them to the App
environmentID: "${envId}",${
Constants.isCustomFlagsmithUrl
? `\n api: "${Project.flagsmithClientAPI}",`
Constants.isCustomFlagsmithUrl()
? `\n api: "${Constants.getFlagsmithSDKUrl()}",`
: ''
}
});
Expand Down
4 changes: 2 additions & 2 deletions frontend/common/code-help/init/init-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module.exports = (
) => `import Flagsmith from "${NPM_NODE_CLIENT}"; // Add this line if you're using ${LIB_NAME} via npm

const ${LIB_NAME} = new Flagsmith({${
Constants.isCustomFlagsmithUrl &&
`\n apiUrl: '${Project.flagsmithClientAPI}',`
Constants.isCustomFlagsmithUrl() &&
`\n apiUrl: '${Constants.getFlagsmithSDKUrl()}',`
}
environmentKey: '${envId}'
});
Expand Down
2 changes: 1 addition & 1 deletion frontend/common/code-help/init/init-php.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = (
) => `use Flagsmith\\Flagsmith;

$flagsmith = new Flagsmith('${envId}'${
Constants.isCustomFlagsmithUrl && `,\n '${Project.flagsmithClientAPI}'\n`
Constants.isCustomFlagsmithUrl() && `,\n '${Constants.getFlagsmithSDKUrl()}'\n`
});

// Check for a feature
Expand Down
4 changes: 2 additions & 2 deletions frontend/common/code-help/init/init-python.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module.exports = (
) => `from flagsmith import Flagsmith

flagsmith = Flagsmith(environment_key="${envId}"${
Constants.isCustomFlagsmithUrl &&
`,\n api_url="${Project.flagsmithClientAPI}"\n`
Constants.isCustomFlagsmithUrl() &&
`,\n api_url="${Constants.getFlagsmithSDKUrl()}"\n`
})

# The method below triggers a network request
Expand Down
4 changes: 2 additions & 2 deletions frontend/common/code-help/init/init-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export default function App() {
<FlagsmithProvider
options={{
environmentID: '${envId}',${
Constants.isCustomFlagsmithUrl
? `\n api: '${Project.flagsmithClientAPI}',`
Constants.isCustomFlagsmithUrl()
? `\n api: '${Constants.getFlagsmithSDKUrl()}',`
: ''
}
}}
Expand Down
4 changes: 2 additions & 2 deletions frontend/common/code-help/init/init-ruby.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module.exports = (

$flagsmith = Flagsmith::Client.new(
environment_key="${envId}"${
Constants.isCustomFlagsmithUrl &&
`,\n api_url="${Project.flagsmithClientAPI}"\n`
Constants.isCustomFlagsmithUrl() &&
`,\n api_url="${Constants.getFlagsmithSDKUrl()}"\n`
})

// Load the environment's flags locally
Expand Down
4 changes: 2 additions & 2 deletions frontend/common/code-help/init/init-rust.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module.exports = (envId, { FEATURE_NAME, FEATURE_NAME_ALT }) => `
use flagsmith::{Flag, Flagsmith, FlagsmithOptions};

let options = FlagsmithOptions {${
Constants.isCustomFlagsmithUrl &&
`api_url: "${Project.flagsmithClientAPI}".to_string(),\n`
Constants.isCustomFlagsmithUrl() &&
`api_url: "${Constants.getFlagsmithSDKUrl()}".to_string(),\n`
}..Default::default()};
let flagsmith = Flagsmith::new(
"${envId}".to_string(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Constants from 'common/constants'

module.exports = (envId) => `
curl -i '${Project.flagsmithClientAPI}flags/' \\
curl -i '${Constants.getFlagsmithSDKUrl()}flags/' \\
-H 'x-environment-key: ${envId}' | tee flagsmith.json
`
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Constants from 'common/constants'

module.exports = (serversideEnvironmentKey) => `
curl -i '${Project.flagsmithClientAPI}environment-document/' \\
curl -i '${Constants.getFlagsmithSDKUrl()}environment-document/' \\
-H 'x-environment-key: ${serversideEnvironmentKey}' | tee flagsmith.json
`
4 changes: 3 additions & 1 deletion frontend/common/code-help/traits/traits-curl.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import Constants from 'common/constants'

module.exports = (
envId,
{ TRAIT_NAME, USER_ID },
userId,
) => `curl -i -X POST '${Project.flagsmithClientAPI}identities/' \\
) => `curl -i -X POST '${Constants.getFlagsmithSDKUrl()}identities/' \\
-H 'x-environment-key: ${envId}' \\
-H 'Content-Type: application/json; charset=utf-8' \\
-d $'{
Expand Down
4 changes: 2 additions & 2 deletions frontend/common/code-help/traits/traits-java.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ module.exports = (
) => `${LIB_NAME_JAVA} ${LIB_NAME} = ${LIB_NAME_JAVA}
.newBuilder()
.setApiKey("${envId}")${
Constants.isCustomFlagsmithUrl &&
Constants.isCustomFlagsmithUrl() &&
`\n .withConfiguration(FlagsmithConfig.builder()
.baseUri("${Project.flagsmithClientAPI}")
.baseUri("${Constants.getFlagsmithSDKUrl()}")
.build())`
}
.build();
Expand Down
4 changes: 2 additions & 2 deletions frontend/common/code-help/traits/traits-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module.exports = (
) => `// Option 1: initialise with an identity and traits
${LIB_NAME}.init({
environmentID: "${envId}",${
Constants.isCustomFlagsmithUrl
? `\n api: "${Project.flagsmithClientAPI}",`
Constants.isCustomFlagsmithUrl()
? `\n api: "${Constants.getFlagsmithSDKUrl()}",`
: ''
}
identity: "${userId || USER_ID}",
Expand Down
Loading
Loading