From 32dc9ed3e78a0b10c9e6adc857d54a5fbba90fc7 Mon Sep 17 00:00:00 2001 From: Felipe Marinho Date: Thu, 21 Apr 2022 15:14:49 -0300 Subject: [PATCH] new: feat: add lambdas --- .gitignore | 1 + api/index.go | 16 +++++++ api/statusinvest/companies.go | 39 ++++++++++++++++ api/statusinvest/indicators.go | 85 ++++++++++++++++++++++++++++++++++ go.mod | 3 ++ main.go | 18 +++++++ vercel.json | 3 ++ 7 files changed, 165 insertions(+) create mode 100644 .gitignore create mode 100644 api/index.go create mode 100644 api/statusinvest/companies.go create mode 100644 api/statusinvest/indicators.go create mode 100644 go.mod create mode 100644 main.go create mode 100644 vercel.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e985853 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vercel diff --git a/api/index.go b/api/index.go new file mode 100644 index 0000000..406f677 --- /dev/null +++ b/api/index.go @@ -0,0 +1,16 @@ +package handler + +import ( + "fmt" + "net/http" + "time" +) + +func HandlerIndex(w http.ResponseWriter, r *http.Request) { + currentTime := time.Now().Format(time.RFC850) + fmt.Fprintf(w, currentTime) + w.Header().Set("Content-Type", "text/html") + fmt.Fprintf(w, ` + Github OAuth2 => https://github.com/xjh22222228/github-oauth2 + `) +} diff --git a/api/statusinvest/companies.go b/api/statusinvest/companies.go new file mode 100644 index 0000000..1ac9a9f --- /dev/null +++ b/api/statusinvest/companies.go @@ -0,0 +1,39 @@ +package statusinvest + +import ( + "io/ioutil" + "net/http" +) + +func HandlerListCompanies(w http.ResponseWriter, r *http.Request) { + client := http.Client{} + req, err := http.NewRequest("GET", "https://statusinvest.com.br/acao/companiesnavigation?page=1&size=500", nil) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + + req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36") + req.Header.Set("Accept", "application/json, text/plain, */*") + + resp, err := client.Do(req) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + + defer resp.Body.Close() + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(resp.StatusCode) + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + + w.Write(body) +} diff --git a/api/statusinvest/indicators.go b/api/statusinvest/indicators.go new file mode 100644 index 0000000..891ffd2 --- /dev/null +++ b/api/statusinvest/indicators.go @@ -0,0 +1,85 @@ +package statusinvest + +import ( + "encoding/json" + "io/ioutil" + "net/http" + "net/url" + "strings" +) + +type Response struct { + Success bool `json:"success"` + Data map[string][]map[string]interface{} `json:"data"` +} + +type ParsedResponse map[string]interface{} + +func HandlerIndicators(w http.ResponseWriter, r *http.Request) { + if r.Method != "GET" { + w.WriteHeader(http.StatusMethodNotAllowed) + w.Write([]byte("Method not allowed")) + return + } + time := r.URL.Query().Get("time") + if time == "" { + time = "7" + } + ticker := r.URL.Query().Get("ticker") + if ticker == "" { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("Ticker is required")) + return + } + + data := url.Values{ + "codes[]": strings.Split(ticker, ","), + "time": {time}, + "byQuarter": {"false"}, + "futureData": {"false"}, + } + + resp, err := http.PostForm("https://statusinvest.com.br/acao/indicatorhistoricallist", data) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + + defer resp.Body.Close() + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(resp.StatusCode) + + out, err := ioutil.ReadAll(resp.Body) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + + var indicators Response + err = json.Unmarshal(out, &indicators) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + + parsedResponse := ParsedResponse{} + + d := indicators.Data[ticker] + + for _, v := range d { + v := v + parsedResponse[v["key"].(string)] = v + } + + out, err = json.Marshal(parsedResponse) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + + w.Write(out) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d6a9e50 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/felipemarinho97/vercel-lambdas + +go 1.17 diff --git a/main.go b/main.go new file mode 100644 index 0000000..1049521 --- /dev/null +++ b/main.go @@ -0,0 +1,18 @@ +package main + +import ( + "net/http" + + handler "github.com/felipemarinho97/vercel-lambdas/api" + "github.com/felipemarinho97/vercel-lambdas/api/statusinvest" +) + +func main() { + http.HandleFunc("/", handler.HandlerIndex) + http.HandleFunc("/statusinvest/companies", statusinvest.HandlerListCompanies) + + err := http.ListenAndServe(":7006", nil) + if err != nil { + panic(err) + } +} diff --git a/vercel.json b/vercel.json new file mode 100644 index 0000000..0e0dcd2 --- /dev/null +++ b/vercel.json @@ -0,0 +1,3 @@ +{ + +} \ No newline at end of file