Skip to content

Commit

Permalink
Merge pull request #23 from StevenMaude/retrieve-html-following-api-c…
Browse files Browse the repository at this point in the history
…hange

Handle updated site API
  • Loading branch information
StevenMaude authored Dec 3, 2020
2 parents 06c2257 + 9cefa11 commit 11b1b7a
Showing 1 changed file with 51 additions and 23 deletions.
74 changes: 51 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"time"
)

type mail struct {
type mailHeader struct {
From string `json:"f"`
Subject string `json:"s"`
HTML string `json:"html"`
Text string `json:"text"`
}

type msg struct {
Expand All @@ -37,41 +37,58 @@ type inbox struct {
Msgs []msg `json:"msgs"`
}

func getMail(latestMsg msg) error {
func newHTTPClient() *http.Client {
return &http.Client{
Timeout: 20 * time.Second,
}
}

func getMailHeader(latestMsg msg) (mailHeader, error) {
msgURL := "https://getnada.com/api/v1/messages/" + latestMsg.UID
fmt.Println("Retrieving URL:", msgURL)
fmt.Println("Retrieving message URL:", msgURL)
var mh mailHeader

resp, err := http.Get(msgURL)
c := newHTTPClient()
resp, err := c.Get(msgURL)
if err != nil {
return err
return mh, err
}

// TODO: move out display of mail from getting mail.
defer resp.Body.Close()

mailMessage := mail{}
err = json.NewDecoder(resp.Body).Decode(&mailMessage)
err = json.NewDecoder(resp.Body).Decode(&mh)
if err != nil {
return err
return mh, err
}

fmt.Println("\nFrom :", mailMessage.From)
fmt.Println("Subject:", mailMessage.Subject)
fmt.Println("Plain text:")
fmt.Println(mailMessage.Text)
return mh, nil
}

fmt.Println("HTML:")
fmt.Println(mailMessage.HTML)
func getMailBody(latestMsg msg) (string, error) {
htmlURL := "https://getnada.com/api/v1/messages/html/" + latestMsg.UID
fmt.Println("Retrieving HTML", htmlURL)

c := newHTTPClient()
htmlResp, err := c.Get(htmlURL)
if err != nil {
return "", err
}
defer htmlResp.Body.Close()

return nil
html, err := ioutil.ReadAll(htmlResp.Body)
if err != nil {
return "", err
}

return string(html), nil
}

func getInbox(address string) (inbox, error) {
webInboxURL := "https://getnada.com/api/v1/inboxes/" + address
fmt.Println("Retrieving URL:", webInboxURL)

addressInbox := inbox{}
resp, err := http.Get(webInboxURL)
var addressInbox inbox
c := newHTTPClient()
resp, err := c.Get(webInboxURL)
if err != nil {
return addressInbox, err
}
Expand Down Expand Up @@ -104,10 +121,21 @@ func main() {
}
fmt.Println("Found", numberMsgs, "messages")

latestMsg := addressInbox.Msgs[0]
err = getMail(latestMsg)
latestMsgID := addressInbox.Msgs[0]
mh, err := getMailHeader(latestMsgID)
if err != nil {
fmt.Println("failed to get message metadate", err)
os.Exit(1)
}

mb, err := getMailBody(latestMsgID)
if err != nil {
fmt.Println("failed to get mail:", err)
os.Exit(1)
}

fmt.Println("From :", mh.From)
fmt.Println("Subject:", mh.Subject)
fmt.Println("HTML:")
fmt.Println(mb)
}

0 comments on commit 11b1b7a

Please sign in to comment.