bloggify() should return error messages
parent
05fb63e633
commit
c8fec23964
38
main.go
38
main.go
|
|
@ -192,9 +192,9 @@ func sendTest(cfg *Config, markdownFile string) {
|
||||||
|
|
||||||
/* Convert Markdown to Blog Post */
|
/* Convert Markdown to Blog Post */
|
||||||
/* *** */
|
/* *** */
|
||||||
url, valid := bloggify(cfg, subject, body, false)
|
url, err := bloggify(cfg, subject, body, false)
|
||||||
if !valid {
|
if err != nil {
|
||||||
fmt.Printf("Error converting '%s' into a blog post\n", markdownFile)
|
fmt.Printf("Error converting '%s' into a blog post\n%v\n", markdownFile, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -218,9 +218,9 @@ func sendTest(cfg *Config, markdownFile string) {
|
||||||
os.WriteFile(sha1File, []byte(sumString), 0666)
|
os.WriteFile(sha1File, []byte(sumString), 0666)
|
||||||
}
|
}
|
||||||
|
|
||||||
func bloggify(cfg *Config, subject, body string, isLive bool) (url string, valid bool) {
|
func bloggify(cfg *Config, subject, body string, isLive bool) (string, error) {
|
||||||
if !isLive {
|
if !isLive {
|
||||||
return "https://handmadecities.com/meetup-invite-test", true
|
return "https://handmadecities.com/meetup-invite-test", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Prepare request payload */
|
/* Prepare request payload */
|
||||||
|
|
@ -232,16 +232,14 @@ func bloggify(cfg *Config, subject, body string, isLive bool) (url string, valid
|
||||||
|
|
||||||
payloadBytes, err := json.Marshal(payload)
|
payloadBytes, err := json.Marshal(payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error encoding bloggify payload: %v\n", err)
|
return "", fmt.Errorf("error encoding payload: %v", err)
|
||||||
return "", false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Prepare request */
|
/* Prepare request */
|
||||||
/* *** */
|
/* *** */
|
||||||
req, err := http.NewRequest("POST", "https://deploy.handmadecities.com/v1/bloggify", bytes.NewReader(payloadBytes))
|
req, err := http.NewRequest("POST", "https://deploy.handmadecities.com/v1/bloggify", bytes.NewReader(payloadBytes))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error creating bloggify request: %v\n", err)
|
return "", fmt.Errorf("error creating request: %v", err)
|
||||||
return "", false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
@ -252,24 +250,21 @@ func bloggify(cfg *Config, subject, body string, isLive bool) (url string, valid
|
||||||
client := http.Client{}
|
client := http.Client{}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error making bloggify request: %v\n", err)
|
return "", fmt.Errorf("error making request: %v", err)
|
||||||
return "", false
|
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
/* Fail if status code is not in the 2xx range */
|
/* Fail if status code is not in the 2xx range */
|
||||||
/* *** */
|
/* *** */
|
||||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||||
fmt.Printf("Bloggify error: HTTP status code %d\n", resp.StatusCode)
|
return "", fmt.Errorf("error: HTTP status code %d", resp.StatusCode)
|
||||||
return "", false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Read response's body */
|
/* Read response's body */
|
||||||
/* *** */
|
/* *** */
|
||||||
respBody, err := ioutil.ReadAll(resp.Body)
|
respBody, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error reading bloggify response body: %v\n", err)
|
return "", fmt.Errorf("error reading response body: %v", err)
|
||||||
return "", false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Parse JSON response */
|
/* Parse JSON response */
|
||||||
|
|
@ -277,18 +272,17 @@ func bloggify(cfg *Config, subject, body string, isLive bool) (url string, valid
|
||||||
var data map[string]interface{}
|
var data map[string]interface{}
|
||||||
if err := json.Unmarshal(respBody, &data); err != nil {
|
if err := json.Unmarshal(respBody, &data); err != nil {
|
||||||
fmt.Printf("Error unmarshalling bloggify JSON: %v\n", err)
|
fmt.Printf("Error unmarshalling bloggify JSON: %v\n", err)
|
||||||
return "", false
|
return "", fmt.Errorf("error unmarshalling JSON: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Extract the URL from the response */
|
/* Extract the URL from the response */
|
||||||
/* *** */
|
/* *** */
|
||||||
url, ok := data["url"].(string)
|
url, ok := data["url"].(string)
|
||||||
if !ok || url == "" {
|
if !ok || url == "" {
|
||||||
fmt.Println("Bloggify response format error: 'url' field not found or empty")
|
return "", fmt.Errorf("response format error: 'url' field not found or empty")
|
||||||
return "", false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return url, true
|
return url, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendNews(cfg *Config, markdownFile string) {
|
func sendNews(cfg *Config, markdownFile string) {
|
||||||
|
|
@ -313,9 +307,9 @@ func sendNews(cfg *Config, markdownFile string) {
|
||||||
|
|
||||||
/* Convert Markdown to Blog Post */
|
/* Convert Markdown to Blog Post */
|
||||||
/* *** */
|
/* *** */
|
||||||
url, valid := bloggify(cfg, subject, body, true)
|
url, err := bloggify(cfg, subject, body, true)
|
||||||
if !valid {
|
if err != nil {
|
||||||
fmt.Printf("Error converting '%s' into a blog post\n", markdownFile)
|
fmt.Printf("Error converting '%s' into a blog post\n%v\n", markdownFile, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue