This repository was archived by the owner on Apr 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_embed.go
60 lines (50 loc) · 1.41 KB
/
fetch_embed.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package oembed
import (
"strconv"
json "github.com/pquerna/ffjson/ffjson"
http "github.com/valyala/fasthttp"
template "github.com/valyala/fasttemplate"
)
// Params represent a optional parameters for Extract method.
type Params struct {
MaxWidth int
MaxHeight int
}
func fetchEmbed(url string, provider *Provider, params *Params) (*OEmbed, error) {
resourceURL := provider.Endpoints[0].URL
resourceURL = template.ExecuteString(resourceURL, "{", "}", map[string]interface{}{"format": "json"})
link := http.AcquireURI()
defer http.ReleaseURI(link)
link.Update(resourceURL)
qa := link.QueryArgs()
qa.Add("format", "json")
qa.Add("url", url)
if params != nil && params.MaxWidth != 0 {
qa.Add("maxwidth", strconv.Itoa(params.MaxWidth))
}
if params != nil && params.MaxHeight != 0 {
qa.Add("maxheight", strconv.Itoa(params.MaxHeight))
}
link.SetQueryStringBytes(qa.QueryString())
req := http.AcquireRequest()
defer http.ReleaseRequest(req)
req.SetRequestURIBytes(link.FullURI())
resp := http.AcquireResponse()
defer http.ReleaseResponse(resp)
if err := http.Do(req, resp); err != nil {
return nil, Error{
Message: err.Error(),
URL: url,
}
}
var oEmbed OEmbed
if err := json.UnmarshalFast(resp.Body(), &oEmbed); err != nil {
return nil, Error{
Message: err.Error(),
URL: url,
}
}
oEmbed.ProviderName = provider.Name
oEmbed.ProviderURL = provider.URL
return &oEmbed, nil
}