Documentation
¶
Index ¶
- Constants
- Variables
- func SetConfig(config ClientConfig)
- func WithEndpoint(endpoint string)
- func WithToken(token string)
- type API
- type APIs
- type Body
- type Client
- func (c Client) Do(r *http.Request, v interface{}) (*http.Response, error)
- func (c *Client) NewRequest(ctx context.Context, method, urlPath string, data interface{}) (*http.Request, error)
- func (c *Client) NewRestRequest(ctx context.Context, method, urlPath string, data map[string]string, ...) (*http.Request, error)
- type ClientConfig
- type ClientOption
Constants ¶
const ( APIURI string = "https://securepayments.sberbank.ru" APISandboxURI string = "https://3dsec.sberbank.ru" )
URLS for API endpoints
Variables ¶
var NewRequest = func(c *Client, ctx context.Context, method, urlPath string, data interface{}) (*http.Request, error) { if strings.Contains(urlPath, "rest") { return nil, fmt.Errorf("path contains rest request, use NewRestRequest instead") } if err := c.Config.validate(); err != nil { return nil, err } uri := APIURI + urlPath if c.Config.SandboxMode { uri = APISandboxURI + urlPath } if c.Config.endpoint != "" { uri = c.Config.endpoint + urlPath } reqBodyData, _ := json.Marshal(data) req, err := http.NewRequest(method, uri, bytes.NewReader(reqBodyData)) if err != nil { return nil, err } req.Header.Set("Cache-Control", "no-cache") req.Header.Set("Content-Type", "application/json") req = req.WithContext(ctx) return req, nil }
var NewRestRequest = func(c *Client, ctx context.Context, method, urlPath string, data map[string]string, jsonParams map[string]string) (*http.Request, error) { uri := APIURI + urlPath if c.Config.SandboxMode { uri = APISandboxURI + urlPath } if c.Config.endpoint != "" { uri = c.Config.endpoint + urlPath } jsonParamsEncoded, _ := json.Marshal(jsonParams) body := url.Values{} body.Add("userName", c.Config.UserName) body.Add("password", c.Config.Password) body.Add("currency", strconv.Itoa(c.Config.Currency)) if string(jsonParamsEncoded[:]) != "null" { body.Add("jsonParams", string(jsonParamsEncoded[:])) body.Add("sessionTimeoutSecs", strconv.Itoa(c.Config.SessionTimeoutSecs)) } for key, value := range data { if value != "" { body.Add(key, value) } } reqData := body.Encode() if method == "GET" { fmt.Println(reqData) uri += "?" + reqData reqData = "" } req, err := http.NewRequest(method, uri, strings.NewReader(reqData)) if err != nil { return nil, err } req.Header.Set("Cache-Control", "no-cache") req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req = req.WithContext(ctx) return req, nil }
Functions ¶
func SetConfig ¶
func SetConfig(config ClientConfig)
func WithEndpoint ¶
func WithEndpoint(endpoint string)
WithEndpoint configures a Client to use the specified API endpoint.
Types ¶
type API ¶
type API interface { NewRestRequest(ctx context.Context, method, urlPath string, data map[string]string, jsonParams map[string]string) (*http.Request, error) NewRequest(ctx context.Context, method, urlPath string, data interface{}) (*http.Request, error) Do(r *http.Request, v interface{}) (*http.Response, error) }
func GetAPI ¶
func GetAPI(options ...ClientOption) API
type APIs ¶
type APIs struct { Api API // contains filtered or unexported fields }
APIs are the currently supported endpoints.
type Body ¶
type Body struct { UserName *string `json:"userName"` Token *string `json:"token"` Password *string `json:"password"` Language *string `json:"language"` Currency *string `json:"currency"` SessionTimeoutSecs *int `json:"sessionTimeoutSecs"` JsonParams []byte `json:"jsonParams"` }
Body struct
type Client ¶
type Client struct { Config *ClientConfig // contains filtered or unexported fields }
Client is a client to SberBank API
func (*Client) NewRequest ¶
func (c *Client) NewRequest(ctx context.Context, method, urlPath string, data interface{}) (*http.Request, error)
NewRequest creates an HTTP request against the API (mobile payments). The returned request is assigned with ctx and has all necessary headers set (auth, user agent, etc.). NewRestRequest creates an HTTP request against the API with 'rest' in path. The returned request is assigned with ctx and has all necessary headers set (auth, user agent, etc.).
func (*Client) NewRestRequest ¶
func (c *Client) NewRestRequest(ctx context.Context, method, urlPath string, data map[string]string, jsonParams map[string]string) (*http.Request, error)
NewRestRequest creates an HTTP request against the API with 'rest' in path. The returned request is assigned with ctx and has all necessary headers set (auth, user agent, etc.).