@@ -16,6 +16,7 @@ import (
1616
1717 "github.com/anchordotdev/cli"
1818 "github.com/anchordotdev/cli/keyring"
19+ "github.com/anchordotdev/cli/version"
1920 "golang.org/x/exp/slices"
2021)
2122
@@ -38,13 +39,16 @@ type Session struct {
3839}
3940
4041// TODO: rename to NewSession
41- func NewClient (cfg * cli.Config ) (* Session , error ) {
42+ func NewClient (ctx context. Context , cfg * cli.Config ) (* Session , error ) {
4243 anc := & Session {
4344 Client : & http.Client {
4445 Transport : urlRewriter {
4546 RoundTripper : responseChecker {
4647 RoundTripper : userAgentSetter {
47- RoundTripper : new (http.Transport ),
48+ RoundTripper : preferSetter {
49+ cfg : cfg ,
50+ RoundTripper : new (http.Transport ),
51+ },
4852 },
4953 },
5054 URL : cfg .API .URL ,
@@ -81,6 +85,12 @@ func NewClient(cfg *cli.Config) (*Session, error) {
8185 PAT : apiToken ,
8286 }
8387
88+ if info , err := anc .UserInfo (ctx ); err == nil {
89+ if err := version .MinimumVersionCheck (info .MinimumCliVersion ); err != nil {
90+ return nil , err
91+ }
92+ }
93+
8494 return anc , nil
8595}
8696
@@ -102,6 +112,18 @@ func (s *Session) AttachService(ctx context.Context, chainSlug string, domains [
102112 return & attachOutput , nil
103113}
104114
115+ func getServiceAttachmentsPath (orgAPID , serviceAPID string ) string {
116+ return fmt .Sprintf ("/orgs/%s/services/%s/attachments" , url .QueryEscape (orgAPID ), url .QueryEscape (serviceAPID ))
117+ }
118+
119+ func (s * Session ) GetServiceAttachments (ctx context.Context , orgAPID string , serviceAPID string ) ([]Attachment , error ) {
120+ var attachments Attachments
121+ if err := s .get (ctx , getServiceAttachmentsPath (orgAPID , serviceAPID ), & attachments ); err != nil {
122+ return nil , err
123+ }
124+ return attachments .Items , nil
125+ }
126+
105127func (s * Session ) CreatePATToken (ctx context.Context , deviceCode string ) (string , error ) {
106128 reqBody := CreateCliTokenJSONRequestBody {
107129 DeviceCode : deviceCode ,
@@ -159,7 +181,9 @@ func (s *Session) CreateEAB(ctx context.Context, chainSlug, orgSlug, realmSlug,
159181 eabInput .Relationships .Chain .Slug = chainSlug
160182 eabInput .Relationships .Organization .Slug = orgSlug
161183 eabInput .Relationships .Realm .Slug = realmSlug
162- eabInput .Relationships .Service .Slug = & serviceSlug
184+ eabInput .Relationships .Service = & RelationshipsServiceSlug {
185+ Slug : serviceSlug ,
186+ }
163187 eabInput .Relationships .SubCa .Slug = subCASlug
164188
165189 var eabOutput Eab
@@ -223,6 +247,26 @@ func (s *Session) GenerateUserFlowCodes(ctx context.Context, source string) (*Au
223247 return & codes , nil
224248}
225249
250+ func (s * Session ) GetOrgs (ctx context.Context ) ([]Organization , error ) {
251+ var orgs Organizations
252+ if err := s .get (ctx , "/orgs" , & orgs ); err != nil {
253+ return nil , err
254+ }
255+ return orgs .Items , nil
256+ }
257+
258+ func getOrgRealmsPath (orgApid string ) string {
259+ return "/orgs/" + url .QueryEscape (orgApid ) + "/realms"
260+ }
261+
262+ func (s * Session ) GetOrgRealms (ctx context.Context , orgApid string ) ([]Realm , error ) {
263+ var realms Realms
264+ if err := s .get (ctx , getOrgRealmsPath (orgApid ), & realms ); err != nil {
265+ return nil , err
266+ }
267+ return realms .Items , nil
268+ }
269+
226270func getOrgServicesPath (orgSlug string ) string {
227271 return "/orgs/" + url .QueryEscape (orgSlug ) + "/services"
228272}
@@ -362,6 +406,36 @@ func (r urlRewriter) RoundTrip(req *http.Request) (*http.Response, error) {
362406 return r .RoundTripper .RoundTrip (req )
363407}
364408
409+ type preferSetter struct {
410+ http.RoundTripper
411+
412+ cfg * cli.Config
413+ }
414+
415+ func (s preferSetter ) RoundTrip (req * http.Request ) (* http.Response , error ) {
416+ path := req .URL .Path
417+
418+ var value []string
419+
420+ if s .cfg .Test .Prefer [path ].Code != 0 {
421+ value = append (value , fmt .Sprintf ("code=%d" , s .cfg .Test .Prefer [path ].Code ))
422+ }
423+
424+ if s .cfg .Test .Prefer [path ].Dynamic {
425+ value = append (value , fmt .Sprintf ("dynamic=%t" , s .cfg .Test .Prefer [path ].Dynamic ))
426+ }
427+
428+ if s .cfg .Test .Prefer [path ].Example != "" {
429+ value = append (value , fmt .Sprintf ("example=%s" , s .cfg .Test .Prefer [path ].Example ))
430+ }
431+
432+ if len (value ) > 0 {
433+ req .Header .Set ("Prefer" , strings .Join (value , " " ))
434+ }
435+
436+ return s .RoundTripper .RoundTrip (req )
437+ }
438+
365439type userAgentSetter struct {
366440 http.RoundTripper
367441}
0 commit comments