auth
137:f59559b33c76 Browse Files
Add a handler to add endpoints to a client. Add a handler that enables adding endpoints to a client through the API.
1.1 --- a/client.go Thu Mar 05 18:55:31 2015 -0500 1.2 +++ b/client.go Thu Mar 05 19:11:53 2015 -0500 1.3 @@ -395,7 +395,7 @@ 1.4 r.Handle("/clients/{id}", wrap(context, GetClientHandler)).Methods("GET") 1.5 r.Handle("/clients/{id}", wrap(context, UpdateClientHandler)).Methods("PATCH") 1.6 // BUG(paddy): We need to implement a handler to delete a client. Also, what should that do with the grants and tokens belonging to that client? 1.7 - // BUG(paddy): We need to implement a handler to add an endpoint to a client. 1.8 + r.Handle("/clients/{id}/endpoints", wrap(context, AddEndpointsHandler)).Methods("POST") 1.9 // BUG(paddy): We need to implement a handler to remove an endpoint from a client. 1.10 // BUG(paddy): We need to implement a handler to list endpoints. 1.11 } 1.12 @@ -663,6 +663,79 @@ 1.13 return 1.14 } 1.15 1.16 +func AddEndpointsHandler(w http.ResponseWriter, r *http.Request, c Context) { 1.17 + type addEndpointReq struct { 1.18 + Endpoints []string `json:"endpoints"` 1.19 + } 1.20 + errors := []requestError{} 1.21 + vars := mux.Vars(r) 1.22 + if vars["id"] == "" { 1.23 + errors = append(errors, requestError{Slug: requestErrMissing, Param: "id"}) 1.24 + encode(w, r, http.StatusBadRequest, response{Errors: errors}) 1.25 + return 1.26 + } 1.27 + id, err := uuid.Parse(vars["id"]) 1.28 + if err != nil { 1.29 + errors = append(errors, requestError{Slug: requestErrInvalidFormat, Param: "id"}) 1.30 + encode(w, r, http.StatusBadRequest, response{Errors: errors}) 1.31 + return 1.32 + } 1.33 + _, err = c.GetClient(id) 1.34 + if err != nil { 1.35 + if err == ErrClientNotFound { 1.36 + errors = append(errors, requestError{Slug: requestErrNotFound, Param: "id"}) 1.37 + encode(w, r, http.StatusBadRequest, response{Errors: errors}) 1.38 + return 1.39 + } 1.40 + errors = append(errors, requestError{Slug: requestErrActOfGod}) 1.41 + encode(w, r, http.StatusInternalServerError, response{Errors: errors}) 1.42 + return 1.43 + } 1.44 + var req addEndpointReq 1.45 + decoder := json.NewDecoder(r.Body) 1.46 + err = decoder.Decode(&req) 1.47 + if err != nil { 1.48 + encode(w, r, http.StatusBadRequest, invalidFormatResponse) 1.49 + return 1.50 + } 1.51 + if len(req.Endpoints) < 1 { 1.52 + errors = append(errors, requestError{Slug: requestErrMissing, Field: "/endpoints"}) 1.53 + encode(w, r, http.StatusBadRequest, response{Errors: errors}) 1.54 + return 1.55 + } 1.56 + endpoints := []Endpoint{} 1.57 + for pos, u := range req.Endpoints { 1.58 + if parsed, err := url.Parse(u); err != nil { 1.59 + errors = append(errors, requestError{Slug: requestErrInvalidFormat, Field: "/endpoints/" + strconv.Itoa(pos)}) 1.60 + continue 1.61 + } else if !parsed.IsAbs() { 1.62 + errors = append(errors, requestError{Slug: requestErrInvalidValue, Field: "/endpoints" + strconv.Itoa(pos)}) 1.63 + continue 1.64 + } 1.65 + e := Endpoint{ 1.66 + ID: uuid.NewID(), 1.67 + ClientID: id, 1.68 + URI: u, 1.69 + Added: time.Now(), 1.70 + } 1.71 + endpoints = append(endpoints, e) 1.72 + } 1.73 + if len(errors) > 0 { 1.74 + encode(w, r, http.StatusBadRequest, response{Errors: errors}) 1.75 + return 1.76 + } 1.77 + err = c.AddEndpoints(id, endpoints) 1.78 + if err != nil { 1.79 + encode(w, r, http.StatusInternalServerError, actOfGodResponse) 1.80 + return 1.81 + } 1.82 + resp := response{ 1.83 + Errors: errors, 1.84 + Endpoints: endpoints, 1.85 + } 1.86 + encode(w, r, http.StatusCreated, resp) 1.87 +} 1.88 + 1.89 func clientCredentialsValidate(w http.ResponseWriter, r *http.Request, context Context) (scopes []string, profileID uuid.ID, valid bool) { 1.90 scopes = strings.Split(r.PostFormValue("scope"), " ") 1.91 valid = true