Skip to content

Commit

Permalink
Merge pull request #11 from jjideenschmiede/development
Browse files Browse the repository at this point in the history
Add sandbox mode for moa2.0
  • Loading branch information
gowizzard authored Oct 14, 2021
2 parents 67b0c74 + 5704adf commit e94eb84
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ To generate an access token for the moa api, you can use the following function.
r := goidealo.Request{
ClientId: "",
ClientPassword: "",
Sandbox: false,
}

// Get access token
Expand Down
12 changes: 8 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ const (
pwsAccessTokenUrl = "https://api.idealo.com/mer/businessaccount/api/v1/oauth/token"
pwsBaseUrl = "https://import.idealo.com"
moaBaseUrl = "https://orders.idealo.com"
moaSandboxBaseUrl = "https://orders-sandbox.idealo.com"
)

// Config is to define config data
type Config struct {
PwsAccessToken, MoaAccessToken, Pws, Moa bool
Path, Method string
Body []byte
PwsAccessToken, MoaAccessToken, Pws, Moa, MoaSandbox bool
Path, Method string
Body []byte
}

// Request is to define the request data
type Request struct {
ClientId, ClientPassword, AccessToken string
Sandbox bool
}

// Send is to send a new request
Expand All @@ -45,11 +47,13 @@ func (c Config) Send(r Request) (*http.Response, error) {
case c.PwsAccessToken:
url = pwsAccessTokenUrl
case c.MoaAccessToken:
url = moaBaseUrl + "/api/v2/oauth/token"
url = moaBaseUrl + c.Path
case c.Pws:
url = pwsBaseUrl + c.Path
case c.Moa:
url = moaBaseUrl + c.Path
case c.MoaSandbox:
url = moaSandboxBaseUrl + c.Path
}

// Define client
Expand Down
13 changes: 13 additions & 0 deletions moa_access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ func MoaAccessToken(r Request) (MoaAccessTokenReturn, error) {
// Config new request
c := Config{
MoaAccessToken: true,
Path: "/api/v2/oauth/token",
Method: "POST",
}

// Check sandbox
if r.Sandbox {
c.MoaAccessToken = false
c.MoaSandbox = true
}

// Send new request
response, err := c.Send(r)
if err != nil {
Expand All @@ -40,6 +47,12 @@ func MoaAccessToken(r Request) (MoaAccessTokenReturn, error) {
// Close request body
defer response.Body.Close()

// Check response status
err = pwsStatusCodes(response.Status)
if err != nil {
return MoaAccessTokenReturn{}, err
}

// Decode data
var decode MoaAccessTokenReturn

Expand Down
6 changes: 6 additions & 0 deletions pws_access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ func PwsAccessToken(r Request) (PwsAccessTokenReturn, error) {
// Close request body
defer response.Body.Close()

// Check response status
err = pwsStatusCodes(response.Status)
if err != nil {
return PwsAccessTokenReturn{}, err
}

// Decode data
var decode PwsAccessTokenReturn

Expand Down

0 comments on commit e94eb84

Please sign in to comment.