2021-10-11 09:18:28 +01:00
|
|
|
package url
|
|
|
|
|
|
|
|
import (
|
2022-01-03 11:21:20 +00:00
|
|
|
"go-micro.dev/v4/api/client"
|
2021-10-11 09:18:28 +01:00
|
|
|
)
|
|
|
|
|
2021-12-30 08:44:34 +00:00
|
|
|
type Url interface {
|
|
|
|
List(*ListRequest) (*ListResponse, error)
|
|
|
|
Proxy(*ProxyRequest) (*ProxyResponse, error)
|
|
|
|
Shorten(*ShortenRequest) (*ShortenResponse, error)
|
|
|
|
}
|
|
|
|
|
2021-10-11 09:18:28 +01:00
|
|
|
func NewUrlService(token string) *UrlService {
|
|
|
|
return &UrlService{
|
|
|
|
client: client.NewClient(&client.Options{
|
|
|
|
Token: token,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type UrlService struct {
|
|
|
|
client *client.Client
|
|
|
|
}
|
|
|
|
|
2021-12-21 13:18:29 +00:00
|
|
|
// List all the shortened URLs
|
2021-10-11 09:18:28 +01:00
|
|
|
func (t *UrlService) List(request *ListRequest) (*ListResponse, error) {
|
2021-12-21 13:18:29 +00:00
|
|
|
|
2021-10-11 09:18:28 +01:00
|
|
|
rsp := &ListResponse{}
|
|
|
|
return rsp, t.client.Call("url", "List", request, rsp)
|
2021-12-21 13:18:29 +00:00
|
|
|
|
2021-10-11 09:18:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Proxy returns the destination URL of a short URL.
|
|
|
|
func (t *UrlService) Proxy(request *ProxyRequest) (*ProxyResponse, error) {
|
2021-12-21 13:18:29 +00:00
|
|
|
|
2021-10-11 09:18:28 +01:00
|
|
|
rsp := &ProxyResponse{}
|
|
|
|
return rsp, t.client.Call("url", "Proxy", request, rsp)
|
2021-12-21 13:18:29 +00:00
|
|
|
|
2021-10-11 09:18:28 +01:00
|
|
|
}
|
|
|
|
|
2021-12-21 13:18:29 +00:00
|
|
|
// Shorten a long URL
|
2021-10-11 09:18:28 +01:00
|
|
|
func (t *UrlService) Shorten(request *ShortenRequest) (*ShortenResponse, error) {
|
2021-12-21 13:18:29 +00:00
|
|
|
|
2021-10-11 09:18:28 +01:00
|
|
|
rsp := &ShortenResponse{}
|
|
|
|
return rsp, t.client.Call("url", "Shorten", request, rsp)
|
2021-12-21 13:18:29 +00:00
|
|
|
|
2021-10-11 09:18:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type ListRequest struct {
|
|
|
|
// filter by short URL, optional
|
2021-12-21 13:18:29 +00:00
|
|
|
ShortUrl string `json:"shortURL"`
|
2021-10-11 09:18:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type ListResponse struct {
|
|
|
|
UrlPairs *URLPair `json:"urlPairs"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProxyRequest struct {
|
|
|
|
// short url ID, without the domain, eg. if your short URL is
|
|
|
|
// `m3o.one/u/someshorturlid` then pass in `someshorturlid`
|
2021-12-21 13:18:29 +00:00
|
|
|
ShortUrl string `json:"shortURL"`
|
2021-10-11 09:18:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type ProxyResponse struct {
|
2021-12-21 13:18:29 +00:00
|
|
|
DestinationUrl string `json:"destinationURL"`
|
2021-10-11 09:18:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type ShortenRequest struct {
|
2021-12-21 13:18:29 +00:00
|
|
|
// the url to shorten
|
|
|
|
DestinationUrl string `json:"destinationURL"`
|
2021-10-11 09:18:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type ShortenResponse struct {
|
2021-12-21 13:18:29 +00:00
|
|
|
// the shortened url
|
|
|
|
ShortUrl string `json:"shortURL"`
|
2021-10-11 09:18:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type URLPair struct {
|
2021-12-21 13:18:29 +00:00
|
|
|
// time of creation
|
|
|
|
Created string `json:"created"`
|
|
|
|
// destination url
|
|
|
|
DestinationUrl string `json:"destinationURL"`
|
|
|
|
// shortened url
|
|
|
|
ShortUrl string `json:"shortURL"`
|
2021-10-11 09:18:28 +01:00
|
|
|
}
|