From af1e4b7fa723113f542b28a90deb8c4d1201ca64 Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Tue, 5 Sep 2017 02:09:59 +0500 Subject: [PATCH] :hammer: Refactoring code --- account.go | 128 -------------------------- content.go | 5 +- create_account.go | 41 +++++++++ create_page.go | 54 +++++++++++ edit_account_info.go | 40 +++++++++ edit_page.go | 55 ++++++++++++ get_account_info.go | 33 +++++++ get_page.go | 28 ++++++ get_page_list.go | 35 ++++++++ get_views.go | 53 +++++++++++ page.go | 197 ----------------------------------------- revoke_access_token.go | 30 +++++++ telegraph.go | 1 - 13 files changed, 371 insertions(+), 329 deletions(-) delete mode 100644 account.go create mode 100644 create_account.go create mode 100644 create_page.go create mode 100644 edit_account_info.go create mode 100644 edit_page.go create mode 100644 get_account_info.go create mode 100644 get_page.go create mode 100644 get_page_list.go create mode 100644 get_views.go delete mode 100644 page.go create mode 100644 revoke_access_token.go diff --git a/account.go b/account.go deleted file mode 100644 index 95382e7..0000000 --- a/account.go +++ /dev/null @@ -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 -} diff --git a/content.go b/content.go index 3e4c5dc..8116269 100644 --- a/content.go +++ b/content.go @@ -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} } } } diff --git a/create_account.go b/create_account.go new file mode 100644 index 0000000..7fba7b3 --- /dev/null +++ b/create_account.go @@ -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 +} diff --git a/create_page.go b/create_page.go new file mode 100644 index 0000000..171bc7e --- /dev/null +++ b/create_page.go @@ -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 +} diff --git a/edit_account_info.go b/edit_account_info.go new file mode 100644 index 0000000..da808ec --- /dev/null +++ b/edit_account_info.go @@ -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 +} diff --git a/edit_page.go b/edit_page.go new file mode 100644 index 0000000..28f8915 --- /dev/null +++ b/edit_page.go @@ -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 +} diff --git a/get_account_info.go b/get_account_info.go new file mode 100644 index 0000000..78776d7 --- /dev/null +++ b/get_account_info.go @@ -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 +} diff --git a/get_page.go b/get_page.go new file mode 100644 index 0000000..8f410d4 --- /dev/null +++ b/get_page.go @@ -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 +} diff --git a/get_page_list.go b/get_page_list.go new file mode 100644 index 0000000..af9d228 --- /dev/null +++ b/get_page_list.go @@ -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 +} diff --git a/get_views.go b/get_views.go new file mode 100644 index 0000000..be80121 --- /dev/null +++ b/get_views.go @@ -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 +} diff --git a/page.go b/page.go deleted file mode 100644 index 43e9bdd..0000000 --- a/page.go +++ /dev/null @@ -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 -} diff --git a/revoke_access_token.go b/revoke_access_token.go new file mode 100644 index 0000000..2b6a213 --- /dev/null +++ b/revoke_access_token.go @@ -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 +} diff --git a/telegraph.go b/telegraph.go index 0038fb8..201a1e5 100644 --- a/telegraph.go +++ b/telegraph.go @@ -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.