Skip to content

Commit

Permalink
refactoring client to the interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Molodkovets Bogdan committed Dec 2, 2019
1 parent 1fef455 commit b07d9f8
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions client.go
Expand Up @@ -13,6 +13,26 @@ const (
calculatorURLDefault = "http://api.cdek.ru/calculator/calculate_price_by_json.php"
)

//ServiceAccessСonfigurator allows to configure client for the service
type ServiceAccessСonfigurator interface {
SetAuth(account, secure string) ServiceProvider
SetCalculatorURL(calculatorURL string) ServiceProvider
}

//ServiceProvider provides CDEK API functionality
type ServiceProvider interface {
ServiceAccessСonfigurator

CalculateDelivery(req GetCostReq) (*GetCostRespResult, error)
GetCities(filter map[CityFilter]string) (*GetCitiesResp, error)
GetPvzList(filter map[PvzListFilter]string) ([]*Pvz, error)
GetRegions(filter map[RegionFilter]string) (*GetRegionsResp, error)
RegisterOrder(req RegisterOrderReq) (*RegisterOrderResp, error)
UpdateOrder(req UpdateOrderReq) (*UpdateOrderResp, error)
DeleteOrder(req DeleteOrderReq) (*DeleteOrderResp, error)
GetStatusReport(statusReportReq StatusReport) (*StatusReportResp, error)
}

//Client SDK Client configuration
type Client struct {
auth *auth
Expand All @@ -21,15 +41,15 @@ type Client struct {
}

//NewClient Client constructor with defaults
func NewClient(apiURL string) *Client {
func NewClient(apiURL string) ServiceProvider {
return &Client{
apiURL: apiURL,
calculatorURL: calculatorURLDefault,
}
}

//SetAuth set auth data
func (c *Client) SetAuth(account, secure string) *Client {
func (c *Client) SetAuth(account, secure string) ServiceProvider {
c.auth = &auth{
account: account,
secure: secure,
Expand All @@ -39,7 +59,7 @@ func (c *Client) SetAuth(account, secure string) *Client {
}

//SetCalculatorURL url for delivery calculation
func (c *Client) SetCalculatorURL(calculatorURL string) *Client {
func (c *Client) SetCalculatorURL(calculatorURL string) ServiceProvider {
c.calculatorURL = calculatorURL

return c
Expand Down

0 comments on commit b07d9f8

Please sign in to comment.