🔨 Refactoring code

This commit is contained in:
Maxim Lebedev 2017-09-05 02:09:59 +05:00
parent 483180ac60
commit af1e4b7fa7
No known key found for this signature in database
GPG Key ID: F8978F46FF0FFA4F
13 changed files with 371 additions and 329 deletions

View File

@ -1,128 +0,0 @@
package telegraph
import (
"fmt"
"strings"
json "github.com/pquerna/ffjson/ffjson"
http "github.com/valyala/fasthttp"
)
// CreateAccount create a new Telegraph account. Most users only need one
// account, but this can be useful for channel administrators who would like
// to 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 access_token field.
func CreateAccount(account *Account) (*Account, error) {
var args http.Args
// Account name, helps users with several accounts remember which they are
// currently using. Displayed to the user above the "Edit/Publish" button
// on Telegra.ph, other users don't see this name.
args.Add("short_name", account.ShortName) // required
// Default author name used when creating new articles.
args.Add("author_name", account.AuthorName)
// Default profile link, opened when users click on the author's name
// below the title. Can be any link, not necessarily to a Telegram profile
// or channel.
args.Add("author_url", account.AuthorURL)
url := fmt.Sprintf(APIEndpoint, "createAccount")
body, err := request(url, &args)
if err != nil {
return nil, err
}
var resp Account
if err := json.Unmarshal(*body.Result, &resp); err != nil {
return nil, err
}
return &resp, nil
}
// EditAccountInfo update information about a Telegraph account. Pass only the
// parameters that you want to edit. On success, returns an Account object
// with the default fields.
func (account *Account) EditAccountInfo(update *Account) (*Account, error) {
var args http.Args
// Access token of the Telegraph account.
args.Add("access_token", account.AccessToken) // required
// New account name.
args.Add("short_name", update.ShortName)
// New default author name used when creating new articles.
args.Add("author_name", update.AuthorName)
// New default profile link, opened when users click on the author's name
// below the title. Can be any link, not necessarily to a Telegram profile
// or channel.
args.Add("author_url", update.AuthorURL)
url := fmt.Sprintf(APIEndpoint, "editAccountInfo")
body, err := request(url, &args)
if err != nil {
return nil, err
}
var resp Account
if err := json.Unmarshal(*body.Result, &resp); err != nil {
return nil, err
}
return &resp, nil
}
// GetAccountInfo get information about a Telegraph account. Returns an
// Account object on success.
func (account *Account) GetAccountInfo(fields ...string) (*Account, error) {
var args http.Args
// Access token of the Telegraph account.
args.Add("access_token", account.AccessToken) // required
// List of account fields to return. Available fields: short_name,
// author_name, author_url, auth_url, page_count.
args.Add("fields", fmt.Sprint(`["`, strings.Join(fields, `","`), `"]`))
url := fmt.Sprintf(APIEndpoint, "getAccountInfo")
body, err := request(url, &args)
if err != nil {
return nil, err
}
var resp Account
if err := json.Unmarshal(*body.Result, &resp); err != nil {
return nil, err
}
return &resp, nil
}
// 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 success, returns an Account object
// with new access_token and auth_url fields.
func (account *Account) RevokeAccessToken() (*Account, error) {
var args http.Args
// Access token of the Telegraph account.
args.Add("access_token", account.AccessToken) // required
url := fmt.Sprintf(APIEndpoint, "revokeAccessToken")
body, err := request(url, &args)
if err != nil {
return nil, err
}
var resp Account
if err := json.Unmarshal(*body.Result, &resp); err != nil {
return nil, err
}
return &resp, nil
}

View File

