From 847f11f5b20626e1898484b2a14f7ac1e73e9987 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 12 Aug 2019 04:23:17 +0100 Subject: [PATCH] moved async api to new file (WIP) --- api.go | 81 +-------------------------------------------- async_api.go | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++ data.go | 1 + 3 files changed, 95 insertions(+), 80 deletions(-) create mode 100644 async_api.go diff --git a/api.go b/api.go index 922299d..5a79088 100755 --- a/api.go +++ b/api.go @@ -17,41 +17,6 @@ func url(path string) string { return string("https://forum.0cd.xyz/" + path) } -// HTTPResp Async HTTP Request -type HTTPResp struct { - ID int - Resp []byte - Err logger.HTTPError -} - -// AsyncGet sends Async GET requests -func AsyncGet(urls map[int]string) ([]*HTTPResp, *logger.HTTPError) { - ch := make(chan *HTTPResp) - response := []*HTTPResp{} - - for id, url := range urls { - go func(i int, u string) { - resp, err := Request("t/" + u) - err = logger.HTTPErr(http.StatusOK) - ch <- &HTTPResp{i, resp, *err} - }(id, url) - } -loop: - for { - select { - case r := <-ch: - response = append(response, r) - if len(response) == len(urls) { - break loop - } - case <-time.After(120 * time.Millisecond): - fmt.Printf(".") - //return nil, logger.HTTPErr(http.StatusRequestTimeout) - } - } - return response, nil -} - // Request sends GET to path func Request(path string) ([]byte, *logger.HTTPError) { req, err := http.NewRequest("GET", url(path), nil) @@ -138,51 +103,7 @@ func GetTopics(path string) (topics TagTopics, err *logger.HTTPError) { return } -// GetTopics2 gets topic list from tag -func GetTopics2(path string) (m map[int]string) { - resp, err := Request("tags/" + path) - if err != nil { - return - } - var topics TagTopics - json.Unmarshal(resp, &topics) - m = make(map[int]string) - for _, topics := range topics.TopicList.Topics { - m[topics.ID] = topics.Slug - } - return -} - -// AsyncTopics request for topics using async -func AsyncTopics(path string) map[string]interface{} { - var topic TopicsList - resp, err := AsyncGet(GetTopics2(path)) - if err != nil { - m := structs.Map(err) - return m - } - for _, topics := range resp { - var t Topic - json.Unmarshal(topics.Resp, &t) - for i := 0; i < len(t.PostStream.Posts); i++ { - if t.PostStream.Posts[i].PostNumber != 1 { - t.PostStream.Posts[i].Cooked = "" - } - } - t.Details.CreatedBy.AvatarTemplate = strings.ReplaceAll(t.Details.CreatedBy.AvatarTemplate, "{size}", "120") - s := strings.SplitAfter(t.PostStream.Posts[0].Cooked, "

") - t.PostStream.Posts[0].Cooked = s[0] - r := strings.ReplaceAll(t.PostStream.Posts[0].Cooked, "href=\"/u/", "href=\""+url("")+"u/") - t.PostStream.Posts[0].Cooked = r - ts, _ := time.Parse("2006-01-02T15:04:05Z07:00", t.CreatedAt) - t.CreatedAt = ts.Format("January 2, 2006") - topic.Topic = append(topic.Topic, t) - } - m := structs.Map(topic) - return m -} - -// Topics n/a +// Topics requests topics func Topics(path string) map[string]interface{} { var topic TopicsList resp, err := GetTopics(path) diff --git a/async_api.go b/async_api.go new file mode 100644 index 0000000..2f7e19f --- /dev/null +++ b/async_api.go @@ -0,0 +1,93 @@ +package request + +// Async api requests (Work in Progress) + +import ( + "encoding/json" + "fmt" + "net/http" + "strings" + "time" + + "0cd.xyz-go/logger" + "github.com/fatih/structs" +) + +// HTTPResp Async HTTP Request +type HTTPResp struct { + ID int + Resp []byte + Err logger.HTTPError +} + +// AsyncGet sends Async GET requests +func AsyncGet(urls map[int]string) ([]*HTTPResp, *logger.HTTPError) { + ch := make(chan *HTTPResp) + response := []*HTTPResp{} + + for id, url := range urls { + go func(i int, u string) { + resp, err := Request("t/" + u) + err = logger.HTTPErr(http.StatusOK) + ch <- &HTTPResp{i, resp, *err} + }(id, url) + } +loop: + for { + select { + case r := <-ch: + response = append(response, r) + if len(response) == len(urls) { + break loop + } + case <-time.After(120 * time.Millisecond): + fmt.Printf(".") + //return nil, logger.HTTPErr(http.StatusRequestTimeout) + } + } + return response, nil +} + +// GetAsyncTopics gets topic list from tag +func GetAsyncTopics(path string) (m map[int]string) { + resp, err := Request("tags/" + path) + if err != nil { + return + } + var topics TagTopics + json.Unmarshal(resp, &topics) + m = make(map[int]string) + for _, topics := range topics.TopicList.Topics { + m[topics.ID] = topics.Slug + } + return +} + +// AsyncTopics request for topics using async +func AsyncTopics(path string) map[string]interface{} { + var topic TopicsList + resp, err := AsyncGet(GetAsyncTopics(path)) + if err != nil { + m := structs.Map(err) + return m + } + for _, topics := range resp { + var t Topic + json.Unmarshal(topics.Resp, &t) + for i := 0; i < len(t.PostStream.Posts); i++ { + if t.PostStream.Posts[i].PostNumber != 1 { + t.PostStream.Posts[i].Cooked = "" + } + } + t.Details.CreatedBy.AvatarTemplate = strings.ReplaceAll(t.Details.CreatedBy.AvatarTemplate, "{size}", "120") + s := strings.SplitAfter(t.PostStream.Posts[0].Cooked, "

") + t.PostStream.Posts[0].Cooked = s[0] + r := strings.ReplaceAll(t.PostStream.Posts[0].Cooked, "href=\"/u/", "href=\""+url("")+"u/") + t.PostStream.Posts[0].Cooked = r + ts, _ := time.Parse("2006-01-02T15:04:05Z07:00", t.CreatedAt) + t.CreatedAt = ts.Format("January 2, 2006") + topic.Topic = append(topic.Topic, t) + } + m := structs.Map(topic) + return m +} diff --git a/data.go b/data.go index 07fc302..717e92d 100644 --- a/data.go +++ b/data.go @@ -4,6 +4,7 @@ import ( "time" ) +// Options struct for application options type Options struct { Title string `json:"title"` Description string `json:"description"`