diff --git a/forkort.go b/forkort.go new file mode 100644 index 0000000..b8b9db5 --- /dev/null +++ b/forkort.go @@ -0,0 +1,46 @@ +package forkort + +import ( + "io" + "log" + "net/http" + "strings" + "time" +) + +func ShortenLink(url string) (string, error) { + client := &http.Client{ + Timeout: time.Second * 15, + } + resp, err := client.Post("https://forkort.dk/api/shorten", "application/json", strings.NewReader(url)) + if err != nil { + return "", err + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + log.Println("Error while reading the response bytes:", err) + return "", err + } + return string(body), nil +} + +func UnshortenLink(token string) (string, error) { + client := &http.Client{ + Timeout: time.Second * 15, + } + resp, err := client.Get("https://forkort.dk/api/unshorten/" + token) + if err != nil { + log.Println("Error on response.\n[ERROR] -", err) + return "", err + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + log.Println("Error while reading the response bytes:", err) + return "", err + } + return string(body), nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..822de63 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/jesperbakhandskemager/forkort-wrapper-go + +go 1.16