-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Mark Cornick <mcornick@mcornick.com>
- Loading branch information
Showing
3 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |