func (c *client) do(req *http.Request, response interface{}) error { trace.DumpRequest(req) res, err := c.httpClient.Do(req) if err != nil { return err } defer res.Body.Close() trace.DumpResponse(res) if res.StatusCode == http.StatusUnauthorized { return Error{Type: "unauthorized", Message: "unauthorized"} } if res.StatusCode > 299 { errResponse := Error{} json.NewDecoder(res.Body).Decode(&errResponse) return errResponse } if response != nil { return json.NewDecoder(res.Body).Decode(response) } return nil }
func (c *client) doSubscribe(routeName string, retries uint16) (RawEventSource, error) { config := sse.Config{ Client: c.streamingHTTPClient, RetryParams: sse.RetryParams{ MaxRetries: retries, RetryInterval: time.Second, }, RequestCreator: func() *http.Request { request, err := c.reqGen.CreateRequest(routeName, nil, nil) c.tokenMutex.RLock() defer c.tokenMutex.RUnlock() request.Header.Add("Authorization", "bearer "+c.authToken) if err != nil { panic(err) // totally shouldn't happen } trace.DumpRequest(request) return request }, } eventSource, err := config.Connect() if err != nil { bre, ok := err.(sse.BadResponseError) if ok && bre.Response.StatusCode == http.StatusUnauthorized { return nil, Error{Type: "unauthorized", Message: "unauthorized"} } return nil, err } return eventSource, nil }
func (f *Fetcher) doFetch() (*Token, bool, error) { values := url.Values{} values.Add("grant_type", "client_credentials") requestBody := values.Encode() tokenURL := fmt.Sprintf("%s:%d/oauth/token", f.config.TokenEndpoint, f.config.Port) request, err := http.NewRequest("POST", tokenURL, bytes.NewBuffer([]byte(requestBody))) if err != nil { return nil, false, err } request.SetBasicAuth(f.config.ClientName, f.config.ClientSecret) request.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8") request.Header.Add("Accept", "application/json; charset=utf-8") trace.DumpRequest(request) f.logger.Debug("http-request", lager.Data{"endpoint": request.URL}) resp, err := f.client.Do(request) if err != nil { f.logger.Debug("error-fetching-token", lager.Data{"error": err.Error()}) return nil, true, err } defer resp.Body.Close() trace.DumpResponse(resp) f.logger.Debug("http-response", lager.Data{"status-code": resp.StatusCode}) body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, true, err } if resp.StatusCode != http.StatusOK { retry := false if resp.StatusCode >= http.StatusInternalServerError { retry = true } return nil, retry, errors.New(fmt.Sprintf("status code: %d, body: %s", resp.StatusCode, body)) } token := &Token{} err = json.Unmarshal(body, token) if err != nil { f.logger.Debug("error-umarshalling-token", lager.Data{"error": err.Error()}) return nil, false, err } return token, false, nil }
func (u *UaaClient) doFetchToken() (*schema.Token, bool, error) { logger := u.logger.Session("uaa-client") values := url.Values{} values.Add("grant_type", "client_credentials") requestBody := values.Encode() tokenURL := fmt.Sprintf("%s/oauth/token", u.config.UaaEndpoint) request, err := http.NewRequest("POST", tokenURL, bytes.NewBuffer([]byte(requestBody))) if err != nil { return nil, false, err } request.SetBasicAuth(u.config.ClientName, u.config.ClientSecret) request.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8") request.Header.Add("Accept", "application/json; charset=utf-8") trace.DumpRequest(request) logger.Info("fetch-token-from-uaa-start", lager.Data{"endpoint": request.URL}) resp, err := u.client.Do(request) if err != nil { return nil, true, err } defer resp.Body.Close() trace.DumpResponse(resp) logger.Info("fetch-token-from-uaa-end", lager.Data{"status-code": resp.StatusCode}) body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, true, err } if resp.StatusCode != http.StatusOK { retry := false if resp.StatusCode >= http.StatusInternalServerError { retry = true } return nil, retry, errors.New(fmt.Sprintf("status code: %d, body: %s", resp.StatusCode, body)) } token := &schema.Token{} err = json.Unmarshal(body, token) if err != nil { return nil, false, err } return token, false, nil }
func (c *client) SubscribeToEvents() (EventSource, error) { eventSource, err := sse.Connect(c.streamingHTTPClient, time.Second, func() *http.Request { request, err := c.reqGen.CreateRequest(EventStreamRoute, nil, nil) request.Header.Add("Authorization", "bearer "+c.authToken) if err != nil { panic(err) // totally shouldn't happen } trace.DumpRequest(request) return request }) if err != nil { return nil, err } return NewEventSource(eventSource), nil }
func (c *client) doSubscribe(routeName string) (RawEventSource, error) { eventSource, err := sse.Connect(c.streamingHTTPClient, time.Second, func() *http.Request { request, err := c.reqGen.CreateRequest(routeName, nil, nil) c.tokenMutex.RLock() defer c.tokenMutex.RUnlock() request.Header.Add("Authorization", "bearer "+c.authToken) if err != nil { panic(err) // totally shouldn't happen } trace.DumpRequest(request) return request }) if err != nil { return nil, err } return eventSource, nil }
func (f *Fetcher) FetchToken() (*Token, error) { values := url.Values{} values.Add("grant_type", "client_credentials") requestBody := values.Encode() tokenURL := fmt.Sprintf("%s:%d/oauth/token", f.config.TokenEndpoint, f.config.Port) request, err := http.NewRequest("POST", tokenURL, bytes.NewBuffer([]byte(requestBody))) if err != nil { return nil, err } trace.DumpRequest(request) request.SetBasicAuth(f.config.ClientName, f.config.ClientSecret) request.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8") request.Header.Add("Accept", "application/json; charset=utf-8") resp, err := f.client.Do(request) if err != nil { return nil, err } defer resp.Body.Close() trace.DumpResponse(resp) body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } if resp.StatusCode != http.StatusOK { return nil, errors.New(fmt.Sprintf("status code: %d, body: %s", resp.StatusCode, body)) } token := &Token{} err = json.Unmarshal(body, token) if err != nil { return nil, err } return token, nil }