1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-02-04 18:21:53 +02:00

86 lines
1.8 KiB
Go
Raw Normal View History

package url
import (
2022-01-03 11:21:20 +00:00
"go-micro.dev/v4/api/client"
)
2021-12-30 08:44:34 +00:00
type Url interface {
List(*ListRequest) (*ListResponse, error)
Proxy(*ProxyRequest) (*ProxyResponse, error)
Shorten(*ShortenRequest) (*ShortenResponse, error)
}
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
func (t *UrlService) List(request *ListRequest) (*ListResponse, error) {
2021-12-21 13:18:29 +00:00
rsp := &ListResponse{}
return rsp, t.client.Call("url", "List", request, rsp)
2021-12-21 13:18:29 +00: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
rsp := &ProxyResponse{}
return rsp, t.client.Call("url", "Proxy", request, rsp)
2021-12-21 13:18:29 +00:00
}
2021-12-21 13:18:29 +00:00
// Shorten a long URL
func (t *UrlService) Shorten(request *ShortenRequest) (*ShortenResponse, error) {
2021-12-21 13:18:29 +00:00
rsp := &ShortenResponse{}
return rsp, t.client.Call("url", "Shorten", request, rsp)
2021-12-21 13:18:29 +00:00
}
type ListRequest struct {
// filter by short URL, optional
2021-12-21 13:18:29 +00:00
ShortUrl string `json:"shortURL"`
}
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"`
}
type ProxyResponse struct {
2021-12-21 13:18:29 +00:00
DestinationUrl string `json:"destinationURL"`
}
type ShortenRequest struct {
2021-12-21 13:18:29 +00:00
// the url to shorten
DestinationUrl string `json:"destinationURL"`
}
type ShortenResponse struct {
2021-12-21 13:18:29 +00:00
// the shortened url
ShortUrl string `json:"shortURL"`
}
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"`
}