Stop injecting to the helper function

GetCryptoService is a helper function and we have injected a specific
http.ResponseWriter object for it to write back error message. Meanwhile
the caller for that function checks whether the cryptoService is nil
or not and return immediately if it is nil.

I think it's not a good idea to write back HTTP response in the helper function,
it's the caller's work and thus there is no need to inject the specific
ResponseWriter object into it.

Signed-off-by: Hu Keping <hukeping@huawei.com>
This commit is contained in:
HuKeping 2015-12-25 16:48:41 +08:00
parent 0c11a97082
commit 74d4af37a3
1 changed files with 8 additions and 9 deletions

View File

@ -3,6 +3,7 @@ package api
import (
"crypto/rand"
"encoding/json"
"fmt"
"net/http"
"github.com/docker/notary/signer"
@ -28,18 +29,16 @@ func Handlers(cryptoServices signer.CryptoServiceIndex) *mux.Router {
// algorithm specified in the HTTP request. If the algorithm isn't specified
// or isn't supported, an error is returned to the client and this function
// returns a nil CryptoService
func getCryptoService(w http.ResponseWriter, algorithm string, cryptoServices signer.CryptoServiceIndex) signed.CryptoService {
func getCryptoService(algorithm string, cryptoServices signer.CryptoServiceIndex) (signed.CryptoService, error) {
if algorithm == "" {
http.Error(w, "algorithm not specified", http.StatusBadRequest)
return nil
return nil, fmt.Errorf("algorithm not specified")
}
if service, ok := cryptoServices[algorithm]; ok {
return service
return service, nil
}
http.Error(w, "algorithm "+algorithm+" not supported", http.StatusBadRequest)
return nil
return nil, fmt.Errorf("algorithm " + algorithm + " not supported")
}
// KeyInfo returns a Handler that given a specific Key ID param, returns the public key bits of that key
@ -79,9 +78,9 @@ func KeyInfo(cryptoServices signer.CryptoServiceIndex) http.Handler {
func CreateKey(cryptoServices signer.CryptoServiceIndex) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
cryptoService := getCryptoService(w, vars["Algorithm"], cryptoServices)
if cryptoService == nil {
// Error handled inside getCryptoService
cryptoService, err := getCryptoService(vars["Algorithm"], cryptoServices)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}