This commit is contained in:
Ross Trottier 2024-05-07 19:23:21 -06:00
parent 880d4985f4
commit 6f1b4a7fe9
7 changed files with 74 additions and 11 deletions

View File

@ -4,6 +4,7 @@ import "federated.computer/wp-sync-slowtwitch/services/wordpress"
type AppCache struct { type AppCache struct {
UsersCache map[string]wordpress.UserData UsersCache map[string]wordpress.UserData
TagCache map[string]wordpress.TagData
} }
func (appCache *AppCache) GetUser(username string) (wordpress.UserData, bool) { func (appCache *AppCache) GetUser(username string) (wordpress.UserData, bool) {
@ -17,3 +18,15 @@ func (appCache *AppCache) SetUser(user wordpress.UserData) {
} }
appCache.UsersCache[user.Name] = user appCache.UsersCache[user.Name] = user
} }
func (appCache *AppCache) GetTag(tagName string) (wordpress.TagData, bool) {
tag, ok := appCache.TagCache[tagName]
return tag, ok
}
func (appCache *AppCache) SetTag(tag wordpress.TagData) {
if appCache.TagCache == nil {
appCache.TagCache = make(map[string]wordpress.TagData)
}
appCache.TagCache[tag.Name] = tag
}

View File

@ -12,7 +12,6 @@ const wordpressSecret = "S34E keY1 A1uX 6ncs Rx4T f21W"
var appCache AppCache var appCache AppCache
func main() { func main() {
//Get user and cache
//Get category and cache //Get category and cache
//Get single category from cache by name //Get single category from cache by name
//Get tag and cache //Get tag and cache
@ -23,11 +22,11 @@ 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
user, ok := wordpress.GetUser(baseUrl, "Bongo Bangidsn Hobs", wordpressKey, wordpressSecret) tag, ok := wordpress.GetTag("golang-tag", baseUrl, wordpressKey, wordpressSecret)
if ok { if ok {
fmt.Println("User found:", user.Name) appCache.SetTag(tag)
appCache.SetUser(user) fmt.Println("Found tag:", tag.Name)
} else { } else {
fmt.Println("User not found") fmt.Println("Could not find tag.")
} }
} }

View File

@ -28,6 +28,5 @@ func (parameters *CreatePost) Execute(baseUrl, user, pass string) CreatePostResp
var post CreatePostResponse var post CreatePostResponse
err := json.Unmarshal(body, &post) err := json.Unmarshal(body, &post)
utilities.CheckError(err) utilities.CheckError(err)
return post return post
} }

View File

@ -0,0 +1,27 @@
package wordpress
import (
"encoding/json"
"federated.computer/wp-sync-slowtwitch/utilities"
)
type CreateTag struct {
Name string `json:"name"`
Description string `json:"description"`
Slug string `json:"slug"`
}
type CreateTagResponse struct {
Id int `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
}
func (parameters *CreateTag) Execute(baseUrl, user, pass string) CreateTagResponse {
endpoint := baseUrl + "tags"
body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters)
var tagResponse CreateTagResponse
err := json.Unmarshal(body, &tagResponse)
utilities.CheckError(err)
return tagResponse
}

View File

@ -23,10 +23,10 @@ type CreateUserResponse struct {
LastName string `json:"last_name"` LastName string `json:"last_name"`
} }
func (parameters *CreateUser) Execute(baseUrl, user, pass string) UserData { func (parameters *CreateUser) Execute(baseUrl, user, pass string) CreateUserResponse {
endpoint := baseUrl + "users" endpoint := baseUrl + "users"
body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters) body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters)
var userData UserData var userData CreateUserResponse
err := json.Unmarshal(body, &userData) err := json.Unmarshal(body, &userData)
utilities.CheckError(err) utilities.CheckError(err)
return userData return userData

View File

@ -0,0 +1,28 @@
package wordpress
import (
"encoding/json"
"federated.computer/wp-sync-slowtwitch/utilities"
"net/url"
)
type TagData struct {
Name string `json:"name"`
Id int `json:"id"`
Slug string `json:"slug"`
}
func GetTag(tagName, baseUrl, user, pass string) (TagData, bool) {
endpoint := baseUrl + "tags?search=" + url.QueryEscape(tagName)
body := utilities.GetHttpRequestToWordpress(endpoint, user, pass)
var tagData []TagData
err := json.Unmarshal(body, &tagData)
if err == nil {
for _, data := range tagData {
if data.Name == tagName {
return data, true
}
}
}
return TagData{}, false
}

View File

@ -14,10 +14,8 @@ type UserData struct {
func GetUser(baseUrl, name, user, pass string) (UserData, bool) { func GetUser(baseUrl, name, user, pass string) (UserData, bool) {
endpoint := baseUrl + "users?search=" + url.QueryEscape(name) endpoint := baseUrl + "users?search=" + url.QueryEscape(name)
body := utilities.GetHttpRequestToWordpress(endpoint, user, pass) body := utilities.GetHttpRequestToWordpress(endpoint, user, pass)
var userData []UserData var userData []UserData
err := json.Unmarshal(body, &userData) err := json.Unmarshal(body, &userData)
if err == nil { if err == nil {
for _, userData := range userData { for _, userData := range userData {
if userData.Name == name { if userData.Name == name {
@ -25,6 +23,5 @@ func GetUser(baseUrl, name, user, pass string) (UserData, bool) {
} }
} }
} }
return UserData{}, false return UserData{}, false
} }