Go client for the Write.as API https://developers.write.as
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

41 rader
902 B

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