···13131414type APIClient struct {
1515 // inner HTTP client
1616- Client *http.Client
1616+ Client *http.Client
17171818 // host URL prefix: scheme, hostname, and port. This field is required.
1919- Host string
1919+ Host string
20202121 // optional auth client "middleware"
2222- Auth AuthMethod
2222+ Auth AuthMethod
23232424 // optional HTTP headers which will be included in all requests. Only a single value per key is included; request-level headers will override any client-level defaults.
2525- DefaultHeaders http.Header
2525+ Headers http.Header
2626+2727+ // optional authenticated account DID for this client. Does not change client behavior; this is included as a convenience for calling code, logging, etc.
2828+ AccountDID *syntax.DID
2929+}
3030+3131+func NewPublicClient(host string) *APIClient {
3232+ return &APIClient{
3333+ Client: http.DefaultClient,
3434+ Host: host,
3535+ Headers: map[string][]string{
3636+ "User-Agent": []string{"indigo-sdk"},
3737+ },
3838+ }
2639}
27402841// High-level helper for simple JSON "Query" API calls.
···110123//
111124// NOTE: this does not currently parse error response JSON body, thought it might in the future.
112125func (c *APIClient) Do(ctx context.Context, req APIRequest) (*http.Response, error) {
113113- httpReq, err := req.HTTPRequest(ctx, c.Host, c.DefaultHeaders)
126126+ httpReq, err := req.HTTPRequest(ctx, c.Host, c.Headers)
114127 if err != nil {
115128 return nil, err
116129 }
···132145 return resp, nil
133146}
134147135135-func NewPublicClient(host string) *APIClient {
136136- return &APIClient{
137137- Client: http.DefaultClient,
138138- Host: host,
139139- DefaultHeaders: map[string][]string{
140140- "User-Agent": []string{"indigo-sdk"},
141141- },
148148+// Returns a shallow copy of the APIClient with the provided service ref configured as a proxy header.
149149+//
150150+// To configure service proxying without creating a copy, simply set the "Atproto-Proxy" header.
151151+func (c *APIClient) WithService(ref string) *APIClient {
152152+ hdr := make(http.Header)
153153+ for k := range c.Headers {
154154+ for _, v := range c.Headers.Values(k) {
155155+ hdr.Add(k, v)
156156+ }
142157 }
158158+159159+ hdr.Set("Atproto-Proxy", ref)
160160+ out := APIClient{
161161+ Client: c.Client,
162162+ Host: c.Host,
163163+ Auth: c.Auth,
164164+ Headers: hdr,
165165+ AccountDID: c.AccountDID,
166166+ }
167167+ return &out
168168+}
169169+170170+// Configures labeler header (Atproto-Accept-Labelers) with the indicated "redact" level labelers, and regular labelers.
171171+func (c *APIClient) SetLabelers(redact, other []syntax.DID) {
172172+ val := ""
173173+ for _, did := range redact {
174174+ if val != "" {
175175+ val = val + ","
176176+ }
177177+ val = fmt.Sprintf("%s%s;redact", val, did.String())
178178+ }
179179+ for _, did := range other {
180180+ if val != "" {
181181+ val = val + ","
182182+ }
183183+ val = val + did.String()
184184+ }
185185+ c.Headers.Set("Atproto-Accept-Labelers", val)
143186}
+10-7
atproto/client/api_request.go
···2233import (
44 "context"
55- "io"
65 "fmt"
66+ "io"
77 "net/http"
88 "net/url"
99···20202121type APIRequest struct {
2222 // HTTP method, eg "GET" (required)
2323- Method string
2323+ Method string
24242525 // atproto API endpoint, as NSID (required)
2626- Endpoint syntax.NSID
2626+ Endpoint syntax.NSID
27272828 // optional request body. if this is provided, then 'Content-Type' header should be specified
2929- Body io.Reader
2929+ Body io.Reader
3030+3131+ // XXX:
3232+ //GetBody func() (io.ReadCloser, error)
30333134 // optional query parameters. These will be encoded as provided.
3235 QueryParams url.Values
33363437 // optional HTTP headers. Only the first value will be included for each header key ("Set" behavior).
3535- Headers http.Header
3838+ Headers http.Header
3639}
37403841// Turns the API request in to an `http.Request`.
···5457 return nil, fmt.Errorf("empty request endpoint")
5558 }
5659 u.Path = "/xrpc/" + r.Endpoint.String()
5757- u.RawQuery = nil
6060+ u.RawQuery = ""
5861 if r.QueryParams != nil {
5962 u.RawQuery = r.QueryParams.Encode()
6063 }
···7376 // ... then request-specific take priority (overwrite)
7477 if r.Headers != nil {
7578 for k := range r.Headers {
7676- httpReq.Header.Set(k, headers.Get(k))
7979+ httpReq.Header.Set(k, r.Headers.Get(k))
7780 }
7881 }
7982