diff --git a/main.go b/main.go index dfb4be5..1cd3cfa 100644 --- a/main.go +++ b/main.go @@ -23,34 +23,10 @@ func main() { //Upload Photo + add to cache //Use cached data to correctly build post form submissions - createPost := wordpress.CreatePost{ - Title: "Test 634534", - Content: "

HELLO WORLD

", - Excerpt: "This article will blow your mind.", - FeaturedMedia: 18, - Author: 1, - Tags: []int{6, 3}, - Status: "publish", - Categories: []int{7}, - Slug: "/scoop-it-up", + posts := wordpress.GetPosts(baseUrl, user, password) + for i, postData := range posts { + fmt.Println("Post Number:", i+1, postData.Title.Rendered) } - - 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 { diff --git a/services/wordpress/get-posts.go b/services/wordpress/get-posts.go index dec77af..d042f11 100644 --- a/services/wordpress/get-posts.go +++ b/services/wordpress/get-posts.go @@ -3,8 +3,6 @@ package wordpress import ( "encoding/json" "federated.computer/wp-sync-slowtwitch/utilities" - "io" - "net/http" ) type PostData struct { @@ -23,18 +21,12 @@ type PostData struct { FeaturedMediaId int `json:"featured_media"` } -func GetPosts(baseUrl string) []PostData { - url := "posts?per_page=99" - req, err := http.Get(baseUrl + url) - utilities.CheckError(err) - - defer utilities.CloseBodyAndCheckError(req.Body) - - body, err := io.ReadAll(req.Body) - utilities.CheckError(err) +func GetPosts(baseUrl, user, pass string) []PostData { + url := baseUrl + "posts?per_page=99" + body := utilities.GetHttpRequestToWordpress(url, user, pass) var posts []PostData - err = json.Unmarshal(body, &posts) + err := json.Unmarshal(body, &posts) utilities.CheckError(err) return posts } diff --git a/utilities/http-utilities.go b/utilities/http-utilities.go index 61c0d59..a745147 100644 --- a/utilities/http-utilities.go +++ b/utilities/http-utilities.go @@ -9,10 +9,10 @@ import ( ) 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) CheckError(err) - body := RequestToWordpress(request, user, pass) + body := requestToWordpress(request, user, pass) return body } @@ -22,24 +22,19 @@ func PostHttpRequestToWordpress(url, user, pass string, parameters any) []byte { CheckError(err) request, err := http.NewRequest("POST", url, strings.NewReader(requestBody)) CheckError(err) - body := RequestToWordpress(request, user, pass) + body := requestToWordpress(request, user, pass) return body } -func RequestToWordpress(request *http.Request, user, pass string) []byte { +func requestToWordpress(request *http.Request, user, pass string) []byte { client := &http.Client{} addAuth(user, pass, request) - //send response, err := client.Do(request) CheckError(err) - //make sure to close body defer CloseBodyAndCheckError(response.Body) - - CheckHttpResponseStatus(response) - + checkHttpResponseStatus(response) body, err := io.ReadAll(response.Body) CheckError(err) - return body } @@ -48,7 +43,7 @@ func addAuth(user, pass string, request *http.Request) { request.Header.Add("content-type", "application/json") } -func CheckHttpResponseStatus(response *http.Response) { +func checkHttpResponseStatus(response *http.Response) { if response.StatusCode != 200 { fmt.Println("Status Code:", response.Status, http.StatusText(response.StatusCode)) }