Skip to content

Commit bfbdb82

Browse files
committed
oauth2.go: added TokenCredential simplified
ResolveURL esign.go: added ClickV1 definition fixed url problems in v2.1 and added tests
1 parent 616ff5d commit bfbdb82

File tree

22 files changed

+502
-434
lines changed

22 files changed

+502
-434
lines changed

esign.go

+20-7
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,19 @@ var ErrNilOp = errors.New("nil operation")
2525

2626
// VersionV21 indicates that the url will resolve
2727
// to /restapi/v2.1
28-
var VersionV21 = &APIVersion{Prefix: "v2.1"}
28+
var VersionV21 = &APIVersion{Version: "v2.1"}
29+
30+
// ClickV1 defines url replacement for clickraps api
31+
var ClickV1 = &APIVersion{
32+
Version: "v1",
33+
Prefix: "clickapi",
34+
}
2935

3036
// APIVersion defines the prefix used to resolve an operation's url. If
3137
// nil or blank, "v2" is assumed.
3238
type APIVersion struct {
33-
Prefix string
39+
Version string
40+
Prefix string
3441
}
3542

3643
// ResolveDSURL updates the passed *url.URL's settings.
@@ -39,14 +46,20 @@ func (v *APIVersion) ResolveDSURL(u *url.URL, host string, accountID string) *ur
3946
newURL := *u
4047
newURL.Scheme = "https"
4148
newURL.Host = host
42-
var prefix = "v2"
43-
if v != nil && v.Prefix != "" {
44-
prefix = v.Prefix
49+
var prefix = "/restapi"
50+
var version = "/v2" // default is v2 restapi
51+
if v != nil {
52+
if v.Prefix != "" {
53+
prefix = "/" + v.Prefix
54+
}
55+
if v.Version != "" {
56+
version = "/" + v.Version
57+
}
4558
}
4659
if strings.HasPrefix(u.Path, "/") {
47-
newURL.Path = "/restapi" + u.Path
60+
newURL.Path = prefix + u.Path
4861
} else {
49-
newURL.Path = "/restapi/" + prefix + "/accounts/" + accountID + "/" + u.Path
62+
newURL.Path = prefix + version + "/accounts/" + accountID + "/" + u.Path
5063
}
5164
return &newURL
5265
}

esign_test.go

