Skip to content

Commit

Permalink
Add webpage get and set
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Cornick <mcornick@mcornick.com>
  • Loading branch information
mcornick committed Mar 4, 2023
1 parent ebd0cf6 commit dc52a95
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
15 changes: 15 additions & 0 deletions omglol/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
81 changes: 81 additions & 0 deletions omglol/web.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
51 changes: 51 additions & 0 deletions omglol/web_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}

0 comments on commit dc52a95

Please sign in to comment.