From c6f66b3ec8935c4556fd8d504f72e12b94d73674 Mon Sep 17 00:00:00 2001 From: Kelly Hofmann Date: Thu, 21 Mar 2024 15:06:35 -0700 Subject: [PATCH 1/4] send POST and PATCH requests for updating/creating flags --- internal/setup/flag_toggle.go | 37 +++++++++++++++++++++++++++++++++++ internal/setup/flags.go | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/internal/setup/flag_toggle.go b/internal/setup/flag_toggle.go index 789d92ca..5518bc09 100644 --- a/internal/setup/flag_toggle.go +++ b/internal/setup/flag_toggle.go @@ -1,7 +1,11 @@ package setup import ( + "bytes" "fmt" + "net/http" + "time" + "github.com/charmbracelet/bubbles/key" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" @@ -11,6 +15,7 @@ type flagToggleModel struct { enabled bool flagKey string logType string + //err error } func NewFlagToggle() flagToggleModel { @@ -27,6 +32,9 @@ func (m flagToggleModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch { case key.Matches(msg, keys.Toggle): m.enabled = !m.enabled + + // uncomment to send PATCH + //m.err = m.toggleFlag() } } @@ -53,3 +61,32 @@ func (m flagToggleModel) View() string { return title + "\n\n" + toggleStyle.Render(toggle) + m.flagKey + furtherInstructions } + +func (m flagToggleModel) toggleFlag() error { + url := fmt.Sprintf("http://localhost/api/v2/flags/skylab/%s", m.flagKey) + c := &http.Client{ + Timeout: 10 * time.Second, + } + + toggleInstruction := "turnFlagOn" + if !m.enabled { + toggleInstruction = "turnFlagOff" + } + + body := fmt.Sprintf(`{ + "environmentKey": "production", + "instructions": [ { "kind": %q } ] + }`, toggleInstruction) + + req, _ := http.NewRequest("PATCH", url, bytes.NewBufferString(body)) + req.Header.Add("Authorization", "") // add token here + req.Header.Add("Content-type", "application/json; domain-model=launchdarkly.semanticpatch") + + res, err := c.Do(req) + if err != nil { + return err + } + defer res.Body.Close() + + return nil +} diff --git a/internal/setup/flags.go b/internal/setup/flags.go index 07c813d0..bd5bdd62 100644 --- a/internal/setup/flags.go +++ b/internal/setup/flags.go @@ -1,7 +1,10 @@ package setup import ( + "bytes" "fmt" + "net/http" + "time" "github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/list" @@ -26,6 +29,7 @@ func (p flag) FilterValue() string { return "" } type flagModel struct { input string textInput textinput.Model + //err error } func NewFlag() tea.Model { @@ -55,6 +59,9 @@ func (m flagModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { input = defaultFlagKey } m.input = input + + // uncomment to send POST + //m.err = createFlag(input) case key.Matches(msg, keys.Quit): return m, tea.Quit default: @@ -74,3 +81,30 @@ func (m flagModel) View() string { style.Render(m.textInput.View()), ) + "\n" } + +func (m flagModel) createFlag() error { + url := "http://localhost/api/v2/flags/skylab" + c := &http.Client{ + Timeout: 10 * time.Second, + } + + body := fmt.Sprintf(`{ + "name": %q, + "key": %q + }`, + m.input, + m.input, + ) + + req, _ := http.NewRequest("POST", url, bytes.NewBufferString(body)) + req.Header.Add("Authorization", "") // add token here + req.Header.Add("Content-type", "application/json") + + res, err := c.Do(req) + if err != nil { + return err + } + defer res.Body.Close() + + return nil +} From 620bd0bc86baa4dd85e5b50966019f9dce230a44 Mon Sep 17 00:00:00 2001 From: Kelly Hofmann Date: Thu, 21 Mar 2024 15:10:08 -0700 Subject: [PATCH 2/4] change project key --- internal/setup/flag_toggle.go | 2 +- internal/setup/flags.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/setup/flag_toggle.go b/internal/setup/flag_toggle.go index 5518bc09..d22499b2 100644 --- a/internal/setup/flag_toggle.go +++ b/internal/setup/flag_toggle.go @@ -63,7 +63,7 @@ func (m flagToggleModel) View() string { } func (m flagToggleModel) toggleFlag() error { - url := fmt.Sprintf("http://localhost/api/v2/flags/skylab/%s", m.flagKey) + url := fmt.Sprintf("http://localhost/api/v2/flags/default/%s", m.flagKey) c := &http.Client{ Timeout: 10 * time.Second, } diff --git a/internal/setup/flags.go b/internal/setup/flags.go index bd5bdd62..1d23faac 100644 --- a/internal/setup/flags.go +++ b/internal/setup/flags.go @@ -83,7 +83,7 @@ func (m flagModel) View() string { } func (m flagModel) createFlag() error { - url := "http://localhost/api/v2/flags/skylab" + url := "http://localhost/api/v2/flags/default" c := &http.Client{ Timeout: 10 * time.Second, } From 4bd9e2cd1d257eddbedba7124383936ad294b594 Mon Sep 17 00:00:00 2001 From: Kelly Hofmann Date: Thu, 21 Mar 2024 15:11:00 -0700 Subject: [PATCH 3/4] change env key --- internal/setup/flag_toggle.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/setup/flag_toggle.go b/internal/setup/flag_toggle.go index d22499b2..22ac8984 100644 --- a/internal/setup/flag_toggle.go +++ b/internal/setup/flag_toggle.go @@ -74,7 +74,7 @@ func (m flagToggleModel) toggleFlag() error { } body := fmt.Sprintf(`{ - "environmentKey": "production", + "environmentKey": "test", "instructions": [ { "kind": %q } ] }`, toggleInstruction) From a3e35c06d65495f4d251a51ad4143a325ab56e22 Mon Sep 17 00:00:00 2001 From: Kelly Hofmann Date: Thu, 21 Mar 2024 15:15:27 -0700 Subject: [PATCH 4/4] add apiToken const --- internal/setup/flag_toggle.go | 2 +- internal/setup/flags.go | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/internal/setup/flag_toggle.go b/internal/setup/flag_toggle.go index 22ac8984..3d9ba8ad 100644 --- a/internal/setup/flag_toggle.go +++ b/internal/setup/flag_toggle.go @@ -79,7 +79,7 @@ func (m flagToggleModel) toggleFlag() error { }`, toggleInstruction) req, _ := http.NewRequest("PATCH", url, bytes.NewBufferString(body)) - req.Header.Add("Authorization", "") // add token here + req.Header.Add("Authorization", apiToken) // add token here req.Header.Add("Content-type", "application/json; domain-model=launchdarkly.semanticpatch") res, err := c.Do(req) diff --git a/internal/setup/flags.go b/internal/setup/flags.go index 1d23faac..193f8a1f 100644 --- a/internal/setup/flags.go +++ b/internal/setup/flags.go @@ -61,7 +61,7 @@ func (m flagModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.input = input // uncomment to send POST - //m.err = createFlag(input) + //m.err = m.createFlag() case key.Matches(msg, keys.Quit): return m, tea.Quit default: @@ -82,6 +82,8 @@ func (m flagModel) View() string { ) + "\n" } +const apiToken = "" + func (m flagModel) createFlag() error { url := "http://localhost/api/v2/flags/default" c := &http.Client{ @@ -97,7 +99,7 @@ func (m flagModel) createFlag() error { ) req, _ := http.NewRequest("POST", url, bytes.NewBufferString(body)) - req.Header.Add("Authorization", "") // add token here + req.Header.Add("Authorization", apiToken) // add token here req.Header.Add("Content-type", "application/json") res, err := c.Do(req)