Skip to content

Commit ce79975

Browse files
authored
api: add Client.NewHTTPRequest (#264)
I want to make authenticated requests against the Sourcegraph API which are not via GraphQL. In particular I am experimenting with Server Sent Events endpoint. This commit adds NewHTTPRequest to the Client interface, which allows access to an authenticated http.Request. This will then be used to communicate with the non-GraphQL endpoint.
1 parent 12fba59 commit ce79975

1 file changed

Lines changed: 25 additions & 14 deletions

File tree

internal/api/api.go

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Package api provides a basic client library for the Sourcegraph GraphQL API.
1+
// Package api provides a basic client library for the Sourcegraph API.
22
package api
33

44
import (
@@ -10,6 +10,7 @@ import (
1010
"io/ioutil"
1111
"net/http"
1212
"os"
13+
"path"
1314

1415
"github.com/hashicorp/go-multierror"
1516
"github.com/kballard/go-shellquote"
@@ -25,6 +26,12 @@ type Client interface {
2526

2627
// NewRequest creates a GraphQL request.
2728
NewRequest(query string, vars map[string]interface{}) Request
29+
30+
// NewHTTPRequest creates an http.Request for the Sourcegraph API.
31+
//
32+
// path is joined against the API route. For example on Sourcegraph.com this
33+
// will result the URL: https://sourcegraph.com/.api/path.
34+
NewHTTPRequest(ctx context.Context, method, path string, body io.Reader) (*http.Request, error)
2835
}
2936

3037
// Request instances represent GraphQL requests.
@@ -104,8 +111,21 @@ func (c *client) NewRequest(query string, vars map[string]interface{}) Request {
104111
}
105112
}
106113

107-
func (c *client) url() string {
108-
return c.opts.Endpoint + "/.api/graphql"
114+
func (c *client) NewHTTPRequest(ctx context.Context, method, p string, body io.Reader) (*http.Request, error) {
115+
req, err := http.NewRequestWithContext(ctx, method, c.opts.Endpoint+path.Join("/.api", p), body)
116+
if err != nil {
117+
return nil, err
118+
}
119+
if c.opts.AccessToken != "" {
120+
req.Header.Set("Authorization", "token "+c.opts.AccessToken)
121+
}
122+
if *c.opts.Flags.trace {
123+
req.Header.Set("X-Sourcegraph-Should-Trace", "true")
124+
}
125+
for k, v := range c.opts.AdditionalHeaders {
126+
req.Header.Set(k, v)
127+
}
128+
return req, nil
109129
}
110130

111131
func (r *request) do(ctx context.Context, result interface{}) (bool, error) {
@@ -128,19 +148,10 @@ func (r *request) do(ctx context.Context, result interface{}) (bool, error) {
128148
}
129149

130150
// Create the HTTP request.
131-
req, err := http.NewRequestWithContext(ctx, "POST", r.client.url(), bytes.NewBuffer(reqBody))
151+
req, err := r.client.NewHTTPRequest(ctx, "POST", "graphql", bytes.NewBuffer(reqBody))
132152
if err != nil {
133153
return false, err
134154
}
135-
if r.client.opts.AccessToken != "" {
136-
req.Header.Set("Authorization", "token "+r.client.opts.AccessToken)
137-
}
138-
if *r.client.opts.Flags.trace {
139-
req.Header.Set("X-Sourcegraph-Should-Trace", "true")
140-
}
141-
for k, v := range r.client.opts.AdditionalHeaders {
142-
req.Header.Set(k, v)
143-
}
144155

145156
// Perform the request.
146157
resp, err := http.DefaultClient.Do(req)
@@ -224,6 +235,6 @@ func (r *request) curlCmd() (string, error) {
224235
s += fmt.Sprintf(" %s \\\n", shellquote.Join("-H", k+": "+v))
225236
}
226237
s += fmt.Sprintf(" %s \\\n", shellquote.Join("-d", string(data)))
227-
s += fmt.Sprintf(" %s", shellquote.Join(r.client.url()))
238+
s += fmt.Sprintf(" %s", shellquote.Join(r.client.opts.Endpoint+"/.api/graphql"))
228239
return s, nil
229240
}

0 commit comments

Comments
 (0)