get user or false
This commit is contained in:
commit
387d4840c4
8
.idea/.gitignore
vendored
Normal file
8
.idea/.gitignore
vendored
Normal file
@ -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
|
9
.idea/WordpressPostMigrator.iml
Normal file
9
.idea/WordpressPostMigrator.iml
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="WEB_MODULE" version="4">
|
||||||
|
<component name="Go" enabled="true" />
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/WordpressPostMigrator.iml" filepath="$PROJECT_DIR$/.idea/WordpressPostMigrator.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
3
go.mod
Normal file
3
go.mod
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
module federated.computer/wp-sync-slowtwitch
|
||||||
|
|
||||||
|
go 1.22.2
|
70
main.go
Normal file
70
main.go
Normal file
@ -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: "<h1>HELLO WORLD</h1>",
|
||||||
|
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
|
||||||
|
}
|
28
services/wordpress/create-post.go
Normal file
28
services/wordpress/create-post.go
Normal file
@ -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
|
||||||
|
}
|
25
services/wordpress/create-user.go
Normal file
25
services/wordpress/create-user.go
Normal file
@ -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
|
||||||
|
}
|
40
services/wordpress/get-posts.go
Normal file
40
services/wordpress/get-posts.go
Normal file
@ -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
|
||||||
|
}
|
33
services/wordpress/get-user.go
Normal file
33
services/wordpress/get-user.go
Normal file
@ -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
|
||||||
|
}
|
11
utilities/error-utilities.go
Normal file
11
utilities/error-utilities.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package utilities
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CheckError(err error) {
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
}
|
||||||
|
}
|
55
utilities/http-utilities.go
Normal file
55
utilities/http-utilities.go
Normal file
@ -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))
|
||||||
|
}
|
||||||
|
}
|
8
utilities/io-utilities.go
Normal file
8
utilities/io-utilities.go
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package utilities
|
||||||
|
|
||||||
|
import "io"
|
||||||
|
|
||||||
|
func CloseBodyAndCheckError(Body io.ReadCloser) {
|
||||||
|
err := Body.Close()
|
||||||
|
CheckError(err)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user