client: export NewAppRequest and SendAppRequest methods

Allow for more flexibility in constructing requests if needed.
This commit is contained in:
Michael Marineau 2017-06-09 12:02:58 -07:00
parent a2f653da34
commit 0152a8b1b0

View file

@ -201,7 +201,7 @@ func (ac *AppClient) SetOEM(oem string) {
} }
func (ac *AppClient) UpdateCheck() (*omaha.UpdateResponse, error) { func (ac *AppClient) UpdateCheck() (*omaha.UpdateResponse, error) {
req := ac.newReq() req := ac.NewAppRequest()
app := req.Apps[0] app := req.Apps[0]
app.AddPing() app.AddPing()
app.AddUpdateCheck() app.AddUpdateCheck()
@ -213,12 +213,8 @@ func (ac *AppClient) UpdateCheck() (*omaha.UpdateResponse, error) {
ac.sentPing = true ac.sentPing = true
appResp, err := ac.doReq(ac.apiEndpoint, req) appResp, err := ac.SendAppRequest(req)
if err, ok := err.(ErrorEvent); ok { if err != nil {
ac.Event(err.ErrorEvent())
return nil, err
} else if err != nil {
ac.Event(NewErrorEvent(ExitCodeOmahaRequestError))
return nil, err return nil, err
} }
@ -245,18 +241,14 @@ func (ac *AppClient) UpdateCheck() (*omaha.UpdateResponse, error) {
} }
func (ac *AppClient) Ping() error { func (ac *AppClient) Ping() error {
req := ac.newReq() req := ac.NewAppRequest()
app := req.Apps[0] app := req.Apps[0]
app.AddPing() app.AddPing()
ac.sentPing = true ac.sentPing = true
appResp, err := ac.doReq(ac.apiEndpoint, req) appResp, err := ac.SendAppRequest(req)
if err, ok := err.(ErrorEvent); ok { if err != nil {
ac.Event(err.ErrorEvent())
return err
} else if err != nil {
ac.Event(NewErrorEvent(ExitCodeOmahaRequestError))
return err return err
} }
@ -279,7 +271,7 @@ func (ac *AppClient) Ping() error {
func (ac *AppClient) Event(event *omaha.EventRequest) <-chan error { func (ac *AppClient) Event(event *omaha.EventRequest) <-chan error {
errc := make(chan error, 1) errc := make(chan error, 1)
url := ac.apiEndpoint url := ac.apiEndpoint
req := ac.newReq() req := ac.NewAppRequest()
app := req.Apps[0] app := req.Apps[0]
app.Events = append(app.Events, event) app.Events = append(app.Events, event)
@ -309,7 +301,8 @@ func (ac *AppClient) Event(event *omaha.EventRequest) <-chan error {
return errc return errc
} }
func (ac *AppClient) newReq() *omaha.Request { // NewAppRequest creates a Request object containing one application.
func (ac *AppClient) NewAppRequest() *omaha.Request {
req := omaha.NewRequest() req := omaha.NewRequest()
req.Version = ac.clientVersion req.Version = ac.clientVersion
req.UserID = ac.userID req.UserID = ac.userID
@ -332,6 +325,21 @@ func (ac *AppClient) newReq() *omaha.Request {
return req return req
} }
// SendAppRequest sends a Request object and validates the response.
// On failure an error event is automatically sent to the server.
func (ac *AppClient) SendAppRequest(req *omaha.Request) (*omaha.AppResponse, error) {
resp, err := ac.doReq(ac.apiEndpoint, req)
if _, ok := err.(omaha.AppStatus); ok {
// No point to sending an error if we got a well-formed
// non-ok application status in the response.
} else if err, ok := err.(ErrorEvent); ok {
ac.Event(err.ErrorEvent())
} else if err != nil {
ac.Event(NewErrorEvent(ExitCodeOmahaRequestError))
}
return resp, err
}
// doReq posts an omaha request. It may be called in its own goroutine so // doReq posts an omaha request. It may be called in its own goroutine so
// it should not touch any mutable data in AppClient, but apiClient is ok. // it should not touch any mutable data in AppClient, but apiClient is ok.
func (ac *AppClient) doReq(url string, req *omaha.Request) (*omaha.AppResponse, error) { func (ac *AppClient) doReq(url string, req *omaha.Request) (*omaha.AppResponse, error) {