From ed9740a137deb7c2cd25cf0b2de762a3e0374f8f Mon Sep 17 00:00:00 2001 From: Anirudh Oppiliappan Date: Thu, 8 May 2025 11:00:59 +0300 Subject: [PATCH] appview: xrpcclient: init wrapper xrpc client --- appview/xrpcclient/xrpc.go | 80 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 appview/xrpcclient/xrpc.go diff --git a/appview/xrpcclient/xrpc.go b/appview/xrpcclient/xrpc.go new file mode 100644 index 00000000..812a08e6 --- /dev/null +++ b/appview/xrpcclient/xrpc.go @@ -0,0 +1,80 @@ +package xrpcclient + +import ( + "bytes" + "context" + "io" + + "github.com/bluesky-social/indigo/api/atproto" + "github.com/bluesky-social/indigo/xrpc" + oauth "github.com/haileyok/atproto-oauth-golang" +) + +type Client struct { + *oauth.XrpcClient + authArgs *oauth.XrpcAuthedRequestArgs +} + +func NewClient(client *oauth.XrpcClient, authArgs *oauth.XrpcAuthedRequestArgs) *Client { + return &Client{ + XrpcClient: client, + authArgs: authArgs, + } +} + +func (c *Client) RepoPutRecord(ctx context.Context, input *atproto.RepoPutRecord_Input) (*atproto.RepoPutRecord_Output, error) { + var out atproto.RepoPutRecord_Output + if err := c.Do(ctx, c.authArgs, xrpc.Procedure, "application/json", "com.atproto.repo.putRecord", nil, input, &out); err != nil { + return nil, err + } + + return &out, nil +} + +func (c *Client) RepoGetRecord(ctx context.Context, cid string, collection string, repo string, rkey string) (*atproto.RepoGetRecord_Output, error) { + var out atproto.RepoGetRecord_Output + + params := map[string]interface{}{ + "cid": cid, + "collection": collection, + "repo": repo, + "rkey": rkey, + } + if err := c.Do(ctx, c.authArgs, xrpc.Query, "", "com.atproto.repo.getRecord", params, nil, &out); err != nil { + return nil, err + } + + return &out, nil +} + +func (c *Client) RepoUploadBlob(ctx context.Context, input io.Reader) (*atproto.RepoUploadBlob_Output, error) { + var out atproto.RepoUploadBlob_Output + if err := c.Do(ctx, c.authArgs, xrpc.Procedure, "*/*", "com.atproto.repo.uploadBlob", nil, input, &out); err != nil { + return nil, err + } + + return &out, nil +} + +func (c *Client) SyncGetBlob(ctx context.Context, cid string, did string) ([]byte, error) { + buf := new(bytes.Buffer) + + params := map[string]interface{}{ + "cid": cid, + "did": did, + } + if err := c.Do(ctx, c.authArgs, xrpc.Query, "", "com.atproto.sync.getBlob", params, nil, buf); err != nil { + return nil, err + } + + return buf.Bytes(), nil +} + +func (c *Client) RepoDeleteRecord(ctx context.Context, input *atproto.RepoDeleteRecord_Input) (*atproto.RepoDeleteRecord_Output, error) { + var out atproto.RepoDeleteRecord_Output + if err := c.Do(ctx, c.authArgs, xrpc.Procedure, "application/json", "com.atproto.repo.deleteRecord", nil, input, &out); err != nil { + return nil, err + } + + return &out, nil +} -- 2.51.2