github.com/diamondburned/arikawa/v2@v2.1.0/utils/httputil/httpdriver/driver.go (about) 1 // Package httpdriver provides interfaces and implementations of a simple HTTP 2 // client. 3 package httpdriver 4 5 import ( 6 "context" 7 "io" 8 "net/http" 9 "net/url" 10 ) 11 12 // NoContent is the status code for HTTP 204, or http.StatusNoContent. 13 const NoContent = 204 14 15 // Client is a generic interface used as an adapter allowing for custom HTTP 16 // client implementations, such as fasthttp. 17 type Client interface { 18 NewRequest(ctx context.Context, method, url string) (Request, error) 19 Do(req Request) (Response, error) 20 } 21 22 // Request is a generic interface for a normal HTTP request. It should be 23 // constructed using (Requester).NewRequest(). 24 type Request interface { 25 // GetPath should return the URL path, for example "/endpoint/thing". 26 GetPath() string 27 // GetContext should return the same context that's passed into NewRequest. 28 // For implementations that don't support this, it can remove a 29 // context.Background(). 30 GetContext() context.Context 31 // AddHeader appends headers. 32 AddHeader(http.Header) 33 // AddQuery appends URL query values. 34 AddQuery(url.Values) 35 // WithBody should automatically close the ReadCloser on finish. This is 36 // similar to the stdlib's Request behavior. 37 WithBody(io.ReadCloser) 38 } 39 40 // Response is returned from (Requester).DoContext(). 41 type Response interface { 42 GetStatus() int 43 GetHeader() http.Header 44 // Body's ReadCloser will always be closed when done, unless DoContext() 45 // returns an error. 46 GetBody() io.ReadCloser 47 } 48 49 // OptHeader returns the response header, or nil if from is nil. 50 func OptHeader(from Response) http.Header { 51 if from == nil { 52 return nil 53 } 54 return from.GetHeader() 55 }