From dc52a952e0c183f5754dc3486ddaed04d42009b3 Mon Sep 17 00:00:00 2001 From: Mark Cornick Date: Thu, 2 Mar 2023 23:10:32 -0500 Subject: [PATCH] Add webpage get and set Signed-off-by: Mark Cornick --- omglol/models.go | 15 +++++++++ omglol/web.go | 81 ++++++++++++++++++++++++++++++++++++++++++++++ omglol/web_test.go | 51 +++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 omglol/web.go create mode 100644 omglol/web_test.go diff --git a/omglol/models.go b/omglol/models.go index 12528ec..917273c 100644 --- a/omglol/models.go +++ b/omglol/models.go @@ -169,3 +169,18 @@ type PersistentURL struct { Counter *int64 `json:"counter"` Listed bool `json:"listed"` } + +type Web struct { + Message string `json:"message"` + Content string `json:"content"` + ContentBytes []byte `json:"omitempty"` + Type string `json:"type"` + Theme string `json:"theme"` + CSS string `json:"css"` + Head string `json:"head"` + Verified bool `json:"verified"` + PFP string `json:"pfp"` + Metadata string `json:"metadata"` + Branding string `json:"branding"` + Modified string `json:"modified"` +} diff --git a/omglol/web.go b/omglol/web.go new file mode 100644 index 0000000..669643b --- /dev/null +++ b/omglol/web.go @@ -0,0 +1,81 @@ +package omglol + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" +) + +// Get webpage content for a domain. See https://api.omg.lol/#token-get-web-retrieve-web-page-content +func (c *Client) GetWeb(domain string) (*Web, error) { + req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/address/%s/web", c.HostURL, domain), nil) + if err != nil { + return nil, err + } + + body, err := c.doRequest(req) + if err != nil { + return nil, err + } + + type webResponse struct { + Request request `json:"request"` + Response Web `json:"response"` + } + + var w webResponse + if err = json.Unmarshal(body, &w); err != nil { + fmt.Printf("error unmarshalling response: %v\n", err) + return nil, err + } + w.Response.ContentBytes = []byte(w.Response.Content) + return &w.Response, nil +} + +// Update webpage content for a domain. See https://api.omg.lol/#token-post-web-update-web-page-content-and-publish +func (c *Client) SetWeb(domain string, content []byte, publish bool) (bool, error) { + type setWebInput struct { + Content string `json:"content"` + Publish bool `json:"publish,omitempty"` + } + + input := setWebInput{string(content), publish} + jsonData, err := json.Marshal(input) + if err != nil { + return false, err + } + + req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/address/%s/web", c.HostURL, domain), bytes.NewBuffer([]byte(jsonData))) + if err != nil { + return false, err + } + + body, err := c.doRequest(req) + if err != nil { + return false, fmt.Errorf("sent: %s, error: %w", jsonData, err) + } + + var r apiResponse + if err := json.Unmarshal(body, &r); err != nil { + fmt.Printf("error unmarshalling response: %v\n", err) + return false, err + } + + type decodedResponse struct { + Message string `json:"message"` + } + var m decodedResponse + if err := json.Unmarshal(r.Response, &m); err != nil { + fmt.Printf("error unmarshalling response: %v\n", err) + return false, err + } + + if m.Message == "Your web content has been saved and published." { + return true, nil + } else if m.Message == "Your web content has been saved." { + return false, nil + } else { + return false, fmt.Errorf("unexpected response: %s", m.Message) + } +} diff --git a/omglol/web_test.go b/omglol/web_test.go new file mode 100644 index 0000000..c7c80e7 --- /dev/null +++ b/omglol/web_test.go @@ -0,0 +1,51 @@ +package omglol + +import ( + "testing" +) + +func TestGetSetRestoreWeb(t *testing.T) { + sleep() + c, err := NewClient(testEmail, testKey, testHostURL) + if err != nil { + t.Errorf(err.Error()) + } + + w, err := c.GetWeb(testOwnedDomain) + if err != nil { + t.Errorf(err.Error()) + } + + previousContent := w.Content + newContent := "this is the new content\n\nit has newlines\n" + + sleep() + published, err := c.SetWeb(testOwnedDomain, []byte(newContent), false) + if err != nil { + t.Errorf(err.Error()) + } + if published { + t.Errorf("Expected published to be false, got true") + } + + sleep() + w, err = c.GetWeb(testOwnedDomain) + if err != nil { + t.Errorf(err.Error()) + } + if w.Content != newContent { + t.Errorf("Expected content '%s', got '%s'", newContent, w.Content) + } + if string(w.ContentBytes) != newContent { + t.Errorf("Expected ContentBytes '%s', got '%s'", newContent, string(w.Content)) + } + + sleep() + published, err = c.SetWeb(testOwnedDomain, []byte(previousContent), true) + if err != nil { + t.Errorf(err.Error()) + } + if !published { + t.Errorf("Expected published to be true, got false") + } +}