🎨,🔨 Small code refactor
This commit is contained in:
parent
88549f2719
commit
483180ac60
10
account.go
10
account.go
|
@ -36,7 +36,7 @@ func CreateAccount(account *Account) (*Account, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var resp Account
|
var resp Account
|
||||||
if err := json.Unmarshal(body.Result, &resp); err != nil {
|
if err := json.Unmarshal(*body.Result, &resp); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,7 +70,7 @@ func (account *Account) EditAccountInfo(update *Account) (*Account, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var resp Account
|
var resp Account
|
||||||
if err := json.Unmarshal(body.Result, &resp); err != nil {
|
if err := json.Unmarshal(*body.Result, &resp); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,7 +87,7 @@ func (account *Account) GetAccountInfo(fields ...string) (*Account, error) {
|
||||||
|
|
||||||
// List of account fields to return. Available fields: short_name,
|
// List of account fields to return. Available fields: short_name,
|
||||||
// author_name, author_url, auth_url, page_count.
|
// author_name, author_url, auth_url, page_count.
|
||||||
args.Add("fields", fmt.Sprintf(`["%s"]`, strings.Join(fields, `","`)))
|
args.Add("fields", fmt.Sprint(`["`, strings.Join(fields, `","`), `"]`))
|
||||||
|
|
||||||
url := fmt.Sprintf(APIEndpoint, "getAccountInfo")
|
url := fmt.Sprintf(APIEndpoint, "getAccountInfo")
|
||||||
body, err := request(url, &args)
|
body, err := request(url, &args)
|
||||||
|
@ -96,7 +96,7 @@ func (account *Account) GetAccountInfo(fields ...string) (*Account, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var resp Account
|
var resp Account
|
||||||
if err := json.Unmarshal(body.Result, &resp); err != nil {
|
if err := json.Unmarshal(*body.Result, &resp); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,7 +120,7 @@ func (account *Account) RevokeAccessToken() (*Account, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var resp Account
|
var resp Account
|
||||||
if err := json.Unmarshal(body.Result, &resp); err != nil {
|
if err := json.Unmarshal(*body.Result, &resp); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
10
page.go
10
page.go
|
@ -48,7 +48,7 @@ func (account *Account) CreatePage(page *Page, returnContent bool) (*Page, error
|
||||||
}
|
}
|
||||||
|
|
||||||
var resp Page
|
var resp Page
|
||||||
if err := json.Unmarshal(body.Result, &resp); err != nil {
|
if err := json.Unmarshal(*body.Result, &resp); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,7 +96,7 @@ func (account *Account) EditPage(update *Page, returnContent bool) (*Page, error
|
||||||
}
|
}
|
||||||
|
|
||||||
var resp Page
|
var resp Page
|
||||||
if err := json.Unmarshal(body.Result, &resp); err != nil {
|
if err := json.Unmarshal(*body.Result, &resp); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,7 +117,7 @@ func GetPage(path string, returnContent bool) (*Page, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var resp Page
|
var resp Page
|
||||||
if err := json.Unmarshal(body.Result, &resp); err != nil {
|
if err := json.Unmarshal(*body.Result, &resp); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,7 +145,7 @@ func (account *Account) GetPageList(offset, limit int) (*PageList, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var resp PageList
|
var resp PageList
|
||||||
if err := json.Unmarshal(body.Result, &resp); err != nil {
|
if err := json.Unmarshal(*body.Result, &resp); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -189,7 +189,7 @@ func GetViews(path string, hour, day, month, year int) (*PageViews, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var resp PageViews
|
var resp PageViews
|
||||||
if err := json.Unmarshal(body.Result, &resp); err != nil {
|
if err := json.Unmarshal(*body.Result, &resp); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
118
telegraph.go
118
telegraph.go
|
@ -3,10 +3,9 @@
|
||||||
package telegraph
|
package telegraph
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
ffjson "github.com/pquerna/ffjson/ffjson"
|
json "github.com/pquerna/ffjson/ffjson"
|
||||||
http "github.com/valyala/fasthttp"
|
http "github.com/valyala/fasthttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -20,119 +19,6 @@ const (
|
||||||
PathEndpoint = "https://api.telegra.ph/%s/%s"
|
PathEndpoint = "https://api.telegra.ph/%s/%s"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
|
||||||
// Account represents a Telegraph account.
|
|
||||||
Account struct {
|
|
||||||
// Only returned by the createAccount and revokeAccessToken method.
|
|
||||||
// Access token of the Telegraph account.
|
|
||||||
AccessToken string `json:"access_token"` // optional
|
|
||||||
|
|
||||||
// URL to authorize a browser on telegra.ph and connect it to a
|
|
||||||
// Telegraph account. This URL is valid for only one use and for 5
|
|
||||||
// minutes only.
|
|
||||||
AuthURL string `json:"auth_url"` // optional
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
ShortName string `json:"short_name"`
|
|
||||||
|
|
||||||
// Default author name used when creating new articles.
|
|
||||||
AuthorName string `json:"author_name"`
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
AuthorURL string `json:"author_url"`
|
|
||||||
|
|
||||||
// Number of pages belonging to the Telegraph account.
|
|
||||||
PageCount int `json:"page_count"` // optional
|
|
||||||
}
|
|
||||||
|
|
||||||
// PageList represents a list of Telegraph articles belonging to an
|
|
||||||
// account. Most recently created articles first.
|
|
||||||
PageList struct {
|
|
||||||
// Total number of pages belonging to the target Telegraph account.
|
|
||||||
TotalCount int `json:"total_count"`
|
|
||||||
|
|
||||||
// Requested pages of the target Telegraph account.
|
|
||||||
Pages []Page `json:"pages"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Page represents a page on Telegraph.
|
|
||||||
Page struct {
|
|
||||||
// Path to the page.
|
|
||||||
Path string `json:"path"`
|
|
||||||
|
|
||||||
// URL of the page.
|
|
||||||
URL string `json:"url"`
|
|
||||||
|
|
||||||
// Title of the page.
|
|
||||||
Title string `json:"title"`
|
|
||||||
|
|
||||||
// Description of the page.
|
|
||||||
Description string `json:"description"`
|
|
||||||
|
|
||||||
// Name of the author, displayed below the title.
|
|
||||||
AuthorName string `json:"author_name"` // optional
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
AuthorURL string `json:"author_url"` // optional
|
|
||||||
|
|
||||||
// Image URL of the page.
|
|
||||||
ImageURL string `json:"image_url"` // optional
|
|
||||||
|
|
||||||
// Content of the page.
|
|
||||||
Content []Node `json:"content"` // optional
|
|
||||||
|
|
||||||
// Number of page views for the page.
|
|
||||||
Views int `json:"views"`
|
|
||||||
|
|
||||||
// Only returned if access_token passed. True, if the target Telegraph
|
|
||||||
// account can edit the page.
|
|
||||||
CanEdit bool `json:"can_edit"` // optional
|
|
||||||
}
|
|
||||||
|
|
||||||
// PageViews represents the number of page views for a Telegraph article.
|
|
||||||
PageViews struct {
|
|
||||||
// Number of page views for the target page.
|
|
||||||
Views int `json:"views"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Node is abstract object represents a DOM Node. It can be a String which
|
|
||||||
// represents a DOM text node or a NodeElement object.
|
|
||||||
Node interface{}
|
|
||||||
|
|
||||||
// NodeElement represents a DOM element node.
|
|
||||||
NodeElement struct {
|
|
||||||
// Name of the DOM element. Available tags: a, aside, b, blockquote,
|
|
||||||
// br, code, em, figcaption, figure, h3, h4, hr, i, iframe, img, li,
|
|
||||||
// ol, p, pre, s, strong, u, ul, video.
|
|
||||||
Tag string `json:"tag"`
|
|
||||||
|
|
||||||
// Attributes of the DOM element. Key of object represents name of
|
|
||||||
// attribute, value represents value of attribute. Available
|
|
||||||
// attributes: href, src.
|
|
||||||
Attrs map[string]string `json:"attrs"` // optional
|
|
||||||
|
|
||||||
// List of child nodes for the DOM element.
|
|
||||||
Children []Node `json:"children"` // optional
|
|
||||||
}
|
|
||||||
|
|
||||||
// Response represents a response from the Telegram API with the result
|
|
||||||
// stored raw. 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).
|
|
||||||
Response struct {
|
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Error string `json:"error"`
|
|
||||||
Result json.RawMessage `json:"result"`
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
func request(url string, args *http.Args) (*Response, error) {
|
func request(url string, args *http.Args) (*Response, error) {
|
||||||
_, body, err := http.Post(nil, url, args)
|
_, body, err := http.Post(nil, url, args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -140,7 +26,7 @@ func request(url string, args *http.Args) (*Response, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var resp Response
|
var resp Response
|
||||||
if err := ffjson.Unmarshal(body, &resp); err != nil {
|
if err := json.Unmarshal(body, &resp); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,116 @@
|
||||||
|
package telegraph
|
||||||
|
|
||||||
|
import "encoding/json"
|
||||||
|
|
||||||
|
type (
|
||||||
|
// Account represents a Telegraph account.
|
||||||
|
Account struct {
|
||||||
|
// Only returned by the createAccount and revokeAccessToken method.
|
||||||
|
// Access token of the Telegraph account.
|
||||||
|
AccessToken string `json:"access_token"` // optional
|
||||||
|
|
||||||
|
// URL to authorize a browser on telegra.ph and connect it to a
|
||||||
|
// Telegraph account. This URL is valid for only one use and for 5
|
||||||
|
// minutes only.
|
||||||
|
AuthURL string `json:"auth_url"` // optional
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
ShortName string `json:"short_name"`
|
||||||
|
|
||||||
|
// Default author name used when creating new articles.
|
||||||
|
AuthorName string `json:"author_name"`
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
AuthorURL string `json:"author_url"`
|
||||||
|
|
||||||
|
// Number of pages belonging to the Telegraph account.
|
||||||
|
PageCount int `json:"page_count"` // optional
|
||||||
|
}
|
||||||
|
|
||||||
|
// PageList represents a list of Telegraph articles belonging to an
|
||||||
|
// account. Most recently created articles first.
|
||||||
|
PageList struct {
|
||||||
|
// Total number of pages belonging to the target Telegraph account.
|
||||||
|
TotalCount int `json:"total_count"`
|
||||||
|
|
||||||
|
// Requested pages of the target Telegraph account.
|
||||||
|
Pages []*Page `json:"pages"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Page represents a page on Telegraph.
|
||||||
|
Page struct {
|
||||||
|
// Path to the page.
|
||||||
|
Path string `json:"path"`
|
||||||
|
|
||||||
|
// URL of the page.
|
||||||
|
URL string `json:"url"`
|
||||||
|
|
||||||
|
// Title of the page.
|
||||||
|
Title string `json:"title"`
|
||||||
|
|
||||||
|
// Description of the page.
|
||||||
|
Description string `json:"description"`
|
||||||
|
|
||||||
|
// Name of the author, displayed below the title.
|
||||||
|
AuthorName string `json:"author_name"` // optional
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
AuthorURL string `json:"author_url"` // optional
|
||||||
|
|
||||||
|
// Image URL of the page.
|
||||||
|
ImageURL string `json:"image_url"` // optional
|
||||||
|
|
||||||
|
// Content of the page.
|
||||||
|
Content []Node `json:"content"` // optional
|
||||||
|
|
||||||
|
// Number of page views for the page.
|
||||||
|
Views int `json:"views"`
|
||||||
|
|
||||||
|
// Only returned if access_token passed. True, if the target Telegraph
|
||||||
|
// account can edit the page.
|
||||||
|
CanEdit bool `json:"can_edit"` // optional
|
||||||
|
}
|
||||||
|
|
||||||
|
// PageViews represents the number of page views for a Telegraph article.
|
||||||
|
PageViews struct {
|
||||||
|
// Number of page views for the target page.
|
||||||
|
Views int `json:"views"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Node is abstract object represents a DOM Node. It can be a String which
|
||||||
|
// represents a DOM text node or a NodeElement object.
|
||||||
|
Node interface{}
|
||||||
|
|
||||||
|
// NodeElement represents a DOM element node.
|
||||||
|
NodeElement struct {
|
||||||
|
// Name of the DOM element. Available tags: a, aside, b, blockquote,
|
||||||
|
// br, code, em, figcaption, figure, h3, h4, hr, i, iframe, img, li,
|
||||||
|
// ol, p, pre, s, strong, u, ul, video.
|
||||||
|
Tag string `json:"tag"`
|
||||||
|
|
||||||
|
// Attributes of the DOM element. Key of object represents name of
|
||||||
|
// attribute, value represents value of attribute. Available
|
||||||
|
// attributes: href, src.
|
||||||
|
Attrs map[string]string `json:"attrs"` // optional
|
||||||
|
|
||||||
|
// List of child nodes for the DOM element.
|
||||||
|
Children []Node `json:"children"` // optional
|
||||||
|
}
|
||||||
|
|
||||||
|
// Response represents a response from the Telegram API with the result
|
||||||
|
// stored raw. 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).
|
||||||
|
Response struct {
|
||||||
|
Ok bool `json:"ok"`
|
||||||
|
Error string `json:"error"`
|
||||||
|
Result *json.RawMessage `json:"result"`
|
||||||
|
}
|
||||||
|
)
|
Loading…
Reference in New Issue