removed dependance on external library

Signed-off-by: Michael <michael.lindman@gmail.com>
This commit is contained in:
Michael 2022-03-22 19:58:24 +00:00
parent 6bca9606f6
commit 6c112b65f9
3 changed files with 24 additions and 5 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
currency
go.sum

2
go.mod
View File

@ -1,5 +1,3 @@
module git.0cd.xyz/michael/currency
go 1.17
require git.0cd.xyz/michael/toolbox v0.0.0-20211024232522-b1a517857097 // indirect

26
main.go
View File

@ -3,18 +3,38 @@ package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"git.0cd.xyz/michael/toolbox/req"
)
const url = "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/"
func request(url string) ([]byte, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return body, nil
}
return nil, fmt.Errorf("request: %s - %d %s", url, resp.StatusCode, http.StatusText(resp.StatusCode))
}
func main() {
from, to, value := ui()
body, _, err := req.Get(url+from+"/"+to+".json", nil)
body, err := request(url + from + "/" + to + ".json")
if err != nil {
log.Fatal(err)
}