-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(partner): add Partner API support (#349)
- Loading branch information
Showing
10 changed files
with
313 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
repos: | ||
- repo: https://github.com/golangci/golangci-lint | ||
rev: v1.50.1 | ||
rev: v1.62.2 | ||
hooks: | ||
- id: golangci-lint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package upcloud | ||
|
||
// PartnerAccount represents details of an account associated with a partner | ||
type PartnerAccount struct { | ||
Username string `json:"username"` | ||
FirstName string `json:"first_name"` | ||
LastName string `json:"last_name"` | ||
Country string `json:"country"` | ||
State string `json:"state"` | ||
Email string `json:"email"` | ||
Phone string `json:"phone"` | ||
Company string `json:"company"` | ||
Address string `json:"address"` | ||
PostalCode string `json:"postal_code"` | ||
City string `json:"city"` | ||
VATNumber string `json:"vat_number"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package upcloud | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUnmarshalPartnerAccount(t *testing.T) { | ||
originalJSON := ` | ||
{ | ||
"address" : "Some street", | ||
"city" : "Some city", | ||
"company" : "Some company", | ||
"country" : "FIN", | ||
"email" : "[email protected]", | ||
"first_name" : "Some", | ||
"last_name" : "User", | ||
"phone" : "+358.91234567", | ||
"postal_code" : "00100", | ||
"state" : "", | ||
"username" : "someuser", | ||
"vat_number" : "" | ||
} | ||
` | ||
|
||
expectedAccount := PartnerAccount{ | ||
Username: "someuser", | ||
FirstName: "Some", | ||
LastName: "User", | ||
Country: "FIN", | ||
State: "", | ||
Email: "[email protected]", | ||
Phone: "+358.91234567", | ||
Company: "Some company", | ||
Address: "Some street", | ||
PostalCode: "00100", | ||
City: "Some city", | ||
VATNumber: "", | ||
} | ||
|
||
account := PartnerAccount{} | ||
err := json.Unmarshal([]byte(originalJSON), &account) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedAccount, account) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package request | ||
|
||
// CreatePartnerAccountContactDetails represents optional contact details in CreatePartnerAccountRequest | ||
type CreatePartnerAccountContactDetails struct { | ||
FirstName string `json:"first_name,omitempty"` | ||
LastName string `json:"last_name,omitempty"` | ||
Country string `json:"country,omitempty"` | ||
State string `json:"state,omitempty"` | ||
Email string `json:"email,omitempty"` | ||
Phone string `json:"phone,omitempty"` | ||
Company string `json:"company,omitempty"` | ||
Address string `json:"address,omitempty"` | ||
PostalCode string `json:"postal_code,omitempty"` | ||
City string `json:"city,omitempty"` | ||
VATNumber string `json:"vat_number,omitempty"` | ||
} | ||
|
||
// CreatePartnerAccountRequest represents a request to create new main account for partner | ||
type CreatePartnerAccountRequest struct { | ||
Username string `json:"username"` | ||
Password string `json:"password"` | ||
ContactDetails *CreatePartnerAccountContactDetails `json:"contact_details,omitempty"` | ||
} | ||
|
||
func (r *CreatePartnerAccountRequest) RequestURL() string { | ||
return "/partner/accounts" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package request | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCreatePartnerAccountRequestWithoutContactDetails(t *testing.T) { | ||
request := CreatePartnerAccountRequest{ | ||
Username: "someuser", | ||
Password: "superSecret123", | ||
} | ||
expectedJSON := ` | ||
{ | ||
"username" : "someuser", | ||
"password" : "superSecret123" | ||
} | ||
` | ||
actualJSON, err := json.Marshal(&request) | ||
assert.NoError(t, err) | ||
assert.JSONEq(t, expectedJSON, string(actualJSON)) | ||
assert.Equal(t, "/partner/accounts", request.RequestURL()) | ||
} | ||
|
||
func TestCreatePartnerAccountRequestWithMinimalContactDetails(t *testing.T) { | ||
request := CreatePartnerAccountRequest{ | ||
Username: "someuser", | ||
Password: "superSecret123", | ||
ContactDetails: &CreatePartnerAccountContactDetails{ | ||
FirstName: "Some", | ||
LastName: "User", | ||
Country: "FIN", | ||
Email: "[email protected]", | ||
Phone: "+358.91234567", | ||
}, | ||
} | ||
expectedJSON := ` | ||
{ | ||
"username" : "someuser", | ||
"password" : "superSecret123", | ||
"contact_details" : { | ||
"first_name" : "Some", | ||
"last_name" : "User", | ||
"country" : "FIN", | ||
"email" : "[email protected]", | ||
"phone" : "+358.91234567" | ||
} | ||
} | ||
` | ||
actualJSON, err := json.Marshal(&request) | ||
assert.NoError(t, err) | ||
assert.JSONEq(t, expectedJSON, string(actualJSON)) | ||
assert.Equal(t, "/partner/accounts", request.RequestURL()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
--- | ||
version: 1 | ||
interactions: | ||
- request: | ||
body: '{"username":"sdk_test_partner_account","password":"superSecret123","contact_details":{"first_name":"Test","last_name":"User","country":"FIN","email":"[email protected]","phone":"+358.31245434"}}' | ||
form: {} | ||
headers: | ||
Accept: | ||
- application/json | ||
Content-Type: | ||
- application/json | ||
User-Agent: | ||
- upcloud-go-api/8.12.0 | ||
url: https://api.upcloud.com/1.3/partner/accounts | ||
method: POST | ||
response: | ||
body: | | ||
{ | ||
"address" : "", | ||
"city" : "", | ||
"company" : "", | ||
"country" : "FIN", | ||
"email" : "[email protected]", | ||
"first_name" : "Test", | ||
"last_name" : "User", | ||
"phone" : "+358.31245434", | ||
"postal_code" : "", | ||
"state" : "", | ||
"username" : "sdk_test_partner_account", | ||
"vat_number" : "" | ||
} | ||
headers: | ||
Content-Length: | ||
- "303" | ||
Content-Type: | ||
- application/json; charset=UTF-8 | ||
Date: | ||
- Tue, 03 Dec 2024 14:54:50 GMT | ||
Server: | ||
- Apache | ||
status: 201 Created | ||
code: 201 | ||
duration: "" | ||
- request: | ||
body: "" | ||
form: {} | ||
headers: | ||
Accept: | ||
- application/json | ||
Content-Type: | ||
- application/json | ||
User-Agent: | ||
- upcloud-go-api/8.12.0 | ||
url: https://api.upcloud.com/1.3/partner/accounts | ||
method: GET | ||
response: | ||
body: | | ||
[ | ||
{ | ||
"address" : "", | ||
"city" : "", | ||
"company" : "", | ||
"country" : "FIN", | ||
"email" : "[email protected]", | ||
"first_name" : "Test", | ||
"last_name" : "User", | ||
"phone" : "+358.31245434", | ||
"postal_code" : "", | ||
"state" : "", | ||
"username" : "sdk_test_partner_account", | ||
"vat_number" : "" | ||
} | ||
] | ||
headers: | ||
Content-Length: | ||
- "349" | ||
Content-Type: | ||
- application/json; charset=UTF-8 | ||
Date: | ||
- Tue, 03 Dec 2024 14:54:51 GMT | ||
Server: | ||
- Apache | ||
status: 200 OK | ||
code: 200 | ||
duration: "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud" | ||
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request" | ||
) | ||
|
||
type Partner interface { | ||
CreatePartnerAccount(ctx context.Context, r *request.CreatePartnerAccountRequest) (*upcloud.PartnerAccount, error) | ||
GetPartnerAccounts(ctx context.Context) ([]upcloud.PartnerAccount, error) | ||
} | ||
|
||
// CreatePartnerAccount creates new main account for partner | ||
func (s *Service) CreatePartnerAccount(ctx context.Context, r *request.CreatePartnerAccountRequest) (*upcloud.PartnerAccount, error) { | ||
partnerAccount := upcloud.PartnerAccount{} | ||
return &partnerAccount, s.create(ctx, r, &partnerAccount) | ||
} | ||
|
||
// GetPartnerAccounts lists accounts associated with partner | ||
func (s *Service) GetPartnerAccounts(ctx context.Context) ([]upcloud.PartnerAccount, error) { | ||
accounts := make([]upcloud.PartnerAccount, 0) | ||
return accounts, s.get(ctx, "/partner/accounts", &accounts) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud" | ||
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request" | ||
"github.com/dnaeon/go-vcr/recorder" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCreatePartnerAccount(t *testing.T) { | ||
record(t, "createpartneraccount", func(ctx context.Context, t *testing.T, rec *recorder.Recorder, svc *Service) { | ||
username := "sdk_test_partner_account" | ||
expectedAccount := upcloud.PartnerAccount{ | ||
Username: username, | ||
FirstName: "Test", | ||
LastName: "User", | ||
Country: "FIN", | ||
Email: "[email protected]", | ||
Phone: "+358.31245434", | ||
} | ||
|
||
account, err := svc.CreatePartnerAccount(ctx, &request.CreatePartnerAccountRequest{ | ||
Username: username, | ||
Password: "superSecret123", | ||
ContactDetails: &request.CreatePartnerAccountContactDetails{ | ||
FirstName: expectedAccount.FirstName, | ||
LastName: expectedAccount.LastName, | ||
Country: expectedAccount.Country, | ||
Email: expectedAccount.Email, | ||
Phone: expectedAccount.Phone, | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
assert.Equal(t, expectedAccount, *account) | ||
|
||
accounts, err := svc.GetPartnerAccounts(ctx) | ||
require.NoError(t, err) | ||
accountFound := false | ||
for _, a := range accounts { | ||
if a.Username == username { | ||
accountFound = true | ||
assert.Equal(t, expectedAccount, a) | ||
} | ||
} | ||
assert.True(t, accountFound) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters