mirror of
https://github.com/jesperh1/forkort-wrapper-go.git
synced 2025-05-16 01:08:13 +01:00
47 lines
964 B
Go
47 lines
964 B
Go
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
|
|
}
|