rewrite request/moved data structs to seperate file

This commit is contained in:
Michael 2019-06-09 19:05:18 +01:00
parent f124f554e5
commit a42d4fd0d9
2 changed files with 99 additions and 77 deletions

99
api.go
View File

@ -9,92 +9,37 @@ import (
"encoding/json"
"time"
"strings"
)
type AutoGen struct {
About struct {
Stats struct {
TopicCount int `json:"topic_count"`
PostCount int `json:"post_count"`
UserCount int `json:"user_count"`
Topics7Days int `json:"topics_7_days"`
Topics30Days int `json:"topics_30_days"`
Posts7Days int `json:"posts_7_days"`
Posts30Days int `json:"posts_30_days"`
Users7Days int `json:"users_7_days"`
Users30Days int `json:"users_30_days"`
ActiveUsers7Days int `json:"active_users_7_days"`
ActiveUsers30Days int `json:"active_users_30_days"`
LikeCount int `json:"like_count"`
Likes7Days int `json:"likes_7_days"`
Likes30Days int `json:"likes_30_days"`
} `json:"stats"`
Description string `json:"description"`
Title string `json:"title"`
Locale string `json:"locale"`
Version string `json:"version"`
HTTPS bool `json:"https"`
Moderators []struct {
ID int `json:"id"`
Username string `json:"username"`
Name string `json:"name"`
AvatarTemplate string `json:"avatar_template"`
Title string `json:"title"`
LastSeenAt time.Time `json:"last_seen_at"`
} `json:"moderators"`
Admins []struct {
ID int `json:"id"`
Username string `json:"username"`
Name string `json:"name"`
AvatarTemplate string `json:"avatar_template"`
Title string `json:"title"`
LastSeenAt time.Time `json:"last_seen_at"`
} `json:"admins"`
} `json:"about"`
}
type TagTopics struct {
TopicList struct {
Topics []struct {
Slug string `json:"slug"`
} `json:"topics"`
} `json:"topic_list"`
}
type Topic struct {
PostStream struct {
Posts []struct {
Cooked string `json:"cooked"`
UserTitle string `json:"user_title"`
} `json:"posts"`
} `json:"post_stream"`
Tags []string `json:"tags"`
ID int `json:"id"`
Title string `json:"title"`
CreatedAt time.Time `json:"created_at"`
Slug string `json:"slug"`
Details struct {
CreatedBy struct {
ID int `json:"id"`
Username string `json:"username"`
Name string `json:"name"`
AvatarTemplate string `json:"avatar_template"`
} `json:"created_by"`
} `json:"details"`
}
func url(path string) string {
return string("https://forum.0cd.xyz/" + path)
}
func Request(path string) (b []byte) {
resp, err := http.Get(url(path))
req, err := http.NewRequest("GET", url(path), nil)
if err != nil {
log.Fatal("Error reading request. ", err)
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Api-Key", "57dc92f574f7f400e1d12670940c19930a1f85b78485ca25ac96d96590dc7f99")
req.Header.Set("Api-Username", "Michael")
client := &http.Client{Timeout: time.Second * 10}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
log.Fatal("Error reading request. ", err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal("Error reading request. ", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
//fmt.Println("response Status:", resp.Status)
//fmt.Println("response Headers:", resp.Header)
//fmt.Printf("%s %s %d\n", resp.Request.Method, url("about.json"), resp.StatusCode)
if resp.StatusCode == 200 {
@ -105,7 +50,7 @@ func Request(path string) (b []byte) {
func About() (*AutoGen) {
var about AutoGen
json.Unmarshal(Request("about.json"), &about)
json.Unmarshal(Request("about"), &about)
for i := 0; i < len(about.About.Admins); i++ {
about.About.Admins[i].AvatarTemplate = strings.ReplaceAll(about.About.Admins[i].AvatarTemplate, "{size}", "120")
}

77
data.go Normal file
View File

@ -0,0 +1,77 @@
package request
import (
"time"
)
type AutoGen struct {
About struct {
Stats struct {
TopicCount int `json:"topic_count"`
PostCount int `json:"post_count"`
UserCount int `json:"user_count"`
Topics7Days int `json:"topics_7_days"`
Topics30Days int `json:"topics_30_days"`
Posts7Days int `json:"posts_7_days"`
Posts30Days int `json:"posts_30_days"`
Users7Days int `json:"users_7_days"`
Users30Days int `json:"users_30_days"`
ActiveUsers7Days int `json:"active_users_7_days"`
ActiveUsers30Days int `json:"active_users_30_days"`
LikeCount int `json:"like_count"`
Likes7Days int `json:"likes_7_days"`
Likes30Days int `json:"likes_30_days"`
} `json:"stats"`
Description string `json:"description"`
Title string `json:"title"`
Locale string `json:"locale"`
Version string `json:"version"`
HTTPS bool `json:"https"`
Moderators []struct {
ID int `json:"id"`
Username string `json:"username"`
Name string `json:"name"`
AvatarTemplate string `json:"avatar_template"`
Title string `json:"title"`
LastSeenAt time.Time `json:"last_seen_at"`
} `json:"moderators"`
Admins []struct {
ID int `json:"id"`
Username string `json:"username"`
Name string `json:"name"`
AvatarTemplate string `json:"avatar_template"`
Title string `json:"title"`
LastSeenAt time.Time `json:"last_seen_at"`
} `json:"admins"`
} `json:"about"`
}
type TagTopics struct {
TopicList struct {
Topics []struct {
Slug string `json:"slug"`
} `json:"topics"`
} `json:"topic_list"`
}
type Topic struct {
PostStream struct {
Posts []struct {
Cooked string `json:"cooked"`
UserTitle string `json:"user_title"`
} `json:"posts"`
} `json:"post_stream"`
Tags []string `json:"tags"`
ID int `json:"id"`
Title string `json:"title"`
CreatedAt time.Time `json:"created_at"`
Slug string `json:"slug"`
Details struct {
CreatedBy struct {
ID int `json:"id"`
Username string `json:"username"`
Name string `json:"name"`
AvatarTemplate string `json:"avatar_template"`
} `json:"created_by"`
} `json:"details"`
}