commit 387d4840c48ca2470381560d5e891e0e0e55ea2d Author: Ross Trottier Date: Tue May 7 16:03:15 2024 -0600 get user or false diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/WordpressPostMigrator.iml b/.idea/WordpressPostMigrator.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/WordpressPostMigrator.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..7ce2cca --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..584a23a --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module federated.computer/wp-sync-slowtwitch + +go 1.22.2 diff --git a/main.go b/main.go new file mode 100644 index 0000000..dfb4be5 --- /dev/null +++ b/main.go @@ -0,0 +1,70 @@ +package main + +import ( + "federated.computer/wp-sync-slowtwitch/services/wordpress" + "fmt" +) + +const baseUrl = "https://go-api-playground.local/wp-json/wp/v2/" +const user = "admin" +const password = "S34E keY1 A1uX 6ncs Rx4T f21W" + +var appCache AppCache + +func main() { + //Get user and cache + //Get category and cache + //Get single category from cache by name + //Get tag and cache + //Get single tag from cache by name + //Get photo and cache with URL + //Create Category (with parent) + add to cache + //Create Tag + add to cache + //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", + } + + 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 { + UsersCache map[string]wordpress.UserData +} + +func (appCache *AppCache) GetUser(username string) (wordpress.UserData, bool) { + user, ok := appCache.UsersCache[username] + return user, ok +} + +func (appCache *AppCache) SetUser(user wordpress.UserData) { + if appCache.UsersCache == nil { + appCache.UsersCache = make(map[string]wordpress.UserData) + } + appCache.UsersCache[user.Username] = user +} diff --git a/services/wordpress/create-post.go b/services/wordpress/create-post.go new file mode 100644 index 0000000..7ea188a --- /dev/null +++ b/services/wordpress/create-post.go @@ -0,0 +1,28 @@ +package wordpress + +import ( + "encoding/json" + "federated.computer/wp-sync-slowtwitch/utilities" +) + +type CreatePost struct { + Title string `json:"title"` + Content string `json:"content"` + Excerpt string `json:"excerpt"` + FeaturedMedia int `json:"featured_media"` + Author int `json:"author"` + Tags []int `json:"tags"` + Status string `json:"status"` + Categories []int `json:"categories"` + Slug string `json:"slug"` +} + +func (parameters *CreatePost) Execute(baseUrl, user, pass string) PostData { + endpoint := baseUrl + "posts" + body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters) + var post PostData + err := json.Unmarshal(body, &post) + utilities.CheckError(err) + + return post +} diff --git a/services/wordpress/create-user.go b/services/wordpress/create-user.go new file mode 100644 index 0000000..e07abd0 --- /dev/null +++ b/services/wordpress/create-user.go @@ -0,0 +1,25 @@ +package wordpress + +import ( + "encoding/json" + "federated.computer/wp-sync-slowtwitch/utilities" +) + +type CreateUser struct { + Username string `json:"username"` + FirstName string `json:"first_name"` + LastName string `json:"last_name"` + Name string `json:"name"` + Email string `json:"email"` + Roles string `json:"roles"` + Password string `json:"password"` +} + +func (parameters *CreateUser) Execute(baseUrl, user, pass string) UserData { + endpoint := baseUrl + "users" + body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters) + var userData UserData + err := json.Unmarshal(body, &userData) + utilities.CheckError(err) + return userData +} diff --git a/services/wordpress/get-posts.go b/services/wordpress/get-posts.go new file mode 100644 index 0000000..dec77af --- /dev/null +++ b/services/wordpress/get-posts.go @@ -0,0 +1,40 @@ +package wordpress + +import ( + "encoding/json" + "federated.computer/wp-sync-slowtwitch/utilities" + "io" + "net/http" +) + +type PostData struct { + Id int `json:"id"` + Link string `json:"link"` + Title struct { + Rendered string `json:"rendered"` + } `json:"title"` + Content struct { + Rendered string `json:"rendered"` + } `json:"content"` + AuthorId int `json:"author"` + Excerpt struct { + Rendered string `json:"rendered"` + } `json:"excerpt"` + 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) + + var posts []PostData + err = json.Unmarshal(body, &posts) + utilities.CheckError(err) + return posts +} diff --git a/services/wordpress/get-user.go b/services/wordpress/get-user.go new file mode 100644 index 0000000..170c49b --- /dev/null +++ b/services/wordpress/get-user.go @@ -0,0 +1,33 @@ +package wordpress + +import ( + "encoding/json" + "federated.computer/wp-sync-slowtwitch/utilities" + "net/url" +) + +type UserData struct { + Id int `json:"id"` + Username string `json:"username"` + Email string `json:"email"` + FirstName string `json:"first_name"` + LastName string `json:"last_name"` +} + +func GetUser(baseUrl, username, user, pass string) (UserData, bool) { + endpoint := baseUrl + "/users?search=" + url.QueryEscape(username) + body := utilities.GetHttpRequestToWordpress(endpoint, user, pass) + + var userData []UserData + err := json.Unmarshal(body, &userData) + + if err == nil { + for _, userData := range userData { + if userData.Username == username { + return userData, true + } + } + } + + return UserData{}, false +} diff --git a/utilities/error-utilities.go b/utilities/error-utilities.go new file mode 100644 index 0000000..8b18ebe --- /dev/null +++ b/utilities/error-utilities.go @@ -0,0 +1,11 @@ +package utilities + +import ( + "fmt" +) + +func CheckError(err error) { + if err != nil { + fmt.Println(err.Error()) + } +} diff --git a/utilities/http-utilities.go b/utilities/http-utilities.go new file mode 100644 index 0000000..61c0d59 --- /dev/null +++ b/utilities/http-utilities.go @@ -0,0 +1,55 @@ +package utilities + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "strings" +) + +func GetHttpRequestToWordpress(url, user, pass string) []byte { + request, err := http.NewRequest("GET", url, strings.NewReader("")) + addAuth(user, pass, request) + CheckError(err) + body := RequestToWordpress(request, user, pass) + return body +} + +func PostHttpRequestToWordpress(url, user, pass string, parameters any) []byte { + requestBodyBytes, err := json.Marshal(parameters) + requestBody := string(requestBodyBytes) + CheckError(err) + request, err := http.NewRequest("POST", url, strings.NewReader(requestBody)) + CheckError(err) + body := RequestToWordpress(request, user, pass) + return body +} + +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) + + body, err := io.ReadAll(response.Body) + CheckError(err) + + return body +} + +func addAuth(user, pass string, request *http.Request) { + request.SetBasicAuth(user, pass) + request.Header.Add("content-type", "application/json") +} + +func CheckHttpResponseStatus(response *http.Response) { + if response.StatusCode != 200 { + fmt.Println("Status Code:", response.Status, http.StatusText(response.StatusCode)) + } +} diff --git a/utilities/io-utilities.go b/utilities/io-utilities.go new file mode 100644 index 0000000..6559f2f --- /dev/null +++ b/utilities/io-utilities.go @@ -0,0 +1,8 @@ +package utilities + +import "io" + +func CloseBodyAndCheckError(Body io.ReadCloser) { + err := Body.Close() + CheckError(err) +}