-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes generated by 549624c0b07c73ac50b7117a9af1f2af70e6aa03
- Loading branch information
1 parent
f3b8118
commit f1c43ba
Showing
38 changed files
with
470 additions
and
93 deletions.
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
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
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,137 @@ | ||
package gocardless | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/google/go-querystring/query" | ||
) | ||
|
||
var _ = query.Values | ||
var _ = bytes.NewBuffer | ||
var _ = json.NewDecoder | ||
var _ = errors.New | ||
|
||
// LogoService manages logos | ||
type LogoServiceImpl struct { | ||
config Config | ||
} | ||
|
||
// Logo model | ||
type Logo struct { | ||
Id string `url:"id,omitempty" json:"id,omitempty"` | ||
} | ||
|
||
type LogoService interface { | ||
CreateForCreditor(ctx context.Context, p LogoCreateForCreditorParams, opts ...RequestOption) (*Logo, error) | ||
} | ||
|
||
type LogoCreateForCreditorParamsLinks struct { | ||
Creditor string `url:"creditor,omitempty" json:"creditor,omitempty"` | ||
} | ||
|
||
// LogoCreateForCreditorParams parameters | ||
type LogoCreateForCreditorParams struct { | ||
Image string `url:"image,omitempty" json:"image,omitempty"` | ||
Links *LogoCreateForCreditorParamsLinks `url:"links,omitempty" json:"links,omitempty"` | ||
} | ||
|
||
// CreateForCreditor | ||
// Creates a new logo associated with a creditor. If a creditor already has a | ||
// logo, this will update the existing logo linked to the creditor. | ||
func (s *LogoServiceImpl) CreateForCreditor(ctx context.Context, p LogoCreateForCreditorParams, opts ...RequestOption) (*Logo, error) { | ||
uri, err := url.Parse(fmt.Sprintf(s.config.Endpoint() + "/branding/logos")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
o := &requestOptions{ | ||
retries: 3, | ||
} | ||
for _, opt := range opts { | ||
err := opt(o) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
if o.idempotencyKey == "" { | ||
o.idempotencyKey = NewIdempotencyKey() | ||
} | ||
|
||
var body io.Reader | ||
|
||
var buf bytes.Buffer | ||
err = json.NewEncoder(&buf).Encode(map[string]interface{}{ | ||
"data": p, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
body = &buf | ||
|
||
req, err := http.NewRequest("POST", uri.String(), body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req = req.WithContext(ctx) | ||
req.Header.Set("Authorization", "Bearer "+s.config.Token()) | ||
req.Header.Set("GoCardless-Version", "2015-07-06") | ||
req.Header.Set("GoCardless-Client-Library", "gocardless-pro-go") | ||
req.Header.Set("GoCardless-Client-Version", "3.9.0") | ||
req.Header.Set("User-Agent", userAgent) | ||
req.Header.Set("Content-Type", "application/json") | ||
req.Header.Set("Idempotency-Key", o.idempotencyKey) | ||
|
||
for key, value := range o.headers { | ||
req.Header.Set(key, value) | ||
} | ||
|
||
client := s.config.Client() | ||
if client == nil { | ||
client = http.DefaultClient | ||
} | ||
|
||
var result struct { | ||
Err *APIError `json:"error"` | ||
Logo *Logo `json:"logos"` | ||
} | ||
|
||
err = try(o.retries, func() error { | ||
res, err := client.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer res.Body.Close() | ||
|
||
err = responseErr(res) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = json.NewDecoder(res.Body).Decode(&result) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if result.Err != nil { | ||
return result.Err | ||
} | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if result.Logo == nil { | ||
return nil, errors.New("missing result") | ||
} | ||
|
||
return result.Logo, nil | ||
} |
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,34 @@ | ||
package gocardless | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
) | ||
|
||
func TestLogoCreateForCreditor(t *testing.T) { | ||
fixtureFile := "testdata/logos.json" | ||
server := runServer(t, fixtureFile, "create_for_creditor") | ||
defer server.Close() | ||
|
||
ctx := context.TODO() | ||
client, err := getClient(t, server.URL) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
p := LogoCreateForCreditorParams{} | ||
|
||
o, err := | ||
client.Logos.CreateForCreditor( | ||
ctx, p) | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if o == nil { | ||
|
||
t.Fatalf("Expected Logo, got nil") | ||
|
||
} | ||
} |
Oops, something went wrong.