♻️ Refactored old code
This commit is contained in:
parent
3143d8ebb1
commit
d29221f807
14
content.go
14
content.go
|
@ -15,21 +15,15 @@ func ContentFormat(data interface{}) (n []Node, err error) {
|
||||||
switch src := data.(type) {
|
switch src := data.(type) {
|
||||||
case string:
|
case string:
|
||||||
dst, err = html.Parse(strings.NewReader(src))
|
dst, err = html.Parse(strings.NewReader(src))
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
case []byte:
|
case []byte:
|
||||||
dst, err = html.Parse(bytes.NewReader(src))
|
dst, err = html.Parse(bytes.NewReader(src))
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
case io.Reader:
|
case io.Reader:
|
||||||
dst, err = html.Parse(src)
|
dst, err = html.Parse(src)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
return nil, ErrInvalidDataType
|
err = ErrInvalidDataType
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
n = append(n, domToNode(dst.FirstChild))
|
n = append(n, domToNode(dst.FirstChild))
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
package telegraph
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestContentFormat(t *testing.T) {
|
||||||
|
t.Run("invalid", func(t *testing.T) {
|
||||||
|
_, err := ContentFormat(42)
|
||||||
|
assert.EqualError(t, ErrInvalidDataType, err.Error())
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("valid", func(t *testing.T) {
|
||||||
|
t.Run("string", func(t *testing.T) {
|
||||||
|
validContentDOM, err := ContentFormat(`<p>Hello, World!</p>`)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotEmpty(t, validContentDOM)
|
||||||
|
})
|
||||||
|
t.Run("bytes", func(t *testing.T) {
|
||||||
|
validContentDOM, err := ContentFormat([]byte(`<p>Hello, World!</p>`))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotEmpty(t, validContentDOM)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
|
@ -1,7 +1,6 @@
|
||||||
package telegraph
|
package telegraph
|
||||||
|
|
||||||
import (
|
import (
|
||||||
json "github.com/pquerna/ffjson/ffjson"
|
|
||||||
http "github.com/valyala/fasthttp"
|
http "github.com/valyala/fasthttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -10,24 +9,19 @@ import (
|
||||||
// keep individual author names and profile links for each of their channels. On
|
// keep individual author names and profile links for each of their channels. On
|
||||||
// success, returns an Account object with the regular fields and an additional
|
// success, returns an Account object with the regular fields and an additional
|
||||||
// access_token field.
|
// access_token field.
|
||||||
func CreateAccount(account *Account) (r *Account, err error) {
|
func CreateAccount(account Account) (*Account, error) {
|
||||||
if account == nil {
|
|
||||||
return nil, ErrNoInputData
|
|
||||||
}
|
|
||||||
|
|
||||||
args := http.AcquireArgs()
|
args := http.AcquireArgs()
|
||||||
defer http.ReleaseArgs(args)
|
defer http.ReleaseArgs(args)
|
||||||
args.Add("short_name", account.ShortName) // required
|
args.Add("short_name", account.ShortName) // required
|
||||||
args.Add("author_name", account.AuthorName)
|
args.Add("author_name", account.AuthorName)
|
||||||
args.Add("author_url", account.AuthorURL)
|
args.Add("author_url", account.AuthorURL)
|
||||||
|
|
||||||
dst := new(Response)
|
data, err := makeRequest("createAccount", args)
|
||||||
dst, err = makeRequest("createAccount", args)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
r = new(Account)
|
var result Account
|
||||||
err = json.Unmarshal(*dst.Result, r)
|
err = parser.Unmarshal(data, &result)
|
||||||
return
|
return &result, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
package telegraph
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCreateAccount(t *testing.T) {
|
||||||
|
t.Run("invalid", func(t *testing.T) {
|
||||||
|
t.Run("nil", func(t *testing.T) {
|
||||||
|
_, err := CreateAccount(Account{})
|
||||||
|
assert.Error(t, err)
|
||||||
|
})
|
||||||
|
t.Run("without shortname", func(t *testing.T) {
|
||||||
|
_, err := CreateAccount(Account{
|
||||||
|
ShortName: "",
|
||||||
|
AuthorName: "Anonymous",
|
||||||
|
})
|
||||||
|
assert.Error(t, err)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
t.Run("valid", func(t *testing.T) {
|
||||||
|
account, err := CreateAccount(Account{
|
||||||
|
ShortName: "Sandbox",
|
||||||
|
AuthorName: "Anonymous",
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, account)
|
||||||
|
})
|
||||||
|
}
|
|
@ -3,38 +3,31 @@ package telegraph
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
json "github.com/pquerna/ffjson/ffjson"
|
|
||||||
http "github.com/valyala/fasthttp"
|
http "github.com/valyala/fasthttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CreatePage create a new Telegraph page. On success, returns a Page object.
|
// CreatePage create a new Telegraph page. On success, returns a Page object.
|
||||||
func (a *Account) CreatePage(page *Page, returnContent bool) (r *Page, err error) {
|
func (a *Account) CreatePage(page Page, returnContent bool) (*Page, error) {
|
||||||
if page == nil {
|
src, err := parser.Marshal(page.Content)
|
||||||
return nil, ErrNoInputData
|
|
||||||
}
|
|
||||||
|
|
||||||
var src []byte
|
|
||||||
src, err = json.Marshal(page.Content)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
args := http.AcquireArgs()
|
args := http.AcquireArgs()
|
||||||
defer http.ReleaseArgs(args)
|
defer http.ReleaseArgs(args)
|
||||||
args.Add("access_token", a.AccessToken) // required
|
args.Add("access_token", a.AccessToken) // required
|
||||||
args.Add("title", page.Title) // required
|
args.Add("title", page.Title) // required
|
||||||
args.Add("author_name", a.AuthorName)
|
args.Add("author_name", page.AuthorName)
|
||||||
args.Add("author_url", a.AuthorURL)
|
args.Add("author_url", page.AuthorURL)
|
||||||
args.Add("content", string(src))
|
args.AddBytesV("content", src)
|
||||||
args.Add("return_content", strconv.FormatBool(returnContent))
|
args.Add("return_content", strconv.FormatBool(returnContent))
|
||||||
|
|
||||||
dst := new(Response)
|
data, err := makeRequest("createPage", args)
|
||||||
dst, err = makeRequest("createPage", args)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
r = new(Page)
|
var result Page
|
||||||
err = json.Unmarshal(*dst.Result, r)
|
err = parser.Unmarshal(data, &result)
|
||||||
return
|
return &result, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
package telegraph
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCreatePage(t *testing.T) {
|
||||||
|
content, err := ContentFormat(`<p>Hello, world!</p>`)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
t.Run("invalid", func(t *testing.T) {
|
||||||
|
var a Account
|
||||||
|
_, err := a.CreatePage(Page{
|
||||||
|
Title: "Sample Page",
|
||||||
|
AuthorName: "Anonymous",
|
||||||
|
Content: content,
|
||||||
|
}, true)
|
||||||
|
assert.Error(t, err)
|
||||||
|
})
|
||||||
|
t.Run("valid", func(t *testing.T) {
|
||||||
|
a := Account{
|
||||||
|
AccessToken: "b968da509bb76866c35425099bc0989a5ec3b32997d55286c657e6994bbb",
|
||||||
|
ShortName: "Sandbox",
|
||||||
|
AuthorName: "Anonymous",
|
||||||
|
}
|
||||||
|
|
||||||
|
page, err := a.CreatePage(Page{
|
||||||
|
Title: "Sample Page",
|
||||||
|
AuthorName: "Anonymous",
|
||||||
|
Content: content,
|
||||||
|
}, true)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
assert.NotEmpty(t, page.URL)
|
||||||
|
})
|
||||||
|
}
|
|
@ -1,18 +1,13 @@
|
||||||
package telegraph
|
package telegraph
|
||||||
|
|
||||||
import (
|
import (
|
||||||
json "github.com/pquerna/ffjson/ffjson"
|
|
||||||
http "github.com/valyala/fasthttp"
|
http "github.com/valyala/fasthttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
// EditAccountInfo update information about a Telegraph account. Pass only the
|
// EditAccountInfo update information about a Telegraph account. Pass only the
|
||||||
// parameters that you want to edit. On success, returns an Account object with
|
// parameters that you want to edit. On success, returns an Account object with
|
||||||
// the default fields.
|
// the default fields.
|
||||||
func (a *Account) EditAccountInfo(update *Account) (r *Account, err error) {
|
func (a *Account) EditAccountInfo(update Account) (*Account, error) {
|
||||||
if update == nil {
|
|
||||||
return nil, ErrNoInputData
|
|
||||||
}
|
|
||||||
|
|
||||||
args := http.AcquireArgs()
|
args := http.AcquireArgs()
|
||||||
defer http.ReleaseArgs(args)
|
defer http.ReleaseArgs(args)
|
||||||
args.Add("access_token", a.AccessToken) // required
|
args.Add("access_token", a.AccessToken) // required
|
||||||
|
@ -20,13 +15,12 @@ func (a *Account) EditAccountInfo(update *Account) (r *Account, err error) {
|
||||||
args.Add("author_name", update.AuthorName)
|
args.Add("author_name", update.AuthorName)
|
||||||
args.Add("author_url", update.AuthorURL)
|
args.Add("author_url", update.AuthorURL)
|
||||||
|
|
||||||
dst := new(Response)
|
data, err := makeRequest("editAccountInfo", args)
|
||||||
dst, err = makeRequest("editAccountInfo", args)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
r = new(Account)
|
var result Account
|
||||||
err = json.Unmarshal(*dst.Result, r)
|
err = parser.Unmarshal(data, &result)
|
||||||
return
|
return &result, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
package telegraph
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEditAccountInfo(t *testing.T) {
|
||||||
|
t.Run("invalid", func(t *testing.T) {
|
||||||
|
var a Account
|
||||||
|
_, err := a.EditAccountInfo(Account{})
|
||||||
|
assert.Error(t, err)
|
||||||
|
})
|
||||||
|
t.Run("valid", func(t *testing.T) {
|
||||||
|
a := Account{
|
||||||
|
AccessToken: "b968da509bb76866c35425099bc0989a5ec3b32997d55286c657e6994bbb",
|
||||||
|
ShortName: "Sandbox",
|
||||||
|
AuthorName: "Anonymous",
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := a.EditAccountInfo(Account{
|
||||||
|
ShortName: "Sandbox",
|
||||||
|
AuthorName: "Anonymous",
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
})
|
||||||
|
}
|
27
edit_page.go
27
edit_page.go
|
@ -4,20 +4,14 @@ import (
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
json "github.com/pquerna/ffjson/ffjson"
|
|
||||||
http "github.com/valyala/fasthttp"
|
http "github.com/valyala/fasthttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
// EditPage edit an existing Telegraph page. On success, returns a Page object.
|
// EditPage edit an existing Telegraph page. On success, returns a Page object.
|
||||||
func (a *Account) EditPage(update *Page, returnContent bool) (r *Page, err error) {
|
func (a *Account) EditPage(update Page, returnContent bool) (*Page, error) {
|
||||||
if update == nil {
|
src, err := parser.Marshal(update.Content)
|
||||||
return nil, ErrNoInputData
|
|
||||||
}
|
|
||||||
|
|
||||||
var src []byte
|
|
||||||
src, err = json.Marshal(update.Content)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
args := http.AcquireArgs()
|
args := http.AcquireArgs()
|
||||||
|
@ -25,18 +19,17 @@ func (a *Account) EditPage(update *Page, returnContent bool) (r *Page, err error
|
||||||
args.Add("access_token", a.AccessToken) // required
|
args.Add("access_token", a.AccessToken) // required
|
||||||
args.Add("path", update.Path) // required
|
args.Add("path", update.Path) // required
|
||||||
args.Add("title", update.Title) // required
|
args.Add("title", update.Title) // required
|
||||||
args.Add("content", string(src)) // required
|
args.AddBytesV("content", src) // required
|
||||||
args.Add("author_name", a.AuthorName)
|
args.Add("author_name", update.AuthorName)
|
||||||
args.Add("author_url", a.AuthorURL)
|
args.Add("author_url", update.AuthorURL)
|
||||||
args.Add("return_content", strconv.FormatBool(returnContent))
|
args.Add("return_content", strconv.FormatBool(returnContent))
|
||||||
|
|
||||||
dst := new(Response)
|
data, err := makeRequest(path.Join("editPage", update.Path), args)
|
||||||
dst, err = makeRequest(path.Join("editPage", update.Path), args)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
r = new(Page)
|
var result Page
|
||||||
err = json.Unmarshal(*dst.Result, r)
|
err = parser.Unmarshal(data, &result)
|
||||||
return
|
return &result, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
package telegraph
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEditPage(t *testing.T) {
|
||||||
|
content, err := ContentFormat("<p>Hello, world!</p>")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
t.Run("invalid", func(t *testing.T) {
|
||||||
|
var a Account
|
||||||
|
_, err := a.EditPage(Page{
|
||||||
|
Title: "Sample Page",
|
||||||
|
AuthorName: "Anonymous",
|
||||||
|
Content: content,
|
||||||
|
}, true)
|
||||||
|
assert.Error(t, err)
|
||||||
|
})
|
||||||
|
t.Run("valid", func(t *testing.T) {
|
||||||
|
a := Account{
|
||||||
|
AccessToken: "b968da509bb76866c35425099bc0989a5ec3b32997d55286c657e6994bbb",
|
||||||
|
ShortName: "Sandbox",
|
||||||
|
AuthorName: "Anonymous",
|
||||||
|
}
|
||||||
|
|
||||||
|
page, err := a.EditPage(Page{
|
||||||
|
Path: "Sample-Page-12-15",
|
||||||
|
Title: "Sample Page",
|
||||||
|
AuthorName: "Anonymous",
|
||||||
|
Content: content,
|
||||||
|
}, true)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotEmpty(t, page.Content)
|
||||||
|
})
|
||||||
|
}
|
|
@ -2,7 +2,6 @@ package telegraph_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"time"
|
|
||||||
|
|
||||||
"gitlab.com/toby3d/telegraph"
|
"gitlab.com/toby3d/telegraph"
|
||||||
)
|
)
|
||||||
|
@ -23,9 +22,9 @@ const data = `
|
||||||
`
|
`
|
||||||
|
|
||||||
var (
|
var (
|
||||||
account = new(telegraph.Account)
|
account *telegraph.Account //nolint:gochecknoglobals
|
||||||
page = new(telegraph.Page)
|
page *telegraph.Page //nolint:gochecknoglobals
|
||||||
content []telegraph.Node
|
content []telegraph.Node //nolint:gochecknoglobals
|
||||||
)
|
)
|
||||||
|
|
||||||
func errCheck(err error) {
|
func errCheck(err error) {
|
||||||
|
@ -37,7 +36,7 @@ func errCheck(err error) {
|
||||||
func Example_fastStart() {
|
func Example_fastStart() {
|
||||||
var err error
|
var err error
|
||||||
// Create new Telegraph account.
|
// Create new Telegraph account.
|
||||||
requisites := &telegraph.Account{
|
requisites := telegraph.Account{
|
||||||
ShortName: "toby3d", // required
|
ShortName: "toby3d", // required
|
||||||
|
|
||||||
// Author name/link can be epmty. So secure. Much anonymously. Wow.
|
// Author name/link can be epmty. So secure. Much anonymously. Wow.
|
||||||
|
@ -58,7 +57,7 @@ func Example_fastStart() {
|
||||||
// Boom!.. And your text will be understandable for Telegraph. MAGIC.
|
// Boom!.. And your text will be understandable for Telegraph. MAGIC.
|
||||||
|
|
||||||
// Create new Telegraph page
|
// Create new Telegraph page
|
||||||
pageData := &telegraph.Page{
|
pageData := telegraph.Page{
|
||||||
Title: "My super-awesome page", // required
|
Title: "My super-awesome page", // required
|
||||||
Content: content, // required
|
Content: content, // required
|
||||||
|
|
||||||
|
@ -75,7 +74,7 @@ func Example_fastStart() {
|
||||||
|
|
||||||
func ExampleCreateAccount() {
|
func ExampleCreateAccount() {
|
||||||
var err error
|
var err error
|
||||||
account, err = telegraph.CreateAccount(&telegraph.Account{
|
account, err = telegraph.CreateAccount(telegraph.Account{
|
||||||
ShortName: "Sandbox",
|
ShortName: "Sandbox",
|
||||||
AuthorName: "Anonymous",
|
AuthorName: "Anonymous",
|
||||||
})
|
})
|
||||||
|
@ -89,7 +88,7 @@ func ExampleCreateAccount() {
|
||||||
|
|
||||||
func ExampleAccount_EditAccountInfo() {
|
func ExampleAccount_EditAccountInfo() {
|
||||||
var err error
|
var err error
|
||||||
account, err = account.EditAccountInfo(&telegraph.Account{
|
account, err = account.EditAccountInfo(telegraph.Account{
|
||||||
ShortName: "Sandbox",
|
ShortName: "Sandbox",
|
||||||
AuthorName: "Anonymous",
|
AuthorName: "Anonymous",
|
||||||
})
|
})
|
||||||
|
@ -122,7 +121,7 @@ func ExampleAccount_RevokeAccessToken() {
|
||||||
|
|
||||||
func ExampleAccount_CreatePage() {
|
func ExampleAccount_CreatePage() {
|
||||||
var err error
|
var err error
|
||||||
page, err = account.CreatePage(&telegraph.Page{
|
page, err = account.CreatePage(telegraph.Page{
|
||||||
Title: "Sample Page",
|
Title: "Sample Page",
|
||||||
AuthorName: account.AuthorName,
|
AuthorName: account.AuthorName,
|
||||||
Content: content,
|
Content: content,
|
||||||
|
@ -135,7 +134,7 @@ func ExampleAccount_CreatePage() {
|
||||||
|
|
||||||
func ExampleAccount_EditPage() {
|
func ExampleAccount_EditPage() {
|
||||||
var err error
|
var err error
|
||||||
page, err = account.EditPage(&telegraph.Page{
|
page, err = account.EditPage(telegraph.Page{
|
||||||
Title: "Sample Page",
|
Title: "Sample Page",
|
||||||
AuthorName: account.AuthorName,
|
AuthorName: account.AuthorName,
|
||||||
Content: content,
|
Content: content,
|
||||||
|
@ -169,10 +168,7 @@ func ExampleAccount_GetPageList() {
|
||||||
|
|
||||||
func ExampleGetViews() {
|
func ExampleGetViews() {
|
||||||
pagePath := "Sample-Page-12-15"
|
pagePath := "Sample-Page-12-15"
|
||||||
views, err := telegraph.GetViews(
|
views, err := telegraph.GetViews(pagePath, 2016, 12)
|
||||||
pagePath,
|
|
||||||
time.Date(2016, 12, 0, 0, 0, 0, 0, nil),
|
|
||||||
)
|
|
||||||
errCheck(err)
|
errCheck(err)
|
||||||
|
|
||||||
log.Println(pagePath, "has been viewed", views.Views, "times")
|
log.Println(pagePath, "has been viewed", views.Views, "times")
|
||||||
|
|
|
@ -3,24 +3,24 @@ package telegraph
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
json "github.com/pquerna/ffjson/ffjson"
|
|
||||||
http "github.com/valyala/fasthttp"
|
http "github.com/valyala/fasthttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetAccountInfo get information about a Telegraph account. Returns an Account object on success.
|
// GetAccountInfo get information about a Telegraph account. Returns an Account object on success.
|
||||||
func (a *Account) GetAccountInfo(fields ...string) (r *Account, err error) {
|
func (a *Account) GetAccountInfo(fields ...string) (*Account, error) {
|
||||||
args := http.AcquireArgs()
|
args := http.AcquireArgs()
|
||||||
defer http.ReleaseArgs(args)
|
defer http.ReleaseArgs(args)
|
||||||
args.Add("access_token", a.AccessToken) // required
|
args.Add("access_token", a.AccessToken) // required
|
||||||
args.Add("fields", `["`+strings.Join(fields, `","`)+`"]`)
|
if len(fields) > 0 {
|
||||||
|
args.Add("fields", `["`+strings.Join(fields, `","`)+`"]`)
|
||||||
|
}
|
||||||
|
|
||||||
dst := new(Response)
|
data, err := makeRequest("getAccountInfo", args)
|
||||||
dst, err = makeRequest("getAccountInfo", args)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
r = new(Account)
|
var result Account
|
||||||
err = json.Unmarshal(*dst.Result, r)
|
err = parser.Unmarshal(data, &result)
|
||||||
return
|
return &result, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
package telegraph
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetAccountInfo(t *testing.T) {
|
||||||
|
t.Run("invalid", func(t *testing.T) {
|
||||||
|
var a Account
|
||||||
|
_, err := a.GetAccountInfo()
|
||||||
|
assert.Error(t, err)
|
||||||
|
})
|
||||||
|
t.Run("valid", func(t *testing.T) {
|
||||||
|
a := Account{
|
||||||
|
AccessToken: "b968da509bb76866c35425099bc0989a5ec3b32997d55286c657e6994bbb",
|
||||||
|
ShortName: "Sandbox",
|
||||||
|
AuthorName: "Anonymous",
|
||||||
|
}
|
||||||
|
|
||||||
|
info, err := a.GetAccountInfo(FieldShortName, FieldPageCount)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, a.ShortName, info.ShortName)
|
||||||
|
assert.NotZero(t, info.PageCount)
|
||||||
|
})
|
||||||
|
}
|
12
get_page.go
12
get_page.go
|
@ -4,24 +4,22 @@ import (
|
||||||
gopath "path"
|
gopath "path"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
json "github.com/pquerna/ffjson/ffjson"
|
|
||||||
http "github.com/valyala/fasthttp"
|
http "github.com/valyala/fasthttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetPage get a Telegraph page. Returns a Page object on success.
|
// GetPage get a Telegraph page. Returns a Page object on success.
|
||||||
func GetPage(path string, returnContent bool) (r *Page, err error) {
|
func GetPage(path string, returnContent bool) (*Page, error) {
|
||||||
args := http.AcquireArgs()
|
args := http.AcquireArgs()
|
||||||
defer http.ReleaseArgs(args)
|
defer http.ReleaseArgs(args)
|
||||||
args.Add("path", path) // required
|
args.Add("path", path) // required
|
||||||
args.Add("return_content", strconv.FormatBool(returnContent))
|
args.Add("return_content", strconv.FormatBool(returnContent))
|
||||||
|
|
||||||
dst := new(Response)
|
data, err := makeRequest(gopath.Join("getPage", path), args)
|
||||||
dst, err = makeRequest(gopath.Join("getPage", path), args)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
r = new(Page)
|
var result Page
|
||||||
err = json.Unmarshal(*dst.Result, r)
|
err = parser.Unmarshal(data, &result)
|
||||||
return
|
return &result, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,26 +3,28 @@ package telegraph
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
json "github.com/pquerna/ffjson/ffjson"
|
|
||||||
http "github.com/valyala/fasthttp"
|
http "github.com/valyala/fasthttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetPageList get a list of pages belonging to a Telegraph account. Returns a PageList object, sorted
|
// GetPageList get a list of pages belonging to a Telegraph account. Returns a PageList object, sorted
|
||||||
// by most recently created pages first.
|
// by most recently created pages first.
|
||||||
func (a *Account) GetPageList(offset, limit int) (r *PageList, err error) {
|
func (a *Account) GetPageList(offset, limit int) (*PageList, error) {
|
||||||
args := http.AcquireArgs()
|
args := http.AcquireArgs()
|
||||||
defer http.ReleaseArgs(args)
|
defer http.ReleaseArgs(args)
|
||||||
args.Add("access_token", a.AccessToken) // required
|
args.Add("access_token", a.AccessToken) // required
|
||||||
args.Add("offset", strconv.Itoa(offset))
|
if offset > 0 {
|
||||||
args.Add("limit", strconv.Itoa(limit))
|
args.Add("offset", strconv.Itoa(offset))
|
||||||
|
}
|
||||||
|
if limit > 0 {
|
||||||
|
args.Add("limit", strconv.Itoa(limit))
|
||||||
|
}
|
||||||
|
|
||||||
dst := new(Response)
|
data, err := makeRequest("getPageList", args)
|
||||||
dst, err = makeRequest("getPageList", args)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
r = new(PageList)
|
var result PageList
|
||||||
err = json.Unmarshal(*dst.Result, r)
|
err = parser.Unmarshal(data, &result)
|
||||||
return
|
return &result, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
package telegraph
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetPageList(t *testing.T) {
|
||||||
|
t.Run("invalid", func(t *testing.T) {
|
||||||
|
var a Account
|
||||||
|
_, err := a.GetPageList(0, 0)
|
||||||
|
assert.Error(t, err)
|
||||||
|
})
|
||||||
|
t.Run("valid", func(t *testing.T) {
|
||||||
|
a := Account{
|
||||||
|
AccessToken: "b968da509bb76866c35425099bc0989a5ec3b32997d55286c657e6994bbb",
|
||||||
|
ShortName: "Sandbox",
|
||||||
|
AuthorName: "Anonymous",
|
||||||
|
}
|
||||||
|
|
||||||
|
list, err := a.GetPageList(1, 1)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, list)
|
||||||
|
})
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
package telegraph
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetPage(t *testing.T) {
|
||||||
|
t.Run("invalid", func(t *testing.T) {
|
||||||
|
_, err := GetPage("wtf", true)
|
||||||
|
assert.Error(t, err)
|
||||||
|
})
|
||||||
|
t.Run("valid", func(t *testing.T) {
|
||||||
|
page, err := GetPage("Sample-Page-12-15", true)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, page)
|
||||||
|
})
|
||||||
|
}
|
29
get_views.go
29
get_views.go
|
@ -3,30 +3,35 @@ package telegraph
|
||||||
import (
|
import (
|
||||||
gopath "path"
|
gopath "path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
|
||||||
|
|
||||||
json "github.com/pquerna/ffjson/ffjson"
|
|
||||||
http "github.com/valyala/fasthttp"
|
http "github.com/valyala/fasthttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetViews get the number of views for a Telegraph article. By default, the total number of page
|
// GetViews get the number of views for a Telegraph article. By default, the total number of page
|
||||||
// views will be returned. Returns a PageViews object on success.
|
// views will be returned. Returns a PageViews object on success.
|
||||||
func GetViews(path string, date time.Time) (r *PageViews, err error) {
|
func GetViews(path string, date ...int) (*PageViews, error) {
|
||||||
args := http.AcquireArgs()
|
args := http.AcquireArgs()
|
||||||
defer http.ReleaseArgs(args)
|
defer http.ReleaseArgs(args)
|
||||||
args.Add("path", path) // required
|
args.Add("path", path) // required
|
||||||
args.Add("year", strconv.Itoa(date.Year()))
|
if len(date) > 0 {
|
||||||
args.Add("month", strconv.Itoa(int(date.Month())))
|
args.Add("year", strconv.Itoa(date[0]))
|
||||||
args.Add("day", strconv.Itoa(date.Day()))
|
}
|
||||||
args.Add("hour", strconv.Itoa(date.Hour()))
|
if len(date) > 1 {
|
||||||
|
args.Add("month", strconv.Itoa(date[1]))
|
||||||
|
}
|
||||||
|
if len(date) > 2 {
|
||||||
|
args.Add("day", strconv.Itoa(date[2]))
|
||||||
|
}
|
||||||
|
if len(date) > 3 {
|
||||||
|
args.Add("hour", strconv.Itoa(date[3]))
|
||||||
|
}
|
||||||
|
|
||||||
dst := new(Response)
|
data, err := makeRequest(gopath.Join("getViews", path), args)
|
||||||
dst, err = makeRequest(gopath.Join("getViews", path), args)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
r = new(PageViews)
|
var result PageViews
|
||||||
err = json.Unmarshal(*dst.Result, r)
|
err = parser.Unmarshal(data, &result)
|
||||||
return
|
return &result, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
package telegraph
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetViews(t *testing.T) {
|
||||||
|
t.Run("invalid", func(t *testing.T) {
|
||||||
|
t.Run("path", func(t *testing.T) {
|
||||||
|
_, err := GetViews("wtf")
|
||||||
|
assert.Error(t, err)
|
||||||
|
})
|
||||||
|
t.Run("year", func(t *testing.T) {
|
||||||
|
_, err := GetViews("Sample-Page-12-15", 1980)
|
||||||
|
assert.Error(t, err)
|
||||||
|
})
|
||||||
|
t.Run("month", func(t *testing.T) {
|
||||||
|
_, err := GetViews("Sample-Page-12-15", 2000, 22)
|
||||||
|
assert.Error(t, err)
|
||||||
|
})
|
||||||
|
t.Run("day", func(t *testing.T) {
|
||||||
|
_, err := GetViews("Sample-Page-12-15", 2000, 2, 42)
|
||||||
|
assert.Error(t, err)
|
||||||
|
})
|
||||||
|
t.Run("hour", func(t *testing.T) {
|
||||||
|
_, err := GetViews("Sample-Page-12-15", 2000, 2, 12, 65)
|
||||||
|
assert.Error(t, err)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
t.Run("valid", func(t *testing.T) {
|
||||||
|
stats, err := GetViews("Sample-Page-12-15")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
if !assert.NotNil(t, stats) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
assert.NotZero(t, stats.Views)
|
||||||
|
})
|
||||||
|
}
|
65
request.go
65
request.go
|
@ -1,65 +0,0 @@
|
||||||
package telegraph
|
|
||||||
|
|
||||||
import (
|
|
||||||
gojson "encoding/json"
|
|
||||||
"errors"
|
|
||||||
"net/url"
|
|
||||||
|
|
||||||
"github.com/kirillDanshin/dlog"
|
|
||||||
json "github.com/pquerna/ffjson/ffjson"
|
|
||||||
http "github.com/valyala/fasthttp"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Response contains a JSON object, which always has a Boolean field ok. If ok
|
|
||||||
// equals true, the request was successful, and the result of the query can be
|
|
||||||
// found in the result field. In case of an unsuccessful request, ok equals
|
|
||||||
// false, and the error is explained in the error field (e.g.
|
|
||||||
// SHORT_NAME_REQUIRED).
|
|
||||||
type Response struct {
|
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Error string `json:"error"`
|
|
||||||
Result *gojson.RawMessage `json:"result,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
var defaultURL = url.URL{
|
|
||||||
Scheme: "https",
|
|
||||||
Host: "api.telegra.ph",
|
|
||||||
}
|
|
||||||
|
|
||||||
func makeRequest(path string, args *http.Args) (r *Response, err error) {
|
|
||||||
requestURL := defaultURL
|
|
||||||
requestURL.Path = path
|
|
||||||
requestURL.RawQuery = args.String()
|
|
||||||
|
|
||||||
req := http.AcquireRequest()
|
|
||||||
defer http.ReleaseRequest(req)
|
|
||||||
req.Header.SetMethod("GET")
|
|
||||||
req.SetRequestURI(requestURL.String())
|
|
||||||
req.Header.SetUserAgent("toby3d/telegraph")
|
|
||||||
req.Header.SetContentType("application/json;charset=utf-8")
|
|
||||||
|
|
||||||
dlog.Ln("request:")
|
|
||||||
dlog.D(req)
|
|
||||||
|
|
||||||
resp := http.AcquireResponse()
|
|
||||||
defer http.ReleaseResponse(resp)
|
|
||||||
err = http.Do(req, resp)
|
|
||||||
if err != nil {
|
|
||||||
dlog.Ln(err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
dlog.Ln("response:")
|
|
||||||
dlog.D(resp)
|
|
||||||
|
|
||||||
r = new(Response)
|
|
||||||
if err = json.Unmarshal(resp.Body(), r); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if !r.Ok {
|
|
||||||
err = errors.New(r.Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
|
@ -1,30 +1,23 @@
|
||||||
package telegraph
|
package telegraph
|
||||||
|
|
||||||
import (
|
import (
|
||||||
json "github.com/pquerna/ffjson/ffjson"
|
|
||||||
http "github.com/valyala/fasthttp"
|
http "github.com/valyala/fasthttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
type revokeAccessTokenParameters struct {
|
|
||||||
// Access token of the Telegraph account.
|
|
||||||
AccessToken string `json:"access_token"` // required
|
|
||||||
}
|
|
||||||
|
|
||||||
// RevokeAccessToken revoke access_token and generate a new one, for example, if the user would
|
// RevokeAccessToken revoke access_token and generate a new one, for example, if the user would
|
||||||
// like to reset all connected sessions, or you have reasons to believe the token was compromised. On
|
// like to reset all connected sessions, or you have reasons to believe the token was compromised. On
|
||||||
// success, returns an Account object with new access_token and auth_url fields.
|
// success, returns an Account object with new access_token and auth_url fields.
|
||||||
func (a *Account) RevokeAccessToken() (r *Account, err error) {
|
func (a *Account) RevokeAccessToken() (*Account, error) {
|
||||||
args := http.AcquireArgs()
|
args := http.AcquireArgs()
|
||||||
defer http.ReleaseArgs(args)
|
defer http.ReleaseArgs(args)
|
||||||
args.Add("access_token", a.AccessToken)
|
args.Add("access_token", a.AccessToken)
|
||||||
|
|
||||||
dst := new(Response)
|
resp, err := makeRequest("revokeAccessToken", args)
|
||||||
dst, err = makeRequest("revokeAccessToken", args)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
r = new(Account)
|
var account Account
|
||||||
err = json.Unmarshal(*dst.Result, r)
|
err = parser.Unmarshal(resp, &account)
|
||||||
return
|
return &account, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
package telegraph
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRevokeAccessToken(t *testing.T) {
|
||||||
|
t.Run("invalid", func(t *testing.T) {
|
||||||
|
var account Account
|
||||||
|
_, err := account.RevokeAccessToken()
|
||||||
|
assert.Error(t, err)
|
||||||
|
})
|
||||||
|
t.Run("valid", func(t *testing.T) {
|
||||||
|
a, err := CreateAccount(Account{
|
||||||
|
ShortName: "Sandbox",
|
||||||
|
AuthorName: "Anonymous",
|
||||||
|
})
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
|
||||||
|
newAccount, err := a.RevokeAccessToken()
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
assert.NotEqual(t, a.AccessToken, newAccount.AccessToken)
|
||||||
|
assert.NotEmpty(t, newAccount.AuthURL)
|
||||||
|
})
|
||||||
|
}
|
|
@ -0,0 +1,56 @@
|
||||||
|
//go:generate ffjson $GOFILE
|
||||||
|
package telegraph
|
||||||
|
|
||||||
|
import (
|
||||||
|
gojson "encoding/json"
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
json "github.com/json-iterator/go"
|
||||||
|
http "github.com/valyala/fasthttp"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Response contains a JSON object, which always has a Boolean field ok. If ok
|
||||||
|
// equals true, the request was successful, and the result of the query can be
|
||||||
|
// found in the result field. In case of an unsuccessful request, ok equals
|
||||||
|
// false, and the error is explained in the error field (e.g.
|
||||||
|
// SHORT_NAME_REQUIRED).
|
||||||
|
type Response struct {
|
||||||
|
Ok bool `json:"ok"`
|
||||||
|
Error string `json:"error"`
|
||||||
|
Result gojson.RawMessage `json:"result,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var parser = json.ConfigFastest //nolint:gochecknoglobals
|
||||||
|
|
||||||
|
func makeRequest(path string, args *http.Args) ([]byte, error) {
|
||||||
|
u := http.AcquireURI()
|
||||||
|
defer http.ReleaseURI(u)
|
||||||
|
u.SetScheme("https")
|
||||||
|
u.SetHost("api.telegra.ph")
|
||||||
|
u.SetPath(path)
|
||||||
|
args.CopyTo(u.QueryArgs())
|
||||||
|
|
||||||
|
req := http.AcquireRequest()
|
||||||
|
defer http.ReleaseRequest(req)
|
||||||
|
req.Header.SetMethod(http.MethodGet)
|
||||||
|
req.SetRequestURIBytes(u.FullURI())
|
||||||
|
req.Header.SetUserAgent("toby3d/telegraph")
|
||||||
|
req.Header.SetContentType("application/json; charset=utf-8")
|
||||||
|
|
||||||
|
resp := http.AcquireResponse()
|
||||||
|
defer http.ReleaseResponse(resp)
|
||||||
|
if err := http.Do(req, resp); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var r Response
|
||||||
|
if err := parser.Unmarshal(resp.Body(), &r); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !r.Ok {
|
||||||
|
return nil, errors.New(r.Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.Result, nil
|
||||||
|
}
|
5
types.go
5
types.go
|
@ -1,9 +1,10 @@
|
||||||
// All types used in the Telegraph API responses are represented as JSON-objects.
|
//go:generate ffjson $GOFILE
|
||||||
// Optional fields may be not returned when irrelevant.
|
|
||||||
package telegraph
|
package telegraph
|
||||||
|
|
||||||
import "errors"
|
import "errors"
|
||||||
|
|
||||||
|
// All types used in the Telegraph API responses are represented as JSON-objects.
|
||||||
|
// Optional fields may be not returned when irrelevant.
|
||||||
type (
|
type (
|
||||||
// Account represents a Telegraph account.
|
// Account represents a Telegraph account.
|
||||||
Account struct {
|
Account struct {
|
||||||
|
|
Loading…
Reference in New Issue