Skip to content

Commit

Permalink
daemon, store: switch to new store APIs in snapd
Browse files Browse the repository at this point in the history
  • Loading branch information
pete-woods committed Sep 29, 2016
1 parent a6cc546 commit c0759be
Show file tree
Hide file tree
Showing 3 changed files with 272 additions and 376 deletions.
86 changes: 53 additions & 33 deletions daemon/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1782,6 +1782,44 @@ func postCreateUser(c *Command, r *http.Request, user *auth.UserState) Response
}, nil)
}

func convertBuyError(err error) Response {
switch err {
default:
return InternalError("%v", err)
case store.ErrInvalidCredentials:
return Unauthorized(err.Error())
case store.ErrUnauthenticated:
return SyncResponse(&resp{
Type: ResponseTypeError,
Result: &errorResult{
Message: err.Error(),
Kind: errorKindLoginRequired,
},
Status: http.StatusBadRequest,
}, nil)
case store.ErrTOSNotAccepted:
return SyncResponse(&resp{
Type: ResponseTypeError,
Result: &errorResult{
Message: err.Error(),
Kind: errorKindTermsNotAccepted,
},
Status: http.StatusBadRequest,
}, nil)
case store.ErrNoPaymentMethods:
return SyncResponse(&resp{
Type: ResponseTypeError,
Result: &errorResult{
Message: err.Error(),
Kind: errorKindNoPaymentMethods,
},
Status: http.StatusBadRequest,
}, nil)
case nil:
return nil
}
}

func postBuy(c *Command, r *http.Request, user *auth.UserState) Response {
var opts store.BuyOptions

Expand All @@ -1795,13 +1833,9 @@ func postBuy(c *Command, r *http.Request, user *auth.UserState) Response {

buyResult, err := s.Buy(&opts, user)

switch err {
default:
return InternalError("%v", err)
case store.ErrInvalidCredentials:
return Unauthorized(err.Error())
case nil:
// continue
resp := convertBuyError(err)
if resp != nil {
return resp
}

return SyncResponse(buyResult, nil)
Expand All @@ -1818,6 +1852,15 @@ func getPaymentMethods(c *Command, r *http.Request, user *auth.UserState) Respon
return InternalError("%v", err)
case store.ErrInvalidCredentials:
return Unauthorized(err.Error())
case store.ErrUnauthenticated:
return SyncResponse(&resp{
Type: ResponseTypeError,
Result: &errorResult{
Message: err.Error(),
Kind: errorKindLoginRequired,
},
Status: http.StatusBadRequest,
}, nil)
case nil:
// continue
}
Expand All @@ -1829,32 +1872,9 @@ func readyToBuy(c *Command, r *http.Request, user *auth.UserState) Response {
s := getStore(c)

err := s.ReadyToBuy(user)

switch err {
default:
return InternalError("%v", err)
case store.ErrInvalidCredentials:
return Unauthorized(err.Error())
case store.ErrTOSNotAccepted:
return SyncResponse(&resp{
Type: ResponseTypeError,
Result: &errorResult{
Message: err.Error(),
Kind: errorKindTermsNotAccepted,
},
Status: http.StatusBadRequest,
}, nil)
case store.ErrNoPaymentMethods:
return SyncResponse(&resp{
Type: ResponseTypeError,
Result: &errorResult{
Message: err.Error(),
Kind: errorKindNoPaymentMethods,
},
Status: http.StatusBadRequest,
}, nil)
case nil:
// continue
resp := convertBuyError(err)
if resp != nil {
return resp
}

return SyncResponse(true, nil)
Expand Down

0 comments on commit c0759be

Please sign in to comment.