2016-05-18 17:17:28 +00:00
|
|
|
// NOTE: Subject to change, do not rely on this package from outside git-lfs source
|
2016-05-13 16:38:06 +00:00
|
|
|
package api
|
2016-05-16 16:57:37 +00:00
|
|
|
|
2016-05-25 19:31:12 +00:00
|
|
|
import "github.com/github/git-lfs/config"
|
|
|
|
|
|
|
|
type Operation string
|
|
|
|
|
|
|
|
const (
|
|
|
|
UploadOperation Operation = "upload"
|
|
|
|
DownloadOperation Operation = "download"
|
|
|
|
)
|
2016-05-17 10:52:56 +00:00
|
|
|
|
2016-05-18 17:17:28 +00:00
|
|
|
// Client exposes the LFS API to callers through a multitude of different
|
|
|
|
// services and transport mechanisms. Callers can make a *RequestSchema using
|
|
|
|
// any service that is attached to the Client, and then execute a request based
|
|
|
|
// on that schema using the `Do()` method.
|
|
|
|
//
|
|
|
|
// A prototypical example follows:
|
|
|
|
// ```
|
|
|
|
// apiResponse, schema := client.Locks.Lock(request)
|
|
|
|
// resp, err := client.Do(schema)
|
|
|
|
// if err != nil {
|
|
|
|
// handleErr(err)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// fmt.Println(apiResponse.Lock)
|
|
|
|
// ```
|
|
|
|
type Client struct {
|
2016-05-20 23:44:01 +00:00
|
|
|
// Locks is the LockService used to interact with the Git LFS file-
|
|
|
|
// locking API.
|
2016-05-18 21:44:34 +00:00
|
|
|
Locks LockService
|
|
|
|
|
2016-05-18 19:19:20 +00:00
|
|
|
// lifecycle is the lifecycle used by all requests through this client.
|
|
|
|
lifecycle Lifecycle
|
2016-05-16 16:57:37 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 19:31:12 +00:00
|
|
|
// NewClient instantiates and returns a new instance of *Client, with the given
|
|
|
|
// lifecycle.
|
2016-05-18 17:17:28 +00:00
|
|
|
//
|
2016-05-25 19:31:12 +00:00
|
|
|
// If no lifecycle is given, a HttpLifecycle is used by default.
|
|
|
|
func NewClient(lifecycle Lifecycle) *Client {
|
2016-05-18 19:19:20 +00:00
|
|
|
if lifecycle == nil {
|
2016-05-25 19:31:12 +00:00
|
|
|
lifecycle = NewHttpLifecycle(config.Config)
|
2016-05-18 19:19:20 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 19:31:12 +00:00
|
|
|
return &Client{lifecycle: lifecycle}
|
2016-05-17 10:52:56 +00:00
|
|
|
}
|
|
|
|
|
2016-05-18 17:17:28 +00:00
|
|
|
// Do preforms the request assosicated with the given *RequestSchema by
|
|
|
|
// delegating into the Lifecycle in use.
|
|
|
|
//
|
|
|
|
// If any error was encountered while either building, executing or cleaning up
|
|
|
|
// the request, then it will be returned immediately, and the request can be
|
|
|
|
// treated as invalid.
|
|
|
|
//
|
2016-05-20 23:44:01 +00:00
|
|
|
// If no error occured, an api.Response will be returned, along with a `nil`
|
|
|
|
// error. At this point, the body of the response has been serialized into
|
|
|
|
// `schema.Into`, and the body has been closed.
|
2016-05-18 17:43:56 +00:00
|
|
|
func (c *Client) Do(schema *RequestSchema) (Response, error) {
|
2016-05-18 19:19:20 +00:00
|
|
|
req, err := c.lifecycle.Build(schema)
|
2016-05-17 10:52:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-05-18 19:19:20 +00:00
|
|
|
resp, err := c.lifecycle.Execute(req, schema.Into)
|
2016-05-17 10:52:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-05-18 19:19:20 +00:00
|
|
|
if err = c.lifecycle.Cleanup(resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
2016-05-17 10:52:56 +00:00
|
|
|
}
|