this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

lexgen: use lexutil.LexClient interface for codegen (#1070)

Instead of referring directly to `xrpc.Client` (and the `Do()` method on
that type) in lexgen output, define a new client interface with roughly
the same function signature. The way this is done, the `xrpc.Client`
implementation is basically untouched, so any existing code depending
on, including in other git repos, it should not be impacted.

The motivation for this is to allow alternative client implementations.
For example, @haileyok has an OAuth client
(https://github.com/haileyok/atproto-oauth-golang), and i'm working on a
flexible SDK client.

This PR doesn't include an actual lexgen run. There are a bunch of
unrelated changes when I do lexgen right now (see
https://github.com/bluesky-social/indigo/pull/1069), and it is easier to
review without all the (many!) changes). However, everything "Just
Works" with both old and new lexgen output. This PR should be mergable
as-is.

I did try a lexgen run and everything worked as expected. I also
manually updated the "record type agnostic" helper code, which gives a
feel for what the full lexgen diff will look like.

authored by

bnewbold and committed by
GitHub
2c892757 d5c9745a

+61 -43
+3 -3
api/agnostic/actorgetPreferences.go
··· 7 7 import ( 8 8 "context" 9 9 10 - "github.com/bluesky-social/indigo/xrpc" 10 + "github.com/bluesky-social/indigo/lex/util" 11 11 ) 12 12 13 13 // ActorGetPreferences_Output is the output of a app.bsky.actor.getPreferences call. ··· 16 16 } 17 17 18 18 // ActorGetPreferences calls the XRPC method "app.bsky.actor.getPreferences". 19 - func ActorGetPreferences(ctx context.Context, c *xrpc.Client) (*ActorGetPreferences_Output, error) { 19 + func ActorGetPreferences(ctx context.Context, c util.LexClient) (*ActorGetPreferences_Output, error) { 20 20 var out ActorGetPreferences_Output 21 21 22 22 params := map[string]interface{}{} 23 - if err := c.Do(ctx, xrpc.Query, "", "app.bsky.actor.getPreferences", params, nil, &out); err != nil { 23 + if err := c.LexDo(ctx, util.Query, "", "app.bsky.actor.getPreferences", params, nil, &out); err != nil { 24 24 return nil, err 25 25 } 26 26
+3 -3
api/agnostic/actorputPreferences.go
··· 7 7 import ( 8 8 "context" 9 9 10 - "github.com/bluesky-social/indigo/xrpc" 10 + "github.com/bluesky-social/indigo/lex/util" 11 11 ) 12 12 13 13 // ActorPutPreferences_Input is the input argument to a app.bsky.actor.putPreferences call. ··· 16 16 } 17 17 18 18 // ActorPutPreferences calls the XRPC method "app.bsky.actor.putPreferences". 19 - func ActorPutPreferences(ctx context.Context, c *xrpc.Client, input *ActorPutPreferences_Input) error { 20 - if err := c.Do(ctx, xrpc.Procedure, "application/json", "app.bsky.actor.putPreferences", nil, input, nil); err != nil { 19 + func ActorPutPreferences(ctx context.Context, c util.LexClient, input *ActorPutPreferences_Input) error { 20 + if err := c.LexDo(ctx, util.Procedure, "application/json", "app.bsky.actor.putPreferences", nil, input, nil); err != nil { 21 21 return err 22 22 } 23 23
+3 -3
api/agnostic/identitygetRecommendedDidCredentials.go
··· 8 8 "context" 9 9 "encoding/json" 10 10 11 - "github.com/bluesky-social/indigo/xrpc" 11 + "github.com/bluesky-social/indigo/lex/util" 12 12 ) 13 13 14 14 // IdentityGetRecommendedDidCredentials calls the XRPC method "com.atproto.identity.getRecommendedDidCredentials". 15 - func IdentityGetRecommendedDidCredentials(ctx context.Context, c *xrpc.Client) (*json.RawMessage, error) { 15 + func IdentityGetRecommendedDidCredentials(ctx context.Context, c util.LexClient) (*json.RawMessage, error) { 16 16 var out json.RawMessage 17 17 18 - if err := c.Do(ctx, xrpc.Query, "", "com.atproto.identity.getRecommendedDidCredentials", nil, nil, &out); err != nil { 18 + if err := c.LexDo(ctx, util.Query, "", "com.atproto.identity.getRecommendedDidCredentials", nil, nil, &out); err != nil { 19 19 return nil, err 20 20 } 21 21
+3 -3
api/agnostic/identitysignPlcOperation.go
··· 8 8 "context" 9 9 "encoding/json" 10 10 11 - "github.com/bluesky-social/indigo/xrpc" 11 + "github.com/bluesky-social/indigo/lex/util" 12 12 ) 13 13 14 14 // IdentitySignPlcOperation_Input is the input argument to a com.atproto.identity.signPlcOperation call. ··· 28 28 } 29 29 30 30 // IdentitySignPlcOperation calls the XRPC method "com.atproto.identity.signPlcOperation". 31 - func IdentitySignPlcOperation(ctx context.Context, c *xrpc.Client, input *IdentitySignPlcOperation_Input) (*IdentitySignPlcOperation_Output, error) { 31 + func IdentitySignPlcOperation(ctx context.Context, c util.LexClient, input *IdentitySignPlcOperation_Input) (*IdentitySignPlcOperation_Output, error) { 32 32 var out IdentitySignPlcOperation_Output 33 - if err := c.Do(ctx, xrpc.Procedure, "application/json", "com.atproto.identity.signPlcOperation", nil, input, &out); err != nil { 33 + if err := c.LexDo(ctx, util.Procedure, "application/json", "com.atproto.identity.signPlcOperation", nil, input, &out); err != nil { 34 34 return nil, err 35 35 } 36 36
+3 -3
api/agnostic/identitysubmitPlcOperation.go
··· 8 8 "context" 9 9 "encoding/json" 10 10 11 - "github.com/bluesky-social/indigo/xrpc" 11 + "github.com/bluesky-social/indigo/lex/util" 12 12 ) 13 13 14 14 // IdentitySubmitPlcOperation_Input is the input argument to a com.atproto.identity.submitPlcOperation call. ··· 17 17 } 18 18 19 19 // IdentitySubmitPlcOperation calls the XRPC method "com.atproto.identity.submitPlcOperation". 20 - func IdentitySubmitPlcOperation(ctx context.Context, c *xrpc.Client, input *IdentitySubmitPlcOperation_Input) error { 21 - if err := c.Do(ctx, xrpc.Procedure, "application/json", "com.atproto.identity.submitPlcOperation", nil, input, nil); err != nil { 20 + func IdentitySubmitPlcOperation(ctx context.Context, c util.LexClient, input *IdentitySubmitPlcOperation_Input) error { 21 + if err := c.LexDo(ctx, util.Procedure, "application/json", "com.atproto.identity.submitPlcOperation", nil, input, nil); err != nil { 22 22 return err 23 23 } 24 24
+2 -3
api/agnostic/repoapplyWrites.go
··· 10 10 "fmt" 11 11 12 12 "github.com/bluesky-social/indigo/lex/util" 13 - "github.com/bluesky-social/indigo/xrpc" 14 13 ) 15 14 16 15 // RepoApplyWrites_Create is a "create" in the com.atproto.repo.applyWrites schema. ··· 179 178 } 180 179 181 180 // RepoApplyWrites calls the XRPC method "com.atproto.repo.applyWrites". 182 - func RepoApplyWrites(ctx context.Context, c *xrpc.Client, input *RepoApplyWrites_Input) (*RepoApplyWrites_Output, error) { 181 + func RepoApplyWrites(ctx context.Context, c util.LexClient, input *RepoApplyWrites_Input) (*RepoApplyWrites_Output, error) { 183 182 var out RepoApplyWrites_Output 184 - if err := c.Do(ctx, xrpc.Procedure, "application/json", "com.atproto.repo.applyWrites", nil, input, &out); err != nil { 183 + if err := c.LexDo(ctx, util.Procedure, "application/json", "com.atproto.repo.applyWrites", nil, input, &out); err != nil { 185 184 return nil, err 186 185 } 187 186
+3 -3
api/agnostic/repocreateRecord.go
··· 7 7 import ( 8 8 "context" 9 9 10 - "github.com/bluesky-social/indigo/xrpc" 10 + "github.com/bluesky-social/indigo/lex/util" 11 11 ) 12 12 13 13 // RepoDefs_CommitMeta is a "commitMeta" in the com.atproto.repo.defs schema. ··· 41 41 } 42 42 43 43 // RepoCreateRecord calls the XRPC method "com.atproto.repo.createRecord". 44 - func RepoCreateRecord(ctx context.Context, c *xrpc.Client, input *RepoCreateRecord_Input) (*RepoCreateRecord_Output, error) { 44 + func RepoCreateRecord(ctx context.Context, c util.LexClient, input *RepoCreateRecord_Input) (*RepoCreateRecord_Output, error) { 45 45 var out RepoCreateRecord_Output 46 - if err := c.Do(ctx, xrpc.Procedure, "application/json", "com.atproto.repo.createRecord", nil, input, &out); err != nil { 46 + if err := c.LexDo(ctx, util.Procedure, "application/json", "com.atproto.repo.createRecord", nil, input, &out); err != nil { 47 47 return nil, err 48 48 } 49 49
+3 -3
api/agnostic/repogetRecord.go
··· 8 8 "context" 9 9 "encoding/json" 10 10 11 - "github.com/bluesky-social/indigo/xrpc" 11 + "github.com/bluesky-social/indigo/lex/util" 12 12 ) 13 13 14 14 // RepoGetRecord_Output is the output of a com.atproto.repo.getRecord call. ··· 25 25 // collection: The NSID of the record collection. 26 26 // repo: The handle or DID of the repo. 27 27 // rkey: The Record Key. 28 - func RepoGetRecord(ctx context.Context, c *xrpc.Client, cid string, collection string, repo string, rkey string) (*RepoGetRecord_Output, error) { 28 + func RepoGetRecord(ctx context.Context, c util.LexClient, cid string, collection string, repo string, rkey string) (*RepoGetRecord_Output, error) { 29 29 var out RepoGetRecord_Output 30 30 31 31 params := map[string]interface{}{ ··· 34 34 "repo": repo, 35 35 "rkey": rkey, 36 36 } 37 - if err := c.Do(ctx, xrpc.Query, "", "com.atproto.repo.getRecord", params, nil, &out); err != nil { 37 + if err := c.LexDo(ctx, util.Query, "", "com.atproto.repo.getRecord", params, nil, &out); err != nil { 38 38 return nil, err 39 39 } 40 40
+3 -3
api/agnostic/repolistRecords.go
··· 8 8 "context" 9 9 "encoding/json" 10 10 11 - "github.com/bluesky-social/indigo/xrpc" 11 + "github.com/bluesky-social/indigo/lex/util" 12 12 ) 13 13 14 14 // RepoListRecords_Output is the output of a com.atproto.repo.listRecords call. ··· 31 31 // limit: The number of records to return. 32 32 // repo: The handle or DID of the repo. 33 33 // reverse: Flag to reverse the order of the returned records. 34 - func RepoListRecords(ctx context.Context, c *xrpc.Client, collection string, cursor string, limit int64, repo string, reverse bool) (*RepoListRecords_Output, error) { 34 + func RepoListRecords(ctx context.Context, c util.LexClient, collection string, cursor string, limit int64, repo string, reverse bool) (*RepoListRecords_Output, error) { 35 35 var out RepoListRecords_Output 36 36 37 37 params := map[string]interface{}{ ··· 41 41 "repo": repo, 42 42 "reverse": reverse, 43 43 } 44 - if err := c.Do(ctx, xrpc.Query, "", "com.atproto.repo.listRecords", params, nil, &out); err != nil { 44 + if err := c.LexDo(ctx, util.Query, "", "com.atproto.repo.listRecords", params, nil, &out); err != nil { 45 45 return nil, err 46 46 } 47 47
+3 -3
api/agnostic/repoputRecord.go
··· 7 7 import ( 8 8 "context" 9 9 10 - "github.com/bluesky-social/indigo/xrpc" 10 + "github.com/bluesky-social/indigo/lex/util" 11 11 ) 12 12 13 13 // RepoPutRecord_Input is the input argument to a com.atproto.repo.putRecord call. ··· 37 37 } 38 38 39 39 // RepoPutRecord calls the XRPC method "com.atproto.repo.putRecord". 40 - func RepoPutRecord(ctx context.Context, c *xrpc.Client, input *RepoPutRecord_Input) (*RepoPutRecord_Output, error) { 40 + func RepoPutRecord(ctx context.Context, c util.LexClient, input *RepoPutRecord_Input) (*RepoPutRecord_Output, error) { 41 41 var out RepoPutRecord_Output 42 - if err := c.Do(ctx, xrpc.Procedure, "application/json", "com.atproto.repo.putRecord", nil, input, &out); err != nil { 42 + if err := c.LexDo(ctx, util.Procedure, "application/json", "com.atproto.repo.putRecord", nil, input, &out); err != nil { 43 43 return nil, err 44 44 } 45 45
-1
lex/gen.go
··· 135 135 pf("\t\"fmt\"\n") 136 136 pf("\t\"encoding/json\"\n") 137 137 pf("\tcbg \"github.com/whyrusleeping/cbor-gen\"\n") 138 - pf("\t\"github.com/bluesky-social/indigo/xrpc\"\n") 139 138 pf("\t\"github.com/bluesky-social/indigo/lex/util\"\n") 140 139 for _, xpkg := range packages { 141 140 if xpkg.Prefix != pkg.Prefix {
+4 -4
lex/type_schema.go
··· 54 54 pf := printerf(w) 55 55 fname := typename 56 56 57 - params := "ctx context.Context, c *xrpc.Client" 57 + params := "ctx context.Context, c util.LexClient" 58 58 inpvar := "nil" 59 59 inpenc := "" 60 60 ··· 161 161 var reqtype string 162 162 switch s.Type { 163 163 case "procedure": 164 - reqtype = "xrpc.Procedure" 164 + reqtype = "util.Procedure" 165 165 case "query": 166 - reqtype = "xrpc.Query" 166 + reqtype = "util.Query" 167 167 default: 168 168 return fmt.Errorf("can only generate RPC for Query or Procedure (got %s)", s.Type) 169 169 } 170 170 171 - pf("\tif err := c.Do(ctx, %s, %q, \"%s\", %s, %s, %s); err != nil {\n", reqtype, inpenc, s.id, queryparams, inpvar, outvar) 171 + pf("\tif err := c.LexDo(ctx, %s, %q, \"%s\", %s, %s, %s); err != nil {\n", reqtype, inpenc, s.id, queryparams, inpvar, outvar) 172 172 pf("\t\treturn %s\n", errRet) 173 173 pf("\t}\n\n") 174 174 pf("\treturn %s\n", outRet)
+18
lex/util/client.go
··· 1 + package util 2 + 3 + import ( 4 + "context" 5 + "net/http" 6 + ) 7 + 8 + const ( 9 + Query = http.MethodGet 10 + Procedure = http.MethodPost 11 + ) 12 + 13 + // API client interface used in lexgen. 14 + // 15 + // 'method' is the HTTP method type. 'inputEncoding' is the Content-Type for bodyData in Procedure calls. 'params' are query parameters. 'bodyData' should be either 'nil', an [io.Reader], or a type which can be marshalled to JSON. 'out' is optional; if not nil it should be a pointer to a type which can be un-Marshaled as JSON, for the response body. 16 + type LexClient interface { 17 + LexDo(ctx context.Context, method string, inputEncoding string, endpoint string, params map[string]any, bodyData any, out any) error 18 + }
+10 -8
xrpc/xrpc.go
··· 34 34 return c.Client 35 35 } 36 36 37 - type XRPCRequestType int 37 + var ( 38 + Query = http.MethodGet 39 + Procedure = http.MethodPost 40 + ) 38 41 39 42 type AuthInfo struct { 40 43 AccessJwt string `json:"accessJwt"` ··· 110 113 Reset time.Time 111 114 } 112 115 113 - const ( 114 - Query = XRPCRequestType(iota) 115 - Procedure 116 - ) 117 - 118 116 // makeParams converts a map of string keys and any values into a URL-encoded string. 119 117 // If a value is a slice of strings, it will be joined with commas. 120 118 // Generally the values will be strings, numbers, booleans, or slices of strings ··· 133 131 return params.Encode() 134 132 } 135 133 136 - func (c *Client) Do(ctx context.Context, kind XRPCRequestType, inpenc string, method string, params map[string]interface{}, bodyobj interface{}, out interface{}) error { 134 + func (c *Client) Do(ctx context.Context, kind string, inpenc string, method string, params map[string]interface{}, bodyobj interface{}, out interface{}) error { 137 135 var body io.Reader 138 136 if bodyobj != nil { 139 137 if rr, ok := bodyobj.(io.Reader); ok { ··· 155 153 case Procedure: 156 154 m = "POST" 157 155 default: 158 - return fmt.Errorf("unsupported request kind: %d", kind) 156 + return fmt.Errorf("unsupported request kind: %s", kind) 159 157 } 160 158 161 159 var paramStr string ··· 227 225 228 226 return nil 229 227 } 228 + 229 + func (c *Client) LexDo(ctx context.Context, method string, inputEncoding string, endpoint string, params map[string]any, bodyData any, out any) error { 230 + return c.Do(ctx, method, inputEncoding, endpoint, params, bodyData, out) 231 + }