find user by username

This commit is contained in:
Ross Trottier 2024-05-07 18:52:47 -06:00
parent f264b68565
commit 880d4985f4
6 changed files with 47 additions and 32 deletions

19
app-cache.go Normal file
View File

@ -0,0 +1,19 @@
package main
import "federated.computer/wp-sync-slowtwitch/services/wordpress"
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.Name] = user
}

29
main.go
View File

@ -6,8 +6,8 @@ import (
)
const baseUrl = "https://go-api-playground.local/wp-json/wp/v2/"
const user = "admin"
const password = "S34E keY1 A1uX 6ncs Rx4T f21W"
const wordpressKey = "admin"
const wordpressSecret = "S34E keY1 A1uX 6ncs Rx4T f21W"
var appCache AppCache
@ -23,24 +23,11 @@ func main() {
//Upload Photo + add to cache
//Use cached data to correctly build post form submissions
posts := wordpress.GetPosts(baseUrl, user, password)
for i, postData := range posts {
fmt.Println("Post Number:", i+1, postData.Title.Rendered)
user, ok := wordpress.GetUser(baseUrl, "Bongo Bangidsn Hobs", wordpressKey, wordpressSecret)
if ok {
fmt.Println("User found:", user.Name)
appCache.SetUser(user)
} else {
fmt.Println("User not found")
}
}
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
}

View File

@ -17,10 +17,15 @@ type CreatePost struct {
Slug string `json:"slug"`
}
func (parameters *CreatePost) Execute(baseUrl, user, pass string) PostData {
type CreatePostResponse struct {
Id int `json:"id"`
Link string `json:"link"`
}
func (parameters *CreatePost) Execute(baseUrl, user, pass string) CreatePostResponse {
endpoint := baseUrl + "posts"
body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters)
var post PostData
var post CreatePostResponse
err := json.Unmarshal(body, &post)
utilities.CheckError(err)

View File

@ -15,6 +15,14 @@ type CreateUser struct {
Password string `json:"password"`
}
type CreateUserResponse struct {
Id int `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
}
func (parameters *CreateUser) Execute(baseUrl, user, pass string) UserData {
endpoint := baseUrl + "users"
body := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters)

View File

@ -24,7 +24,6 @@ type PostData struct {
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)
utilities.CheckError(err)

View File

@ -7,15 +7,12 @@ import (
)
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"`
Id int `json:"id"`
Name string `json:"name"`
}
func GetUser(baseUrl, username, user, pass string) (UserData, bool) {
endpoint := baseUrl + "/users?search=" + url.QueryEscape(username)
func GetUser(baseUrl, name, user, pass string) (UserData, bool) {
endpoint := baseUrl + "users?search=" + url.QueryEscape(name)
body := utilities.GetHttpRequestToWordpress(endpoint, user, pass)
var userData []UserData
@ -23,7 +20,7 @@ func GetUser(baseUrl, username, user, pass string) (UserData, bool) {
if err == nil {
for _, userData := range userData {
if userData.Username == username {
if userData.Name == name {
return userData, true
}
}