@ -81,11 +81,10 @@ func domToNode(domNode *html.Node) interface{} {
var nodeElement NodeElement
if _, ok := availableTags[strings.ToLower(domNode.Data)]; ok {
nodeElement.Tag = domNode.Data
for _, attr := range domNode.Attr {
if _, ok := availableAttributes[strings.ToLower(attr.Key)]; ok {
nodeElement.Attrs = map[string]string{
attr.Key: attr.Val,
}
nodeElement.Attrs = map[string]string{attr.Key: attr.Val}
}
}
}

41
create_account.go Normal file
View File

@ -0,0 +1,41 @@
package telegraph
import (
"fmt"
json "github.com/pquerna/ffjson/ffjson"
http "github.com/valyala/fasthttp"
)
// CreateAccount create a new Telegraph account. Most users only need one
// account, but this can be useful for channel administrators who would like
// to 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 access_token field.
func CreateAccount(account *Account) (*Account, error) {
var args http.Args
// Account name, helps users with several accounts remember which they are
// currently using. Displayed to the user above the "Edit/Publish" button
// on Telegra.ph, other users don't see this name.
args.Add("short_name", account.ShortName) // required
// Default author name used when creating new articles.
args.Add("author_name", account.AuthorName)
// Default profile link, opened when users click on the author's name
// below the title. Can be any link, not necessarily to a Telegram profile
// or channel.
args.Add("author_url", account.AuthorURL)
url := fmt.Sprintf(APIEndpoint, "createAccount")
body, err := request(url, &args)
if err != nil {
return nil, err
}
var resp Account
err = json.Unmarshal(*body.Result, &resp)
return &resp, err
}

54
create_page.go Normal file
View File

@ -0,0 +1,54 @@
package telegraph
import (
"fmt"
"strconv"
json "github.com/pquerna/ffjson/ffjson"
http "github.com/valyala/fasthttp"
)
// CreatePage create a new Telegraph page. On success, returns a Page object.
func (account *Account) CreatePage(page *Page, returnContent bool) (*Page, error) {
var args http.Args
// Access token of the Telegraph account.
args.Add("access_token", account.AccessToken) // required
// Page title.
args.Add("title", page.Title) // required
if page.AuthorName != "" {
// Author name, displayed below the article's title.
args.Add("author_name", page.AuthorName)
}
if page.AuthorURL != "" {
// Profile link, opened when users click on the author's name below
// the title. Can be any link, not necessarily to a Telegram profile
// or channel.
args.Add("author_url", page.AuthorURL)
}
// If true, a content field will be returned in the Page object.
args.Add("return_content", strconv.FormatBool(returnContent))
content, err := json.Marshal(page.Content)
if err != nil {
return nil, err
}
// Content of the page.
args.Add("content", string(content)) // required
url := fmt.Sprintf(APIEndpoint, "createPage")
body, err := request(url, &args)
if err != nil {
return nil, err
}
var resp Page
err = json.Unmarshal(*body.Result, &resp)
return &resp, err
}

40
edit_account_info.go Normal file
View File

@ -0,0 +1,40 @@
package telegraph
import (
"fmt"
json "github.com/pquerna/ffjson/ffjson"
http "github.com/valyala/fasthttp"
)
// EditAccountInfo update information about a Telegraph account. Pass only the
// parameters that you want to edit. On success, returns an Account object
// with the default fields.
func (account *Account) EditAccountInfo(update *Account) (*Account, error) {
var args http.Args
// Access token of the Telegraph account.
args.Add("access_token", account.AccessToken) // required
// New account name.
args.Add("short_name", update.ShortName)
// New default author name used when creating new articles.
args.Add("author_name", update.AuthorName)
// New default profile link, opened when users click on the author's name
// below the title. Can be any link, not necessarily to a Telegram profile
// or channel.
args.Add("author_url", update.AuthorURL)
url := fmt.Sprintf(APIEndpoint, "editAccountInfo")
body, err := request(url, &args)
if err != nil {
return nil, err
}
var resp Account
err = json.Unmarshal(*body.Result, &resp)
return &resp, err
}

55
edit_page.go Normal file
View File

@ -0,0 +1,55 @@
package telegraph
import (
"fmt"
"strconv"
json "github.com/pquerna/ffjson/ffjson"
http "github.com/valyala/fasthttp"
)
// EditPage edit an existing Telegraph page. On success, returns a Page
// object.
func (account *Account) EditPage(update *Page, returnContent bool) (*Page, error) {
var args http.Args
// Access token of the Telegraph account.
args.Add("access_token", account.AccessToken) // required
// Page title.
args.Add("title", update.Title) // required
if update.AuthorName != "" {
// Author name, displayed below the article's title.
args.Add("author_name", update.AuthorName)
}
if update.AuthorURL != "" {
// Profile link, opened when users click on the author's name below
// the title. Can be any link, not necessarily to a Telegram profile
// or channel.
args.Add("author_url", update.AuthorURL)
}
// If true, a content field will be returned in the Page object.
args.Add("return_content", strconv.FormatBool(returnContent))
content, err := json.Marshal(update.Content)
if err != nil {
return nil, err
}
// Content of the page.
args.Add("content", string(content)) // required
url := fmt.Sprintf(PathEndpoint, "editPage", update.Path)
body, err := request(url, &args)
if err != nil {
return nil, err
}
var resp Page
err = json.Unmarshal(*body.Result, &resp)
return &resp, err
}

33
get_account_info.go Normal file
View File

@ -0,0 +1,33 @@
package telegraph
import (
"fmt"
"strings"
json "github.com/pquerna/ffjson/ffjson"
http "github.com/valyala/fasthttp"
)
// GetAccountInfo get information about a Telegraph account. Returns an
// Account object on success.
func (account *Account) GetAccountInfo(fields ...string) (*Account, error) {
var args http.Args
// Access token of the Telegraph account.
args.Add("access_token", account.AccessToken) // required
// List of account fields to return. Available fields: short_name,
// author_name, author_url, auth_url, page_count.
args.Add("fields", fmt.Sprint(`["`, strings.Join(fields, `","`), `"]`))
url := fmt.Sprintf(APIEndpoint, "getAccountInfo")
body, err := request(url, &args)
if err != nil {
return nil, err
}
var resp Account
err = json.Unmarshal(*body.Result, &resp)
return &resp, err
}

28
get_page.go Normal file
View File

@ -0,0 +1,28 @@
package telegraph
import (
"fmt"
"strconv"
json "github.com/pquerna/ffjson/ffjson"
http "github.com/valyala/fasthttp"
)
// GetPage get a Telegraph page. Returns a Page object on success.
func GetPage(path string, returnContent bool) (*Page, error) {
var args http.Args
// If true, content field will be returned in Page object.
args.Add("return_content", strconv.FormatBool(returnContent))
url := fmt.Sprintf(PathEndpoint, "getPage", path)
body, err := request(url, &args)
if err != nil {
return nil, err
}
var resp Page
err = json.Unmarshal(*body.Result, &resp)
return &resp, err
}

35
get_page_list.go Normal file
View File

@ -0,0 +1,35 @@
package telegraph
import (
"fmt"
"strconv"
json "github.com/pquerna/ffjson/ffjson"
http "github.com/valyala/fasthttp"
)
// GetPageList get a list of pages belonging to a Telegraph account. Returns
// a PageList object, sorted by most recently created pages first.
func (account *Account) GetPageList(offset, limit int) (*PageList, error) {
var args http.Args
// Access token of the Telegraph account.
args.Add("access_token", account.AccessToken) // required
// Sequential number of the first page to be returned.
args.Add("offset", strconv.Itoa(offset))
// Limits the number of pages to be retrieved.
args.Add("limit", strconv.Itoa(limit))
url := fmt.Sprintf(APIEndpoint, "getPageList")
body, err := request(url, &args)
if err != nil {
return nil, err
}
var resp PageList
err = json.Unmarshal(*body.Result, &resp)
return &resp, err
}

53
get_views.go Normal file
View File

@ -0,0 +1,53 @@
package telegraph
import (
"fmt"
"strconv"
json "github.com/pquerna/ffjson/ffjson"
http "github.com/valyala/fasthttp"
)
// 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.
func GetViews(path string, hour, day, month, year int) (*PageViews, error) {
var args http.Args
if hour > -1 {
// If passed, the number of page views for the requested hour will
// be returned.
args.Add("hour", strconv.Itoa(hour))
}
if day > 0 {
// Required if hour is passed. If passed, the number of page views
// for the requested day will be returned.
args.Add("day", strconv.Itoa(day))
}
if month > 0 {
// Required if day is passed. If passed, the number of page views
// for the requested month will be returned.
args.Add("month", strconv.Itoa(month))
}
if year > 0 {
// Required if month is passed. If passed, the number of page views
// for the requested year will be returned.
args.Add("year", strconv.Itoa(year))
}
url := fmt.Sprintf(PathEndpoint, "getViews", path)
body, err := request(url, &args)
if err != nil {
return nil, err
}
var resp PageViews
if err := json.Unmarshal(*body.Result, &resp); err != nil {
return nil, err
}
return &resp, nil
}

197
page.go
View File

@ -1,197 +0,0 @@
package telegraph
import (
"fmt"
"strconv"
json "github.com/pquerna/ffjson/ffjson"
http "github.com/valyala/fasthttp"
)
// CreatePage create a new Telegraph page. On success, returns a Page object.
func (account *Account) CreatePage(page *Page, returnContent bool) (*Page, error) {
var args http.Args
// Access token of the Telegraph account.
args.Add("access_token", account.AccessToken) // required
// Page title.
args.Add("title", page.Title) // required
if page.AuthorName != "" {
// Author name, displayed below the article's title.
args.Add("author_name", page.AuthorName)
}
if page.AuthorURL != "" {
// Profile link, opened when users click on the author's name below
// the title. Can be any link, not necessarily to a Telegram profile
// or channel.
args.Add("author_url", page.AuthorURL)
}
// If true, a content field will be returned in the Page object.
args.Add("return_content", strconv.FormatBool(returnContent))
content, err := json.Marshal(page.Content)
if err != nil {
return nil, err
}
// Content of the page.
args.Add("content", string(content)) // required
url := fmt.Sprintf(APIEndpoint, "createPage")
body, err := request(url, &args)
if err != nil {
return nil, err
}
var resp Page
if err := json.Unmarshal(*body.Result, &resp); err != nil {
return nil, err
}
return &resp, nil
}
// EditPage edit an existing Telegraph page. On success, returns a Page
// object.
func (account *Account) EditPage(update *Page, returnContent bool) (*Page, error) {
var args http.Args
// Access token of the Telegraph account.
args.Add("access_token", account.AccessToken) // required
// Page title.
args.Add("title", update.Title) // required
if update.AuthorName != "" {
// Author name, displayed below the article's title.
args.Add("author_name", update.AuthorName)
}
if update.AuthorURL != "" {
// Profile link, opened when users click on the author's name below
// the title. Can be any link, not necessarily to a Telegram profile
// or channel.
args.Add("author_url", update.AuthorURL)
}
// If true, a content field will be returned in the Page object.
args.Add("return_content", strconv.FormatBool(returnContent))
content, err := json.Marshal(update.Content)
if err != nil {
return nil, err
}
// Content of the page.
args.Add("content", string(content)) // required
url := fmt.Sprintf(PathEndpoint, "editPage", update.Path)
body, err := request(url, &args)
if err != nil {
return nil, err
}
var resp Page
if err := json.Unmarshal(*body.Result, &resp); err != nil {
return nil, err
}
return &resp, nil
}
// GetPage get a Telegraph page. Returns a Page object on success.
func GetPage(path string, returnContent bool) (*Page, error) {
var args http.Args
// If true, content field will be returned in Page object.
args.Add("return_content", strconv.FormatBool(returnContent))
url := fmt.Sprintf(PathEndpoint, "getPage", path)
body, err := request(url, &args)
if err != nil {
return nil, err
}
var resp Page
if err := json.Unmarshal(*body.Result, &resp); err != nil {
return nil, err
}
return &resp, nil
}
// GetPageList get a list of pages belonging to a Telegraph account. Returns
// a PageList object, sorted by most recently created pages first.
func (account *Account) GetPageList(offset, limit int) (*PageList, error) {
var args http.Args
// Access token of the Telegraph account.
args.Add("access_token", account.AccessToken) // required
// Sequential number of the first page to be returned.
args.Add("offset", strconv.Itoa(offset))
// Limits the number of pages to be retrieved.
args.Add("limit", strconv.Itoa(limit))
url := fmt.Sprintf(APIEndpoint, "getPageList")
body, err := request(url, &args)
if err != nil {
return nil, err
}
var resp PageList
if err := json.Unmarshal(*body.Result, &resp); err != nil {
return nil, err
}
return &resp, nil
}
// 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.
func GetViews(path string, hour, day, month, year int) (*PageViews, error) {
var args http.Args
if hour > -1 {
// If passed, the number of page views for the requested hour will
// be returned.
args.Add("hour", strconv.Itoa(hour))
}
if day > 0 {
// Required if hour is passed. If passed, the number of page views
// for the requested day will be returned.
args.Add("day", strconv.Itoa(day))
}
if month > 0 {
// Required if day is passed. If passed, the number of page views
// for the requested month will be returned.
args.Add("month", strconv.Itoa(month))
}
if year > 0 {
// Required if month is passed. If passed, the number of page views
// for the requested year will be returned.
args.Add("year", strconv.Itoa(year))
}
url := fmt.Sprintf(PathEndpoint, "getViews", path)
body, err := request(url, &args)
if err != nil {
return nil, err
}
var resp PageViews
if err := json.Unmarshal(*body.Result, &resp); err != nil {
return nil, err
}
return &resp, nil
}

30
revoke_access_token.go Normal file
View File

@ -0,0 +1,30 @@
package telegraph
import (
"fmt"
json "github.com/pquerna/ffjson/ffjson"
http "github.com/valyala/fasthttp"
)
// 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 success, returns an Account object
// with new access_token and auth_url fields.
func (account *Account) RevokeAccessToken() (*Account, error) {
var args http.Args
// Access token of the Telegraph account.
args.Add("access_token", account.AccessToken) // required
url := fmt.Sprintf(APIEndpoint, "revokeAccessToken")
body, err := request(url, &args)
if err != nil {
return nil, err
}
var resp Account
err = json.Unmarshal(*body.Result, &resp)
return &resp, nil
}

View File

@ -9,7 +9,6 @@ import (
http "github.com/valyala/fasthttp"
)
// Telegraph constants
const (
// APIEndpoint should be presented in this for all queries to the Telegraph
// API must be served over HTTPS.