diff --git a/pkg/client/medium_client.go b/pkg/client/medium_client.go new file mode 100644 index 0000000..bd5a77a --- /dev/null +++ b/pkg/client/medium_client.go @@ -0,0 +1,92 @@ +package client + +import ( + "fmt" + "io" + "log" + "net/http" + "strings" + + "github.com/medium.rip/pkg/entities" +) + +func PostData(postId string) (*entities.MediumResponse, error) { + // http client to post data + url := "https://medium.com/_/graphql" + method := "POST" + + payload := strings.NewReader(fmt.Sprintf(`query { + post(id: "%s") { + title + createdAt + creator { + id + name + } + content { + bodyModel { + paragraphs { + name + text + type + href + layout + markups { + title + type + href + userId + start + end + anchorType + } + iframe { + mediaResource { + href + iframeSrc + iframeWidth + iframeHeight + } + } + metadata { + id + originalWidth + originalHeight + } + } + } + } + } + }`, postId)) + + client := &http.Client{} + req, err := http.NewRequest(method, url, payload) + if err != nil { + log.Printf("Error constructing request %v\n", err) + return nil, err + } + + req.Header.Add("Content-Type", "application/json") + + res, err := client.Do(req) + if err != nil { + log.Printf("Error making request to Medium API %v\n", err) + return nil, err + } + + defer res.Body.Close() + + body, err := io.ReadAll(res.Body) + if err != nil { + log.Printf("Error reading body from response %v\n", err) + return nil, err + } + + mr, err := entities.UnmarshalMediumResponse(body) + if err != nil { + log.Printf("Error unmarshalling body from response %v\n", err) + return nil, err + } + + return &mr, nil +} diff --git a/pkg/entities/medium_response.go b/pkg/entities/medium_response.go new file mode 100644 index 0000000..5370398 --- /dev/null +++ b/pkg/entities/medium_response.go @@ -0,0 +1,78 @@ +package entities + +import "encoding/json" + +func UnmarshalMediumResponse(data []byte) (MediumResponse, error) { + var r MediumResponse + err := json.Unmarshal(data, &r) + return r, err +} + +func (r *MediumResponse) Marshal() ([]byte, error) { + return json.Marshal(r) +} + +type MediumResponse struct { + Data Data `json:"data"` +} + +type Data struct { + Post Post `json:"post"` +} + +type Post struct { + Title string `json:"title"` + CreatedAt int64 `json:"createdAt"` + Creator Creator `json:"creator"` + Content Content `json:"content"` +} + +type Content struct { + BodyModel BodyModel `json:"bodyModel"` +} + +type BodyModel struct { + Paragraphs []Paragraph `json:"paragraphs"` +} + +type Paragraph struct { + Name string `json:"name"` + Text string `json:"text"` + Type Type `json:"type"` + Href interface{} `json:"href"` + Layout *string `json:"layout"` + Markups []Markup `json:"markups"` + Iframe interface{} `json:"iframe"` + Metadata *Metadata `json:"metadata"` +} + +type Markup struct { + Title *string `json:"title"` + Type string `json:"type"` + Href *string `json:"href"` + UserID interface{} `json:"userId"` + Start int64 `json:"start"` + End int64 `json:"end"` + AnchorType *string `json:"anchorType"` +} + +type Metadata struct { + ID string `json:"id"` + OriginalWidth int64 `json:"originalWidth"` + OriginalHeight int64 `json:"originalHeight"` +} + +type Creator struct { + ID string `json:"id"` + Name string `json:"name"` +} + +type Type string + +const ( + H3 Type = "H3" + H4 Type = "H4" + Img Type = "IMG" + P Type = "P" + Pre Type = "PRE" +)