mirror of
https://github.com/SphericalKat/medium.rip.git
synced 2026-10-21 01:34:22 +00:00
feat: add method to fetch response from medium API
Signed-off-by: Sphericalkat <amolele@gmail.com>
This commit is contained in:
1 parent
83154587d8
commit
6117c82d32
2 files changed
+170
No files matched your search
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user