Go client for the Write.as API https://developers.write.as
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
865 B

  1. package writeas
  2. import (
  3. "fmt"
  4. "net/http"
  5. )
  6. type BodyResponse struct {
  7. Body string `json:"body"`
  8. }
  9. // Markdown takes raw Markdown and renders it into usable HTML. See
  10. // https://developers.write.as/docs/api/#render-markdown.
  11. func (c *Client) Markdown(body, collectionURL string) (string, error) {
  12. p := &BodyResponse{}
  13. data := struct {
  14. RawBody string `json:"raw_body"`
  15. CollectionURL string `json:"collection_url,omitempty"`
  16. }{
  17. RawBody: body,
  18. CollectionURL: collectionURL,
  19. }
  20. env, err := c.post("/markdown", data, p)
  21. if err != nil {
  22. return "", err
  23. }
  24. var ok bool
  25. if p, ok = env.Data.(*BodyResponse); !ok {
  26. return "", fmt.Errorf("Wrong data returned from API.")
  27. }
  28. status := env.Code
  29. if status != http.StatusOK {
  30. return "", fmt.Errorf("Problem getting markdown: %d. %s\n", status, env.ErrorMessage)
  31. }
  32. return p.Body, nil
  33. }