Skip to content

Commit

Permalink
daemon, store: switch to new store APIs in snapd (#2036)
Browse files Browse the repository at this point in the history
* respond to Gustavo's review comments

- rename order->orderResult
- rename purchased->bought
- rename map ordersByID->bought

* respond to John's review comments

- Re-order case statements.
- Minimise scope of resp / err variables.
- Add FIXME to ReadyToBuy call.
  • Loading branch information
pete-woods committed Sep 30, 2016
1 parent 89daf34 commit 63eec5c
Show file tree
Hide file tree
Showing 3 changed files with 275 additions and 381 deletions.
85 changes: 51 additions & 34 deletions daemon/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1787,6 +1787,44 @@ func postCreateUser(c *Command, r *http.Request, user *auth.UserState) Response
}, nil)
}

func convertBuyError(err error) Response {
switch err {
case nil:
return nil
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)
default:
return InternalError("%v", err)
}
}

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

Expand All @@ -1800,13 +1838,8 @@ 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
if resp := convertBuyError(err); resp != nil {
return resp
}

return SyncResponse(buyResult, nil)
Expand All @@ -1823,6 +1856,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 @@ -1833,33 +1875,8 @@ func getPaymentMethods(c *Command, r *http.Request, user *auth.UserState) Respon
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
if resp := convertBuyError(s.ReadyToBuy(user)); resp != nil {
return resp
}

return SyncResponse(true, nil)
Expand Down

0 comments on commit 63eec5c

Please sign in to comment.