+43-61
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"errors"
1212
"fmt"
1313
"io"
14-
"log"
14+
"io/ioutil"
1515
"mime"
1616
"mime/multipart"
1717
"net/http"
@@ -24,8 +24,8 @@ import (
2424

2525
"github.com/jfcote87/ctxclient"
2626
"github.com/jfcote87/esign"
27-
"github.com/jfcote87/esign/v2/folders"
28-
"github.com/jfcote87/esign/v2/templates"
27+
"github.com/jfcote87/esign/v2.1/folders"
28+
"github.com/jfcote87/esign/v2.1/templates"
2929
"github.com/jfcote87/oauth2"
3030
"github.com/jfcote87/testutils"
3131
)
@@ -117,7 +117,7 @@ func TestOp_Do(t *testing.T) {
117117
Path: strings.Join([]string{"do", "test0", "testvar1", "go"}, "/"),
118118
QueryOpts: make(url.Values),
119119
Method: "GET",
120-
Version: &esign.APIVersion{Prefix: "v2.1"},
120+
Version: esign.VersionV21,
121121
},
122122
{
123123
Credential: cx,
@@ -519,75 +519,33 @@ type TokenCache struct {
519519
User *esign.UserInfo `json:"user_info"`
520520
}
521521

522-
func TestGeneratedOpss(t *testing.T) {
523-
jwtConfigJSON, ok := os.LookupEnv("DOCUSIGN_JWTConfig")
524-
if !ok {
525-
t.Skip()
526-
}
527-
f, err := os.Open(jwtConfigJSON)
522+
// TestGenerateOps creates an OAuth2Credential using environment
523+
// variables DOCUSIGN_Token DOCUSIGN_AcccountID and or
524+
// DOCUSIGN_JWTConfig and DOCUSIGN_JWTAPIUser. If neither of these
525+
// variables are set, skip the test.
526+
func TestGeneratedOps(t *testing.T) {
527+
cred, err := getLocalCredential()
528528
if err != nil {
529-
t.Fatalf("%s open: %v", jwtConfigJSON, err)
529+
t.Errorf("%v", err)
530+
return
530531
}
531-
var cfg *esign.JWTConfig
532-
if err = json.NewDecoder(f).Decode(&cfg); err != nil {
533-
f.Close()
534-
t.Fatalf("%v", err)
535-
}
536-
f.Close()
537-
jwtAPIUserName, ok := os.LookupEnv("DOCUSIGN_JWTAPIUser")
538-
if !ok {
532+
if cred == nil {
539533
t.Skip()
540534
}
541-
var cacheFunc func(context.Context, oauth2.Token, esign.UserInfo)
542-
var tokenCache *TokenCache
543-
if jwtConfigCache, ok := os.LookupEnv("DOCUSIGN_CachedToken"); ok {
544-
cacheFunc = func(ctx context.Context, tk oauth2.Token, ui esign.UserInfo) {
545-
f, err := os.Create(jwtConfigCache)
546-
if err != nil {
547-
return
548-
}
549-
json.NewEncoder(f).Encode(TokenCache{&tk, &ui})
550-
f.Close()
551-
}
552-
fr, err := os.Open(jwtConfigCache)
553-
if err == nil {
554-
if err := json.NewDecoder(fr).Decode(&tokenCache); err != nil {
555-
t.Errorf("decode token cache %s: %v", jwtConfigCache, err)
556-
tokenCache = nil
557-
}
558-
fr.Close()
559-
}
560-
}
561-
562-
cfg.CacheFunc = cacheFunc
563-
var tk *oauth2.Token
564-
var uInfo *esign.UserInfo
565-
if tokenCache != nil {
566-
uInfo = tokenCache.User
567-
tk = tokenCache.Token
568-
}
569-
cred, err := cfg.Credential(jwtAPIUserName, tk, uInfo)
570-
if err != nil {
571-
log.Fatalf("jwt credential: %v", err)
572-
}
573-
574-
u, err := cred.UserInfo(context.Background())
575-
if err != nil {
576-
t.Fatalf("Userinfo: %v", err)
577-
}
578-
_ = u
579-
535+
ctx := context.Background()
536+
// Read throught all folders
580537
sv := folders.New(cred)
581-
l, err := sv.List().Do(context.Background())
538+
l, err := sv.List().Do(ctx)
582539
if err != nil {
583540
t.Errorf("List: %v", err)
541+
return
584542
}
585543
if len(l.Folders) < 1 {
586544
t.Errorf("expecting multiple folders")
587545
}
588546

547+
// Read through all templates
589548
svT := templates.New(cred)
590-
591549
tList, err := svT.List().Do(context.Background())
592550
if err != nil {
593551
t.Errorf("Template List: %v", err)
@@ -600,7 +558,31 @@ func TestGeneratedOpss(t *testing.T) {
600558
t.Errorf("unable to open template %s: %v", tmpl.Name, err)
601559
continue
602560
}
603-
t.Logf("Got: %s", tx.EnvelopeTemplateDefinition.TemplateID)
561+
t.Logf("Got: %s", tx.TemplateID)
562+
}
563+
}
604564

565+
func getLocalCredential() (*esign.OAuth2Credential, error) {
566+
if tk, ok := os.LookupEnv("DOCUSIGN_Token"); ok {
567+
acctID, _ := os.LookupEnv("DOCUSIGN_AccountID")
568+
return esign.TokenCredential(tk, true).WithAccountID(acctID), nil
569+
}
570+
571+
if jwtConfigJSON, ok := os.LookupEnv("DOCUSIGN_JWTConfig"); ok {
572+
jwtAPIUserName, ok := os.LookupEnv("DOCUSIGN_JWTAPIUser")
573+
if !ok {
574+
return nil, fmt.Errorf("expected DOCUSIGN_JWTAPIUser environment variable with DOCUSIGN_JWTConfig=%s", jwtConfigJSON)
575+
}
576+
577+
buffer, err := ioutil.ReadFile(jwtConfigJSON)
578+
if err != nil {
579+
return nil, fmt.Errorf("%s open: %v", jwtConfigJSON, err)
580+
}
581+
var cfg *esign.JWTConfig
582+
if err = json.Unmarshal(buffer, &cfg); err != nil {
583+
return nil, fmt.Errorf("%v", err)
584+
}
585+
return cfg.Credential(jwtAPIUserName, nil, nil)
605586
}
587+
return nil, nil
606588
}

gen-esign/swagger.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -409,19 +409,21 @@ func (o Operation) CommentLines(funcName string, docPrefix string, hasFileUpload
409409

410410
// OpPath removes the accountId prefix for the op path. Allows
411411
// for credential to fill in.
412-
func (o Operation) OpPath() string {
413-
if strings.HasPrefix(o.Path, "/v2/accounts/{accountId}") {
414-
if len(o.Path) < 25 {
412+
func (o Operation) OpPath(ver string) string {
413+
stdPrefix := "/" + ver + "/accounts/{accountId}/"
414+
stdPrefixLen := len(stdPrefix)
415+
if strings.HasPrefix(o.Path, stdPrefix) { //o.Path, "/" + ver + "/accounts/{accountId}") {
416+
if len(o.Path) < stdPrefixLen {
415417
return ""
416418
}
417-
return o.Path[25:]
419+
return o.Path[stdPrefixLen:]
418420
}
419421
return o.Path
420422
}
421423

422424
// OpPath2 creates a replacement string
423-
func (o Operation) OpPath2(p []PathParam) string {
424-
path := o.OpPath()
425+
func (o Operation) OpPath2(ver string, p []PathParam) string {
426+
path := o.OpPath(ver)
425427
if len(p) > 0 {
426428
parts := make([]string, 0)
427429
for _, part := range strings.Split(path, "/") {

gen-esign/templates/service.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func New(cred esign.Credential) *Service {
4242
return &{{.FuncName}}Op{
4343
Credential: s.credential,
4444
Method: "{{.HTTPMethod}}",
45-
Path: {{.OpPath2 .PathParams}},
45+
Path: {{.OpPath2 $verPrefix .PathParams}},
4646
{{if .OpPayload}}Payload: {{if .IsMediaUpload}}&esign.UploadFile{ Reader: media, ContentType: mimeType }{{else}}{{.OpPayload.GoName}}{{end}},
4747
{{end}}{{if .HasUploads}}Files: uploads,
4848
{{end}}{{if len .Produces}}Accept: "{{.Accepts}}",

0 commit comments

Comments
 (0)