new: feat: add lambdas

This commit is contained in:
2022-04-21 15:14:49 -03:00
commit 32dc9ed3e7
7 changed files with 165 additions and 0 deletions

View File

@@ -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)
}

View File

@@ -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)
}