added get request abstraction http utility
This commit is contained in:
parent
387d4840c4
commit
f264b68565
30
main.go
30
main.go
@ -23,34 +23,10 @@ func main() {
|
|||||||
//Upload Photo + add to cache
|
//Upload Photo + add to cache
|
||||||
//Use cached data to correctly build post form submissions
|
//Use cached data to correctly build post form submissions
|
||||||
|
|
||||||
createPost := wordpress.CreatePost{
|
posts := wordpress.GetPosts(baseUrl, user, password)
|
||||||
Title: "Test 634534",
|
for i, postData := range posts {
|
||||||
Content: "<h1>HELLO WORLD</h1>",
|
fmt.Println("Post Number:", i+1, postData.Title.Rendered)
|
||||||
Excerpt: "This article will blow your mind.",
|
|
||||||
FeaturedMedia: 18,
|
|
||||||
Author: 1,
|
|
||||||
Tags: []int{6, 3},
|
|
||||||
Status: "publish",
|
|
||||||
Categories: []int{7},
|
|
||||||
Slug: "/scoop-it-up",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
post := createPost.Execute(baseUrl, user, password)
|
|
||||||
fmt.Println(post.Title.Rendered)
|
|
||||||
|
|
||||||
createUser := wordpress.CreateUser{
|
|
||||||
Username: "Crkaaadfdfdfain",
|
|
||||||
Password: "raadfadomPass",
|
|
||||||
Name: "Chekadadsing Msocadsfker",
|
|
||||||
FirstName: "Cafhefarking",
|
|
||||||
LastName: "McCloaaddfcker",
|
|
||||||
Email: "chedfsddfsdfgs@gmail.com",
|
|
||||||
Roles: "author",
|
|
||||||
}
|
|
||||||
|
|
||||||
user := createUser.Execute(baseUrl, user, password)
|
|
||||||
appCache.SetUser(user)
|
|
||||||
fmt.Println("Created:", user.Username)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type AppCache struct {
|
type AppCache struct {
|
||||||
|
@ -3,8 +3,6 @@ package wordpress
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"federated.computer/wp-sync-slowtwitch/utilities"
|
"federated.computer/wp-sync-slowtwitch/utilities"
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type PostData struct {
|
type PostData struct {
|
||||||
@ -23,18 +21,12 @@ type PostData struct {
|
|||||||
FeaturedMediaId int `json:"featured_media"`
|
FeaturedMediaId int `json:"featured_media"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetPosts(baseUrl string) []PostData {
|
func GetPosts(baseUrl, user, pass string) []PostData {
|
||||||
url := "posts?per_page=99"
|
url := baseUrl + "posts?per_page=99"
|
||||||
req, err := http.Get(baseUrl + url)
|
body := utilities.GetHttpRequestToWordpress(url, user, pass)
|
||||||
utilities.CheckError(err)
|
|
||||||
|
|
||||||
defer utilities.CloseBodyAndCheckError(req.Body)
|
|
||||||
|
|
||||||
body, err := io.ReadAll(req.Body)
|
|
||||||
utilities.CheckError(err)
|
|
||||||
|
|
||||||
var posts []PostData
|
var posts []PostData
|
||||||
err = json.Unmarshal(body, &posts)
|
err := json.Unmarshal(body, &posts)
|
||||||
utilities.CheckError(err)
|
utilities.CheckError(err)
|
||||||
return posts
|
return posts
|
||||||
}
|
}
|
||||||
|
@ -9,10 +9,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func GetHttpRequestToWordpress(url, user, pass string) []byte {
|
func GetHttpRequestToWordpress(url, user, pass string) []byte {
|
||||||
request, err := http.NewRequest("GET", url, strings.NewReader(""))
|
request, err := http.NewRequest("GET", url, nil)
|
||||||
addAuth(user, pass, request)
|
addAuth(user, pass, request)
|
||||||
CheckError(err)
|
CheckError(err)
|
||||||
body := RequestToWordpress(request, user, pass)
|
body := requestToWordpress(request, user, pass)
|
||||||
return body
|
return body
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -22,24 +22,19 @@ func PostHttpRequestToWordpress(url, user, pass string, parameters any) []byte {
|
|||||||
CheckError(err)
|
CheckError(err)
|
||||||
request, err := http.NewRequest("POST", url, strings.NewReader(requestBody))
|
request, err := http.NewRequest("POST", url, strings.NewReader(requestBody))
|
||||||
CheckError(err)
|
CheckError(err)
|
||||||
body := RequestToWordpress(request, user, pass)
|
body := requestToWordpress(request, user, pass)
|
||||||
return body
|
return body
|
||||||
}
|
}
|
||||||
|
|
||||||
func RequestToWordpress(request *http.Request, user, pass string) []byte {
|
func requestToWordpress(request *http.Request, user, pass string) []byte {
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
addAuth(user, pass, request)
|
addAuth(user, pass, request)
|
||||||
//send
|
|
||||||
response, err := client.Do(request)
|
response, err := client.Do(request)
|
||||||
CheckError(err)
|
CheckError(err)
|
||||||
//make sure to close body
|
|
||||||
defer CloseBodyAndCheckError(response.Body)
|
defer CloseBodyAndCheckError(response.Body)
|
||||||
|
checkHttpResponseStatus(response)
|
||||||
CheckHttpResponseStatus(response)
|
|
||||||
|
|
||||||
body, err := io.ReadAll(response.Body)
|
body, err := io.ReadAll(response.Body)
|
||||||
CheckError(err)
|
CheckError(err)
|
||||||
|
|
||||||
return body
|
return body
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,7 +43,7 @@ func addAuth(user, pass string, request *http.Request) {
|
|||||||
request.Header.Add("content-type", "application/json")
|
request.Header.Add("content-type", "application/json")
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckHttpResponseStatus(response *http.Response) {
|
func checkHttpResponseStatus(response *http.Response) {
|
||||||
if response.StatusCode != 200 {
|
if response.StatusCode != 200 {
|
||||||
fmt.Println("Status Code:", response.Status, http.StatusText(response.StatusCode))
|
fmt.Println("Status Code:", response.Status, http.StatusText(response.StatusCode))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user