2023-05-25 17:50:18 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2023-05-27 08:04:41 +00:00
|
|
|
"os"
|
2023-05-25 17:50:18 +00:00
|
|
|
"strings"
|
|
|
|
|
2023-05-27 08:04:41 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/medium.rip/internal/config"
|
2023-05-25 17:50:18 +00:00
|
|
|
"github.com/medium.rip/pkg/entities"
|
|
|
|
)
|
|
|
|
|
|
|
|
func PostData(postId string) (*entities.MediumResponse, error) {
|
2023-05-27 08:04:41 +00:00
|
|
|
if config.Conf.Env == "dev" {
|
|
|
|
file, err := os.ReadFile("response.json")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mr, err := entities.UnmarshalMediumResponse(file)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error unmarshalling body from response %v\n", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &mr, nil
|
|
|
|
}
|
|
|
|
|
2023-05-25 17:50:18 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-05-27 06:21:19 +00:00
|
|
|
req.Header.Add("Accept", "application/json")
|
|
|
|
req.Header.Add("Content-Type", "application/json; charset=utf-8")
|
2023-05-25 17:50:18 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2023-05-26 14:12:53 +00:00
|
|
|
|
2023-05-25 17:50:18 +00:00
|
|
|
return &mr, nil
|
|
|
|
}
|