github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/registry/regserver/handlers/profile.go (about) 1 package handlers 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net/http" 7 8 apiutil "github.com/qri-io/qri/api/util" 9 testkeys "github.com/qri-io/qri/auth/key/test" 10 "github.com/qri-io/qri/registry" 11 ) 12 13 // NewProfilesHandler creates a profiles handler function that operates 14 // on a *registry.Profiles 15 func NewProfilesHandler(profiles registry.Profiles) http.HandlerFunc { 16 return func(w http.ResponseWriter, r *http.Request) { 17 switch r.Method { 18 case "POST": 19 ps := []*registry.Profile{} 20 switch r.Header.Get("Content-Type") { 21 case "application/json": 22 if err := json.NewDecoder(r.Body).Decode(&ps); err != nil { 23 apiutil.WriteErrResponse(w, http.StatusBadRequest, err) 24 return 25 } 26 default: 27 err := fmt.Errorf("Content-Type must be application/json") 28 apiutil.WriteErrResponse(w, http.StatusBadRequest, err) 29 return 30 } 31 32 for _, pro := range ps { 33 profiles.Create(pro.Username, pro) 34 } 35 36 apiutil.WriteResponse(w, ps) 37 case "GET": 38 39 l, err := profiles.Len() 40 if err != nil { 41 apiutil.WriteErrResponse(w, http.StatusInternalServerError, err) 42 } 43 ps := make([]*registry.Profile, l) 44 45 i := 0 46 profiles.SortedRange(func(key string, p *registry.Profile) (bool, error) { 47 ps[i] = p 48 i++ 49 return true, nil 50 }) 51 52 apiutil.WriteResponse(w, ps) 53 default: 54 apiutil.NotFoundHandler(w, r) 55 } 56 } 57 } 58 59 // NewProfileHandler creates a profile handler func that operats on 60 // a *registry.Profiles 61 func NewProfileHandler(profiles registry.Profiles) http.HandlerFunc { 62 return func(w http.ResponseWriter, r *http.Request) { 63 p := ®istry.Profile{} 64 switch r.Header.Get("Content-Type") { 65 case "application/json": 66 if err := json.NewDecoder(r.Body).Decode(p); err != nil { 67 apiutil.WriteErrResponse(w, http.StatusBadRequest, err) 68 return 69 } 70 default: 71 err := fmt.Errorf("Content-Type must be application/json") 72 apiutil.WriteErrResponse(w, http.StatusBadRequest, err) 73 return 74 } 75 76 switch r.Method { 77 case "GET": 78 var err error 79 if p.Username != "" { 80 p, err = profiles.Load(p.Username) 81 } else { 82 var ok bool 83 err = profiles.Range(func(_ string, profile *registry.Profile) (bool, error) { 84 if profile.ProfileID == p.ProfileID || profile.PublicKey == p.PublicKey { 85 p = profile 86 ok = true 87 return true, nil 88 } 89 return false, nil 90 }) 91 if !ok { 92 err = registry.ErrNotFound 93 } 94 } 95 if err != nil { 96 apiutil.NotFoundHandler(w, r) 97 return 98 } 99 case "POST": 100 if err := registry.RegisterProfile(profiles, p); err != nil { 101 apiutil.WriteErrResponse(w, http.StatusBadRequest, err) 102 return 103 } 104 case "PUT": 105 if err := registry.UpdateProfile(profiles, p); err != nil { 106 apiutil.WriteErrResponse(w, http.StatusBadRequest, err) 107 return 108 } 109 case "DELETE": 110 if err := registry.DeregisterProfile(profiles, p); err != nil { 111 apiutil.WriteErrResponse(w, http.StatusBadRequest, err) 112 return 113 } 114 default: 115 apiutil.NotFoundHandler(w, r) 116 return 117 } 118 119 apiutil.WriteResponse(w, p) 120 } 121 } 122 123 // NewProveKeyHandler creates a handler that implements provekey 124 func NewProveKeyHandler(profiles registry.Profiles) http.HandlerFunc { 125 return func(w http.ResponseWriter, r *http.Request) { 126 // TODO(dustmop): Lookup account referred to by the request, 127 // and retrieve appropriate data. 128 kd := testkeys.GetKeyData(3) 129 res := map[string]string{} 130 res["profileID"] = kd.EncodedPeerID 131 apiutil.WriteResponse(w, res) 132 } 133 }