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

commands: features, metrics, experiments #31

Merged
merged 11 commits into from
May 11, 2023
Merged

Conversation

tinahollygb
Copy link
Contributor

@tinahollygb tinahollygb commented May 10, 2023

New commands

Adds the following new commands:

growthbook experiments get ID
growthbook experiments list
growthbook experiments results EXPERIMENTID
growthbook features get FEATUREKEY
growthbook features list
growthbook metrics get METRICID
growthbook metrics list

You can run yarn build && ./bin/run features list > output.json to put the features response into a file output.json.

Architecture

Standardizes the architecture for networking and command generation. Running yarn plop command and yarn plop repository will generate some standardized files which include considerations for default arguments and flags (e.g. pagination, profile, base URL, etc.). See the plop templates for details.

This approach will allow us to wrap the networking calls in case we need to change the implementation for whatever reason (e.g. if we need to move to a different generator instead of typescript-axios).

The repositories also attach the user agent growthbook-cli/<version>.

Commands

Commands are bootstrapped with default args that the user can configure, e.g. base URL and profile. We also have reusable command flags for pagination.

Repositories

The repositories are generated so contributors don't need to worry about the interfaces and super methods. Provided the resource exists, generating a new repository is as simple as running yarn plop repository (or just yarn plop and choosing "repository"). Doing yarn plop repository DataSource results in the following:

import {BaseApiRepository, BaseRepository} from './base.repository'
import {DataSourcesApi} from '../generated/api'

export class DataSourcesRepository extends BaseRepository implements BaseApiRepository<DataSourcesApi> {
  api(): DataSourcesApi  {
    return new DataSourcesApi(this.apiConfig())
  }
}

And for the experiments repo:

export class ExperimentsRepository extends BaseRepository implements BaseApiRepository<ExperimentsApi> {
  api(): ExperimentsApi  {
    return new ExperimentsApi(this.apiConfig())
  }
}

This means you can now call this.api() in any repository to get the OpenAPI-generated API client already configured with the right base URL, and auth and user agent headers. For example, to add support to get an experiment by ID to the generated ExperimentsRepository, we can add a method with a similar (or the same) signature and forward it:

@@ -1,6 +1,12 @@
 export class ExperimentsRepository extends BaseRepository implements BaseApiRepository<ExperimentsApi> {
+  public async getExperiment(id: string): Promise<Experiment> {
+    const experimentsApi = this.api()
+    const result = await experimentsApi.getExperiment(id)
 
+    return result.data.experiment
+  }
+
   api(): ExperimentsApi  {
     return new ExperimentsApi(this.apiConfig())
   }
 }

The BaseRepository implements the OpenAPI configuration as well as other configuration:

  • ensures the base URL is prefixed with the provided version or /api/v1 if left blank
  • ensures the access token is added to the authorization header
  • ensures the user agent growthbook-cli/<cli-version> is added to the user agent header

Testing these changes

Run yarn build then run all of the above-mentioned commands. Instead of growthbook for local development you'll need to run ./bin/run instead so ./bin/run features list.

You can run yarn build && ./bin/run features list > output.json to put the features response into a file output.json.

@tinahollygb tinahollygb marked this pull request as ready for review May 10, 2023 23:38
@tinahollygb tinahollygb requested a review from jdorn May 10, 2023 23:48
@tinahollygb tinahollygb merged commit fc4ec31 into main May 11, 2023
@tinahollygb tinahollygb deleted the tina/get-features branch May 11, 2023 00:19
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 this pull request may close these issues.

1 participant