mirror of
https://github.com/SphericalKat/medium.rip.git
synced 2024-12-26 01:25:57 +00:00
feat: add method to fetch response from medium API
Signed-off-by: Sphericalkat <amolele@gmail.com>
This commit is contained in:
parent
83154587d8
commit
6117c82d32
92
pkg/client/medium_client.go
Normal file
92
pkg/client/medium_client.go
Normal file
@ -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
|
||||
}
|
78
pkg/entities/medium_response.go
Normal file
78
pkg/entities/medium_response.go
Normal file
@ -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"
|
||||
)
|
Loading…
Reference in New Issue
Block a user