🔀 Merge branch 'develop'
This commit is contained in:
commit
eb5e871e39
|
@ -0,0 +1,16 @@
|
||||||
|
# https://EditorConfig.org
|
||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
charset = utf-8
|
||||||
|
end_of_line = lf
|
||||||
|
insert_final_newline = false
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
|
||||||
|
[*.go]
|
||||||
|
indent_style = tab
|
||||||
|
indent_size = 8
|
||||||
|
|
||||||
|
[*.{html,js,css,yaml,yml,md}]
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 2
|
|
@ -1,21 +1,16 @@
|
||||||
image: golang:alpine
|
image: golang:alpine
|
||||||
|
|
||||||
cache:
|
|
||||||
paths:
|
|
||||||
- /go/src/github.com
|
|
||||||
- /go/src/gitlab.com
|
|
||||||
- /go/src/golang.org
|
|
||||||
- /go/src/google.golang.org
|
|
||||||
- /go/src/gopkg.in
|
|
||||||
|
|
||||||
stages:
|
stages:
|
||||||
- test
|
- test
|
||||||
|
- review
|
||||||
|
|
||||||
before_script:
|
before_script:
|
||||||
- apk add --no-cache git build-base bash
|
- apk add --no-cache git build-base bash make
|
||||||
- mkdir -p /go/src/gitlab.com/$CI_PROJECT_NAMESPACE /go/src/_/builds
|
- mkdir -p /go/src/gitlab.com/$CI_PROJECT_NAMESPACE /go/src/_/builds
|
||||||
- cp -r $CI_PROJECT_DIR /go/src/gitlab.com/$CI_PROJECT_PATH
|
- cp -r $CI_PROJECT_DIR /go/src/gitlab.com/$CI_PROJECT_PATH
|
||||||
- ln -s /go/src/gitlab.com/$CI_PROJECT_NAMESPACE /go/src/_/builds/$CI_PROJECT_NAMESPACE
|
- ln -s /go/src/gitlab.com/$CI_PROJECT_NAMESPACE /go/src/_/builds/$CI_PROJECT_NAMESPACE
|
||||||
|
- go get github.com/golangci/golangci-lint/cmd/golangci-lint
|
||||||
|
- go install github.com/golangci/golangci-lint/cmd/golangci-lint
|
||||||
- make dep
|
- make dep
|
||||||
|
|
||||||
unit_tests:
|
unit_tests:
|
||||||
|
@ -23,19 +18,14 @@ unit_tests:
|
||||||
script:
|
script:
|
||||||
- make test
|
- make test
|
||||||
|
|
||||||
.race_detector:
|
|
||||||
stage: test
|
|
||||||
script:
|
|
||||||
- make race
|
|
||||||
|
|
||||||
code_coverage:
|
code_coverage:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- make coverage
|
- make coverage
|
||||||
|
coverage: '/^coverage:\s(\d+(?:\.\d+)?%)/'
|
||||||
|
|
||||||
lint_code:
|
lint_code:
|
||||||
stage: test
|
stage: review
|
||||||
script:
|
script:
|
||||||
- go get github.com/go-critic/go-critic/cmd/gocritic
|
- make lint
|
||||||
- go install github.com/go-critic/go-critic/cmd/gocritic
|
allow_failure: true
|
||||||
- make lint
|
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
default_stages: [commit, push]
|
||||||
|
fail_fast: true
|
||||||
|
minimum_pre_commit_version: '1'
|
||||||
|
repos:
|
||||||
|
- repo: https://github.com/syntaqx/git-hooks
|
||||||
|
rev: v0.0.16
|
||||||
|
hooks:
|
||||||
|
- id: go-mod-tidy
|
||||||
|
- id: go-fmt
|
||||||
|
- id: go-test
|
||||||
|
- repo: https://github.com/golangci/golangci-lint
|
||||||
|
rev: v1.17.1
|
||||||
|
hooks:
|
||||||
|
- id: golangci-lint
|
|
@ -1,5 +1,5 @@
|
||||||
# MIT License
|
# MIT License
|
||||||
Copyright (c) 2018 Maxim Lebedev
|
Copyright (c) 2019 Maxim Lebedev
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
21
Makefile
21
Makefile
|
@ -1,32 +1,29 @@
|
||||||
PROJECT_NAMESPACE := $(CI_PROJECT_NAMESPACE)
|
PROJECT_NAMESPACE := $(CI_PROJECT_NAMESPACE)
|
||||||
PROJECT_NAME := $(CI_PROJECT_NAME)
|
PROJECT_NAME := $(CI_PROJECT_NAME)
|
||||||
PROJECT_PATH := $(PROJECT_NAMESPACE)/$(PROJECT_NAME)
|
PROJECT_PATH := "$(PROJECT_NAMESPACE)/$(PROJECT_NAME)"
|
||||||
PACKAGE_NAME := "gitlab.com/$(PROJECT_PATH)"
|
PACKAGE_NAME := "gitlab.com/$(PROJECT_PATH)"
|
||||||
PACKAGE_PATH := $(GOPATH)/src/$(PACKAGE_NAME)
|
PACKAGE_PATH := "$(GOPATH)/src/$(PACKAGE_NAME)"
|
||||||
PACKAGE_LIST := $(shell go list $(PACKAGE_NAME)/... | grep -v /vendor/)
|
PACKAGE_LIST := $(shell go list $(PACKAGE_NAME)/... | grep -v /vendor/)
|
||||||
GO_FILES := $(shell find . -name '*.go' | grep -v /vendor/ | grep -v _test.go)
|
GO_FILES := $(shell find . -name '*.go' | grep -v /vendor/ | grep -v _test.go)
|
||||||
|
|
||||||
.PHONY: all lint test rase coverage dep build clean
|
.PHONY: all lint test rase coverage dep
|
||||||
|
|
||||||
all: build
|
all: dep test race lint
|
||||||
|
|
||||||
lint: ## Lint the files
|
lint: ## Lint the files
|
||||||
@gocritic check-project $(PACKAGE_PATH)
|
@golangci-lint run ./...
|
||||||
|
|
||||||
test: ## Run unittests
|
test: ## Run unittests
|
||||||
@go test -short ${PACKAGE_LIST}
|
@go test -short $(PACKAGE_NAME)/...
|
||||||
|
|
||||||
race: dep ## Run data race detector
|
race: dep ## Run data race detector
|
||||||
@go test -race -short ${PACKAGE_LIST}
|
@go test -race -short ${PACKAGE_LIST}
|
||||||
|
|
||||||
coverage: ## Generate global code coverage report
|
coverage: ## Generate global code coverage report
|
||||||
@go test -cover -v -coverpkg=$(PACKAGE_NAME) ${PACKAGE_LIST}
|
@go test -cover -v -coverpkg=$(PACKAGE_NAME)/... ${PACKAGE_LIST}
|
||||||
|
|
||||||
dep: ## Get the dependencies
|
dep: ## Get the dependencies
|
||||||
@go get -v -d -t ${PACKAGE_LIST}
|
@go get -v -d -t $(PACKAGE_NAME)/...
|
||||||
|
|
||||||
clean: ## Remove previous build
|
|
||||||
@rm -f $(PROJECT_NAME)
|
|
||||||
|
|
||||||
help: ## Display this help screen
|
help: ## Display this help screen
|
||||||
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
|
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
# GoLang bindings for the Telegraph API [![discord](https://discordapp.com/api/guilds/208605007744860163/widget.png)](https://discord.gg/QJ8z5BN)
|
||||||
|
> This project is just to provide a wrapper around the API without any additional features.
|
||||||
|
|
||||||
|
All methods and types available and this library (possibly) is ready for use in production. Yaay!
|
||||||
|
|
||||||
|
## Start using telegraph
|
||||||
|
Download and install it:
|
||||||
|
`$ go get -u gitlab.com/toby3d/telegraph`
|
||||||
|
|
||||||
|
Import it in your code:
|
||||||
|
`import "gitlab.com/toby3d/telegraph"`
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
See [GoDoc examples section](https://godoc.org/gitlab.com/toby3d/telegraph#pkg-examples) or check [example_test.go](/example_test.go).
|
||||||
|
|
||||||
|
## Need help?
|
||||||
|
- [Open new issue](https://gitlab.com/toby3d/telegraph/issues/new)
|
||||||
|
- [Discuss in Discord](https://discord.gg/QJ8z5BN)
|
|
@ -3,8 +3,6 @@ I develop this project in my spare time, and I do it and I will do it free of ch
|
||||||
|
|
||||||
**These people sponsored current version of the project:**
|
**These people sponsored current version of the project:**
|
||||||
- Aurielb
|
- Aurielb
|
||||||
- Daniil Tlenov
|
|
||||||
- @kirillDanshin
|
|
||||||
- MoD21k
|
- MoD21k
|
||||||
- @YamiOdymel
|
- @YamiOdymel
|
||||||
|
|
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,25 +0,0 @@
|
||||||
# GoLang bindings for the Telegraph API [![discord](https://discordapp.com/api/guilds/208605007744860163/widget.png)](https://discord.gg/QJ8z5BN)
|
|
||||||
> This project is just to provide a wrapper around the API without any additional features.
|
|
||||||
|
|
||||||
[![License](https://img.shields.io/npm/l/express.svg?maxAge=2592000)](LICENSE.md)
|
|
||||||
[![Build Status](https://travis-ci.org/toby3d/telegraph.svg)](https://travis-ci.org/toby3d/telegraph)
|
|
||||||
[![GoDoc](https://godoc.org/github.com/toby3d/telegraph?status.svg)](https://godoc.org/github.com/toby3d/telegraph)
|
|
||||||
[![Go Report](https://goreportcard.com/badge/github.com/toby3d/telegraph)](https://goreportcard.com/report/github.com/toby3d/telegraph)
|
|
||||||
[![Patreon](https://img.shields.io/badge/support-patreon-E6461A.svg?maxAge=2592000)](https://www.patreon.com/toby3d)
|
|
||||||
[![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/avelino/awesome-go)
|
|
||||||
|
|
||||||
All [methods](https://toby3d.github.io/telegraph/#available-methods) and [types](https://toby3d.github.io/telegraph/#available-types) available and this library (possibly) is ready for use in production. Yaay!
|
|
||||||
|
|
||||||
## Start using telegraph
|
|
||||||
Download and install it:
|
|
||||||
`$ go get -u github.com/toby3d/telegraph`
|
|
||||||
|
|
||||||
Import it in your code:
|
|
||||||
`import "github.com/toby3d/telegraph"`
|
|
||||||
|
|
||||||
## Examples
|
|
||||||
See [GoDoc examples section](https://godoc.org/github.com/toby3d/telegraph#pkg-examples) or check [example_test.go](/example_test.go).
|
|
||||||
|
|
||||||
## Need help?
|
|
||||||
- [Open new issue](https://github.com/toby3d/telegraph/issues/new)
|
|
||||||
- [Discuss in Discord](https://discord.gg/QJ8z5BN)
|
|
|
@ -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)
|
||||||
|
})
|
||||||
|
}
|
15
go.mod
15
go.mod
|
@ -2,11 +2,12 @@ module gitlab.com/toby3d/telegraph
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/kirillDanshin/dlog v0.0.0-20170728000807-97d876b12bf9
|
github.com/json-iterator/go v1.1.6
|
||||||
github.com/kirillDanshin/myutils v0.0.0-20160713214838-182269b1fbcc // indirect
|
github.com/klauspost/compress v1.7.4 // indirect
|
||||||
github.com/klauspost/compress v1.4.1 // indirect
|
github.com/klauspost/cpuid v1.2.1 // indirect
|
||||||
github.com/klauspost/cpuid v1.2.0 // indirect
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
github.com/pquerna/ffjson v0.0.0-20181028064349-e517b90714f7
|
github.com/modern-go/reflect2 v1.0.1 // indirect
|
||||||
github.com/valyala/fasthttp v1.0.0
|
github.com/stretchr/testify v1.3.0
|
||||||
golang.org/x/net v0.0.0-20181129055619-fae4c4e3ad76
|
github.com/valyala/fasthttp v1.4.0
|
||||||
|
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80
|
||||||
)
|
)
|
||||||
|
|
37
go.sum
37
go.sum
|
@ -1,24 +1,33 @@
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/kirillDanshin/dlog v0.0.0-20170728000807-97d876b12bf9 h1:mA7k8E2Vrmyj5CW/D1XZBFmohVNi7jf757vibGwzRbo=
|
github.com/json-iterator/go v1.1.6 h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwKs=
|
||||||
github.com/kirillDanshin/dlog v0.0.0-20170728000807-97d876b12bf9/go.mod h1:l8CN7iyX1k2xlsTYVTpCtwBPcxThf/jLWDGVcF6T/bM=
|
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
|
||||||
github.com/kirillDanshin/myutils v0.0.0-20160713214838-182269b1fbcc h1:OkOhOn3WBUmfATC1NsA3rBlgHGkjk0KGnR5akl/8uXc=
|
|
||||||
github.com/kirillDanshin/myutils v0.0.0-20160713214838-182269b1fbcc/go.mod h1:Bt95qRxLvpdmASW9s2tTxGdQ5ma4o4n8QFhCvzCew/M=
|
|
||||||
github.com/klauspost/compress v1.4.0 h1:8nsMz3tWa9SWWPL60G1V6CUsf4lLjWLTNEtibhe8gh8=
|
github.com/klauspost/compress v1.4.0 h1:8nsMz3tWa9SWWPL60G1V6CUsf4lLjWLTNEtibhe8gh8=
|
||||||
github.com/klauspost/compress v1.4.0/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
|
github.com/klauspost/compress v1.4.0/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
|
||||||
github.com/klauspost/compress v1.4.1 h1:8VMb5+0wMgdBykOV96DwNwKFQ+WTI4pzYURP99CcB9E=
|
github.com/klauspost/compress v1.7.4 h1:4UqAIzZ1Ns2epCTyJ1d2xMWvxtX+FNSCYWeOFogK9nc=
|
||||||
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
|
github.com/klauspost/compress v1.7.4/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
|
||||||
github.com/klauspost/cpuid v0.0.0-20180405133222-e7e905edc00e h1:+lIPJOWl+jSiJOc70QXJ07+2eg2Jy2EC7Mi11BWujeM=
|
github.com/klauspost/cpuid v0.0.0-20180405133222-e7e905edc00e h1:+lIPJOWl+jSiJOc70QXJ07+2eg2Jy2EC7Mi11BWujeM=
|
||||||
github.com/klauspost/cpuid v0.0.0-20180405133222-e7e905edc00e/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
|
github.com/klauspost/cpuid v0.0.0-20180405133222-e7e905edc00e/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
|
||||||
github.com/klauspost/cpuid v1.2.0 h1:NMpwD2G9JSFOE1/TJjGSo5zG7Yb2bTe7eq1jH+irmeE=
|
github.com/klauspost/cpuid v1.2.1 h1:vJi+O/nMdFt0vqm8NZBI6wzALWdA2X+egi0ogNyrC/w=
|
||||||
github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
|
github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
|
||||||
github.com/pquerna/ffjson v0.0.0-20181028064349-e517b90714f7 h1:gGBSHPOU7g8YjTbhwn+lvFm2VDEhhA+PwDIlstkgSxE=
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||||
github.com/pquerna/ffjson v0.0.0-20181028064349-e517b90714f7/go.mod h1:YARuvh7BUWHNhzDq2OM5tzR2RiCcN2D7sapiKyCel/M=
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
|
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
|
||||||
|
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
||||||
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||||
github.com/valyala/fasthttp v1.0.0 h1:BwIoZQbBsTo3v2F5lz5Oy3TlTq4wLKTLV260EVTEWco=
|
github.com/valyala/fasthttp v1.4.0 h1:PuaTGZIw3mjYhhhbVbCQp8aciRZN9YdoB7MGX9Ko76A=
|
||||||
github.com/valyala/fasthttp v1.0.0/go.mod h1:4vX61m6KN+xDduDNwXrhIAVZaZaZiQ1luJk8LWSxF3s=
|
github.com/valyala/fasthttp v1.4.0/go.mod h1:4vX61m6KN+xDduDNwXrhIAVZaZaZiQ1luJk8LWSxF3s=
|
||||||
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
|
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
golang.org/x/net v0.0.0-20181129055619-fae4c4e3ad76 h1:xx5MUFyRQRbPk6VjWjIE1epE/K5AoDD8QUN116NCy8k=
|
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 h1:Ao/3l156eZf2AW5wK8a7/smtodRU+gha3+BeqJ69lRk=
|
||||||
golang.org/x/net v0.0.0-20181129055619-fae4c4e3ad76/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
|
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
|
||||||
|
}
|
|
@ -1,106 +0,0 @@
|
||||||
package telegraph_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"gitlab.com/toby3d/telegraph"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
invalidAuthorURL = "lolwat"
|
|
||||||
invalidPageURL = "sukablyat'"
|
|
||||||
invalidContent = 42
|
|
||||||
)
|
|
||||||
|
|
||||||
var invalidAccount = new(telegraph.Account)
|
|
||||||
|
|
||||||
func TestInvalidContentFormat(t *testing.T) {
|
|
||||||
_, err := telegraph.ContentFormat(invalidContent)
|
|
||||||
assert.EqualError(t, telegraph.ErrInvalidDataType, err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestInvalidCreateAccount(t *testing.T) {
|
|
||||||
_, err := telegraph.CreateAccount(invalidAccount)
|
|
||||||
assert.Error(t, err)
|
|
||||||
|
|
||||||
t.Run("invalidCreatePage", testInvalidCreatePage)
|
|
||||||
t.Run("invalidEditAccountInfo", testInvalidEditAccountInfo)
|
|
||||||
t.Run("invalidEditPage", testInvalidEditPage)
|
|
||||||
t.Run("invalidGetAccountInfo", testInvalidGetAccountInfo)
|
|
||||||
t.Run("invalidGetPageList", testInvalidGetPageList)
|
|
||||||
t.Run("invalidGetPageListByLimit", testInvalidGetPageListByLimit)
|
|
||||||
t.Run("invalidGetPageListByOffset", testInvalidGetPageListByOffset)
|
|
||||||
t.Run("invalidRevokeAccessToken", testInvalidRevokeAccessToken)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testInvalidCreatePage(t *testing.T) {
|
|
||||||
_, err := invalidAccount.CreatePage(&telegraph.Page{AuthorURL: invalidAuthorURL}, false)
|
|
||||||
assert.Error(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testInvalidEditAccountInfo(t *testing.T) {
|
|
||||||
_, err := invalidAccount.EditAccountInfo(&telegraph.Account{AuthorURL: invalidAuthorURL})
|
|
||||||
assert.Error(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testInvalidEditPage(t *testing.T) {
|
|
||||||
_, err := invalidAccount.EditPage(&telegraph.Page{AuthorURL: invalidAuthorURL}, false)
|
|
||||||
assert.Error(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testInvalidGetAccountInfo(t *testing.T) {
|
|
||||||
_, err := invalidAccount.GetAccountInfo(telegraph.FieldShortName, telegraph.FieldPageCount)
|
|
||||||
assert.Error(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testInvalidGetPageList(t *testing.T) {
|
|
||||||
_, err := invalidAccount.GetPageList(0, 3)
|
|
||||||
assert.Error(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testInvalidGetPageListByOffset(t *testing.T) {
|
|
||||||
_, err := invalidAccount.GetPageList(-42, 3)
|
|
||||||
assert.Error(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testInvalidGetPageListByLimit(t *testing.T) {
|
|
||||||
_, err := invalidAccount.GetPageList(0, 9000)
|
|
||||||
assert.Error(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestInvalidGetPage(t *testing.T) {
|
|
||||||
_, err := telegraph.GetPage(invalidPageURL, true)
|
|
||||||
assert.Error(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestInvalidGetViewsByPage(t *testing.T) {
|
|
||||||
_, err := telegraph.GetViews(invalidPageURL, time.Date(2016, time.December, 0, 0, 0, 0, 0, time.UTC))
|
|
||||||
assert.Error(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestInvalidGetViewsByHour(t *testing.T) {
|
|
||||||
_, err := telegraph.GetViews(validPageURL, time.Date(0, 0, 0, 42, 0, 0, 0, time.UTC))
|
|
||||||
assert.Error(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestInvalidGetViewsByDay(t *testing.T) {
|
|
||||||
_, err := telegraph.GetViews(validPageURL, time.Date(0, 0, 42, 23, 0, 0, 0, time.UTC))
|
|
||||||
assert.Error(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestInvalidGetViewsByMonth(t *testing.T) {
|
|
||||||
_, err := telegraph.GetViews(validPageURL, time.Date(0, 22, 24, 23, 0, 0, 0, time.UTC))
|
|
||||||
assert.Error(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestInvalidGetViewsByYear(t *testing.T) {
|
|
||||||
_, err := telegraph.GetViews(validPageURL, time.Date(1980, time.December, 24, 23, 0, 0, 0, time.UTC))
|
|
||||||
assert.Error(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testInvalidRevokeAccessToken(t *testing.T) {
|
|
||||||
_, err := invalidAccount.RevokeAccessToken()
|
|
||||||
assert.Error(t, err)
|
|
||||||
}
|
|
|
@ -1,125 +0,0 @@
|
||||||
package telegraph_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"gitlab.com/toby3d/telegraph"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
validTitle = "Testing"
|
|
||||||
validShortName = "Sandbox"
|
|
||||||
validAuthorName = "Anonymous"
|
|
||||||
validNewAuthorName = "Gopher"
|
|
||||||
validAuthorURL = "https://t.me/telegraph"
|
|
||||||
validPageURL = "Sample-Page-12-15"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
validContentDOM []telegraph.Node
|
|
||||||
validAccount *telegraph.Account
|
|
||||||
validPage *telegraph.Page
|
|
||||||
|
|
||||||
validContent = `<p>Hello, World!</p>`
|
|
||||||
)
|
|
||||||
|
|
||||||
func testValidContentFormatByString(t *testing.T) {
|
|
||||||
var err error
|
|
||||||
validContentDOM, err = telegraph.ContentFormat(validContent)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.NotEmpty(t, validContentDOM)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testValidContentFormatByBytes(t *testing.T) {
|
|
||||||
var err error
|
|
||||||
validContentDOM, err = telegraph.ContentFormat([]byte(validContent))
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.NotEmpty(t, validContentDOM)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestValidCreateAccount(t *testing.T) {
|
|
||||||
var err error
|
|
||||||
validAccount, err = telegraph.CreateAccount(&telegraph.Account{
|
|
||||||
ShortName: validShortName,
|
|
||||||
// AuthorName: validAuthorName,
|
|
||||||
})
|
|
||||||
assert.NoError(t, err)
|
|
||||||
|
|
||||||
t.Run("validCreatePage", testValidCreatePage)
|
|
||||||
t.Run("validEditAccountInfo", testValidEditAccountInfo)
|
|
||||||
t.Run("validGetAccountInfo", testValidGetAccountInfo)
|
|
||||||
t.Run("validGetPageList", testValidGetPageList)
|
|
||||||
t.Run("validRevokeAccessToken", testValidRevokeAccessToken)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testValidCreatePage(t *testing.T) {
|
|
||||||
t.Run("validContentFormatByString", testValidContentFormatByString)
|
|
||||||
t.Run("validContentFormatByBytes", testValidContentFormatByBytes)
|
|
||||||
|
|
||||||
var err error
|
|
||||||
validPage, err = validAccount.CreatePage(&telegraph.Page{
|
|
||||||
Title: validTitle,
|
|
||||||
AuthorName: validAuthorName,
|
|
||||||
AuthorURL: validAuthorURL,
|
|
||||||
Content: validContentDOM,
|
|
||||||
}, true)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.NotEmpty(t, validPage.URL)
|
|
||||||
t.Run("validEditPage", testValidEditPage)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testValidEditAccountInfo(t *testing.T) {
|
|
||||||
update, err := validAccount.EditAccountInfo(&telegraph.Account{
|
|
||||||
ShortName: validShortName,
|
|
||||||
AuthorName: validNewAuthorName,
|
|
||||||
AuthorURL: validAuthorURL,
|
|
||||||
})
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.NotEqual(t, validAccount.AuthorName, update.AuthorName)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testValidEditPage(t *testing.T) {
|
|
||||||
var err error
|
|
||||||
validPage, err = validAccount.EditPage(&telegraph.Page{
|
|
||||||
Path: validPage.Path,
|
|
||||||
Title: validTitle,
|
|
||||||
AuthorName: validAuthorName,
|
|
||||||
Content: validContentDOM,
|
|
||||||
}, true)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testValidGetAccountInfo(t *testing.T) {
|
|
||||||
info, err := validAccount.GetAccountInfo(telegraph.FieldShortName, telegraph.FieldPageCount)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, validAccount.ShortName, info.ShortName)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestValidGetPage(t *testing.T) {
|
|
||||||
page, err := telegraph.GetPage(validPage.Path, true)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, validPage.Title, page.Title)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testValidGetPageList(t *testing.T) {
|
|
||||||
pages, err := validAccount.GetPageList(0, 3)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.NotZero(t, pages.TotalCount)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestValidGetViews(t *testing.T) {
|
|
||||||
stats, err := telegraph.GetViews(validPageURL, time.Date(2016, time.December, 0, 0, 0, 0, 0, time.UTC))
|
|
||||||
assert.NoError(t, err)
|
|
||||||
t.Log("get", stats.Views, "views")
|
|
||||||
}
|
|
||||||
|
|
||||||
func testValidRevokeAccessToken(t *testing.T) {
|
|
||||||
oldToken := validAccount.AccessToken
|
|
||||||
var err error
|
|
||||||
validAccount, err = validAccount.RevokeAccessToken()
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.NotEmpty(t, validAccount.AccessToken)
|
|
||||||
assert.NotEqual(t, validAccount.AccessToken, oldToken)
|
|
||||||
}
|
|
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