Skip to content

Commit

Permalink
add support for specifying custom api url
Browse files Browse the repository at this point in the history
  • Loading branch information
celestix committed Apr 11, 2024
1 parent 1f3b075 commit a9810bc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
17 changes: 7 additions & 10 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,19 @@ import (
// GetTelegraphClient returns a new TelegraphClient using the specified options.
func GetTelegraphClient(options *ClientOpt) *TelegraphClient {
if options == nil {
options = GetDefaultOptions()
options = &ClientOpt{}
}
if options.HttpClient == nil {
options.HttpClient = http.DefaultClient
}
if options.ApiUrl == "" {
options.ApiUrl = "https://api.telegra.ph/"
}

return &TelegraphClient{
HttpClient: options.HttpClient,
}
}

// GetDefaultOptions returns the default-options used for constructing a
// TelegraphClient.
func GetDefaultOptions() *ClientOpt {
return &ClientOpt{
HttpClient: http.DefaultClient,
}
}

// EditInfo is a helper method to easily call EditAccountInfo by an account.
func (a *Account) EditInfo(client *TelegraphClient, opts *EditAccountInfoOpts) (*Account, error) {
return client.EditAccountInfo(a.AccessToken, opts)
Expand Down
2 changes: 1 addition & 1 deletion requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Body struct {
}

func (c *TelegraphClient) InvokeRequest(method string, params url.Values) (json.RawMessage, error) {
r, err := http.NewRequest(http.MethodPost, "https://api.telegra.ph/"+method, strings.NewReader(params.Encode()))
r, err := http.NewRequest(http.MethodPost, c.ApiUrl+method, strings.NewReader(params.Encode()))
if err != nil {
return nil, fmt.Errorf("failed to build POST request to %s: %w", method, err)
}
Expand Down
5 changes: 5 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ import (

// TelegraphClient is the client that contains all library methods implemented on it.
type TelegraphClient struct {
// Api URL of the Telegraph API.
ApiUrl string
// HttpClient is the http client used to send http requests to the Telegraph API.
HttpClient *http.Client
}

// ClientOpt is the options used to construct the TelegraphClient value.
type ClientOpt struct {
// Api URL of the Telegraph API.
ApiUrl string
// HttpClient is the http client used to send http requests to the Telegraph API.
HttpClient *http.Client
}

Expand Down

0 comments on commit a9810bc

Please sign in to comment.