func (jClient *Client) Do(request *http.Request) (*http.Response, error) {
	jClient.Log.Println("Trying request:", request.URL)

	if jClient.userID == "" {
		// Set the user agent to firefox in order to get a guest token
		request.Header.Add("User-Agent", "Mozilla/5.0 (X11; Linux x86_64)")
	}

	resp, err := jClient.httpClient.Do(request)

	if err != nil {
		return nil, err
	}

	webAuthMsg := resp.Header.Get("x-com-ibm-team-repository-web-auth-msg")
	if webAuthMsg != "authrequired" && resp.StatusCode != 401 {
		// Request didn't require any further authentication. Return the result.
		return resp, nil
	}

	err = resp.Body.Close()

	if err != nil {
		return nil, err
	}

	// If credentials are provided then do the dance to become authenticated
	if jClient.password != "" {
		jClient.Log.Println("Authenticating using provided credentials for", jClient.userID)

		form := &url.Values{}
		form.Add("origin", "https://login.jazz.net")
		form.Add("username", jClient.userID)
		form.Add("password", jClient.password)

		authReq, err := http.NewRequest("POST", "https://login.jazz.net/sso/login.do", bytes.NewBufferString(form.Encode()))
		if err != nil {
			return nil, err
		}

		authReq.Header = make(map[string][]string)
		authReq.Header["Content-Type"] = []string{"application/x-www-form-urlencoded"}

		resp, err = jClient.httpClient.Do(authReq)
		if err != nil {
			return nil, err
		}

		authReq, err = http.NewRequest("GET", "https://login.jazz.net/psso/proxy/force?origin=https%3A%2F%2Flogin.jazz.net", nil)
		if err != nil {
			return nil, err
		}

		resp, err = jClient.httpClient.Do(authReq)
		if err != nil {
			return nil, err
		}

		// Unauthorized, authorize now
		if resp.StatusCode == 401 {
			type ForwardTo struct {
				RedirectUri string `json:"redirect_uri"`
				Client      string `json:"client_id"`
				State       string `json:"state"`
			}
			type Result1 struct {
				ForwardTo ForwardTo `json:"forwardTo"`
			}

			result1 := &Result1{}
			b, _ := ioutil.ReadAll(resp.Body)
			err := json.Unmarshal(b, result1)
			if err != nil {
				return nil, err
			}

			forwardTo := result1.ForwardTo
			client := forwardTo.Client
			state := forwardTo.State
			//redirectUri := forwardTo.RedirectUri

			authReq, err = http.NewRequest("GET", "https://login.jazz.net/sso/oauth/authorize?origin=https%3A%2F%2Flogin.jazz.net&response_type=code&client_id="+client+"&state="+state+"&redirect_uri=https%3A%2F%2Flogin.jazz.net%2Fpsso%2Fproxy%2Fauthorize", nil)
			if err != nil {
				return nil, err
			}

			resp, err = jClient.httpClient.Do(authReq)
			if err != nil {
				return nil, err
			}

			// The credentials did not work, abort with an error
			if resp.StatusCode != 200 {
				return nil, errorFromResponse(resp)
			}

			b, _ = ioutil.ReadAll(resp.Body)
			resp.Body.Close()

			type Result2 struct {
				Code string `json:"code"`
			}
			result2 := &Result2{}
			err = json.Unmarshal(b, result2)
			if err != nil {
				return nil, err
			}

			code := result2.Code

			authReq, err = http.NewRequest("GET", "https://login.jazz.net/psso/proxy/authorize.do?origin=https%3A%2F%2Flogin.jazz.net&state="+state+"&code="+code, nil)
			if err != nil {
				return nil, err
			}

			resp, err = jClient.httpClient.Do(authReq)
			if err != nil {
				return nil, err
			}

			resp.Body.Close()

			// Last step is to discover the Jazz ID for the current user
			identReq, err := http.NewRequest("GET", "https://hub.jazz.net/manage/service/com.ibm.team.jazzhub.common.service.ICurrentUserService", nil)
			if err != nil {
				return nil, err
			}

			resp, err = jClient.httpClient.Do(identReq)
			if err != nil {
				return nil, err
			}

			if resp.StatusCode != 200 {
				return nil, errorFromResponse(resp)
			}

			b, _ = ioutil.ReadAll(resp.Body)
			resp.Body.Close()

			type IdentResult struct {
				UserId string `json:"userId"`
			}
			identResult := &IdentResult{}
			err = json.Unmarshal(b, identResult)
			if err != nil {
				return nil, err
			}

			jClient.SetJazzId(identResult.UserId)
		} else {
			panic(errorFromResponse(resp))
		}

		// If the initial request was a POST or PUT then send the special
		//  signal that the caller should repeat their request now that they
		//  are authenticated.
		if request.Body != nil {
			return nil, nil
		}
	} else {
		return nil, &JazzError{Msg: "Guest access was not granted"}
	}

	jClient.Log.Println("Retrying request")
	resp, err = jClient.httpClient.Do(request)

	if err != nil {
		return nil, err
	}

	return resp, nil
}
// Perform an http requests with this client
// Authentication is performed automatically
// In some instances both the response and error are nil in which case you must repeat your request
Perform an http requests with this client
Authentication is performed automatically
In some instances both the response and error are nil in which case you must repeat your request
// Perform an http requests with this client // Authentication is performed automatically // In some instances both the response and error are nil in which case you must repeat your request
func (jClient *Client) Do(request *http.Request) (*http.Response, error) {
	jClient.Log.Println("Trying request:", request.URL)
	if jClient.userID == "" {
		request.Header.Add("User-Agent", "Mozilla/5.0 (X11; Linux x86_64)")
	}
	resp, err := jClient.httpClient.Do(request)
	if err != nil {
		return nil, err
	}
	webAuthMsg := resp.Header.Get("x-com-ibm-team-repository-web-auth-msg")
	if webAuthMsg != "authrequired" && resp.StatusCode != 401 {
		return resp, nil
	}
	err = resp.Body.Close()
	if err != nil {
		return nil, err
	}
	if jClient.password != "" {
		jClient.Log.Println("Authenticating using provided credentials for", jClient.userID)
		form := &url.Values{}
		form.Add("origin", "https://login.jazz.net")
		form.Add("username", jClient.userID)
		form.Add("password", jClient.password)
		authReq, err := http.NewRequest("POST", "https://login.jazz.net/sso/login.do", bytes.NewBufferString(form.Encode()))
		if err != nil {
			return nil, err
		}
		authReq.Header = make(map[string][]string)
		authReq.Header["Content-Type"] = []string{"application/x-www-form-urlencoded"}
		resp, err = jClient.httpClient.Do(authReq)
		if err != nil {
			return nil, err
		}
		authReq, err = http.NewRequest("GET", "https://login.jazz.net/psso/proxy/force?origin=https%3A%2F%2Flogin.jazz.net", nil)
		if err != nil {
			return nil, err
		}
		resp, err = jClient.httpClient.Do(authReq)
		if err != nil {
			return nil, err
		}
		if resp.StatusCode == 401 {
			type ForwardTo struct {
				RedirectUri string `json:"redirect_uri"`
				Client      string `json:"client_id"`
				State       string `json:"state"`
			}
			type Result1 struct {
				ForwardTo ForwardTo `json:"forwardTo"`
			}
			result1 := &Result1{}
			b, _ := ioutil.ReadAll(resp.Body)
			err := json.Unmarshal(b, result1)
			if err != nil {
				return nil, err
			}
			forwardTo := result1.ForwardTo
			client := forwardTo.Client
			state := forwardTo.State
			authReq, err = http.NewRequest("GET", "https://login.jazz.net/sso/oauth/authorize?origin=https%3A%2F%2Flogin.jazz.net&response_type=code&client_id="+client+"&state="+state+"&redirect_uri=https%3A%2F%2Flogin.jazz.net%2Fpsso%2Fproxy%2Fauthorize", nil)
			if err != nil {
				return nil, err
			}
			resp, err = jClient.httpClient.Do(authReq)
			if err != nil {
				return nil, err
			}
			if resp.StatusCode != 200 {
				return nil, errorFromResponse(resp)
			}
			b, _ = ioutil.ReadAll(resp.Body)
			resp.Body.Close()
			type Result2 struct {
				Code string `json:"code"`
			}
			result2 := &Result2{}
			err = json.Unmarshal(b, result2)
			if err != nil {
				return nil, err
			}
			code := result2.Code
			authReq, err = http.NewRequest("GET", "https://login.jazz.net/psso/proxy/authorize.do?origin=https%3A%2F%2Flogin.jazz.net&state="+state+"&code="+code, nil)
			if err != nil {
				return nil, err
			}
			resp, err = jClient.httpClient.Do(authReq)
			if err != nil {
				return nil, err
			}
			resp.Body.Close()
			identReq, err := http.NewRequest("GET", "https://hub.jazz.net/manage/service/com.ibm.team.jazzhub.common.service.ICurrentUserService", nil)
			if err != nil {
				return nil, err
			}
			resp, err = jClient.httpClient.Do(identReq)
			if err != nil {
				return nil, err
			}
			if resp.StatusCode != 200 {
				return nil, errorFromResponse(resp)
			}
			b, _ = ioutil.ReadAll(resp.Body)
			resp.Body.Close()
			type IdentResult struct {
				UserId string `json:"userId"`
			}
			identResult := &IdentResult{}
			err = json.Unmarshal(b, identResult)
			if err != nil {
				return nil, err
			}
			jClient.SetJazzId(identResult.UserId)
		} else {
			panic(errorFromResponse(resp))
		}
		if request.Body != nil {
			return nil, nil
		}
	} else {
		return nil, &JazzError{Msg: "Guest access was not granted"}
	}
	jClient.Log.Println("Retrying request")
	resp, err = jClient.httpClient.Do(request)
	if err != nil {
		return nil, err
	}
	return resp, nil
}
func ( jClient * Client ) Do ( request * http . Request ) ( * http . Response , error ) { jClient . Log . Println ( " " , request . URL ) if jClient . userID == " " { request . Header . Add ( " " , " " ) } resp , err := jClient . httpClient . Do ( request ) if err != nil { return nil , err } webAuthMsg := resp . Header . Get ( " " ) if webAuthMsg != " " && resp . StatusCode != 401 { return resp , nil } err = resp . Body . Close ( ) if err != nil { return nil , err } if jClient . password != " " { jClient . Log . Println ( " " , jClient . userID ) form := & url . Values { } form . Add ( " " , " " ) form . Add ( " " , jClient . userID ) form . Add ( " " , jClient . password ) authReq , err := http . NewRequest ( " " , " " , bytes . NewBufferString ( form . Encode ( ) ) ) if err != nil { return nil , err } authReq . Header = make ( map [ string ] [ ] string ) authReq . Header [ " " ] = [ ] string { " " } resp , err = jClient . httpClient . Do ( authReq ) if err != nil { return nil , err } authReq , err = http . NewRequest ( " " , " " , nil ) if err != nil { return nil , err } resp , err = jClient . httpClient . Do ( authReq ) if err != nil { return nil , err } if resp . StatusCode == 401 { type ForwardTo struct { RedirectUri string `json:"redirect_uri"` Client string `json:"client_id"` State string `json:"state"` } type Result1 struct { ForwardTo ForwardTo `json:"forwardTo"` } result1 := & Result1 { } b , _ := ioutil . ReadAll ( resp . Body ) err := json . Unmarshal ( b , result1 ) if err != nil { return nil , err } forwardTo := result1 . ForwardTo client := forwardTo . Client state := forwardTo . State authReq , err = http . NewRequest ( " " , " " + client + " " + state + " " , nil ) if err != nil { return nil , err } resp , err = jClient . httpClient . Do ( authReq ) if err != nil { return nil , err } if resp . StatusCode != 200 { return nil , errorFromResponse ( resp ) } b , _ = ioutil . ReadAll ( resp . Body ) resp . Body . Close ( ) type Result2 struct { Code string `json:"code"` } result2 := & Result2 { } err = json . Unmarshal ( b , result2 ) if err != nil { return nil , err } code := result2 . Code authReq , err = http . NewRequest ( " " , " " + state + " " + code , nil ) if err != nil { return nil , err } resp , err = jClient . httpClient . Do ( authReq ) if err != nil { return nil , err } resp . Body . Close ( ) identReq , err := http . NewRequest ( " " , " " , nil ) if err != nil { return nil , err } resp , err = jClient . httpClient . Do ( identReq ) if err != nil { return nil , err } if resp . StatusCode != 200 { return nil , errorFromResponse ( resp ) } b , _ = ioutil . ReadAll ( resp . Body ) resp . Body . Close ( ) type IdentResult struct { UserId string `json:"userId"` } identResult := & IdentResult { } err = json . Unmarshal ( b , identResult ) if err != nil { return nil , err } jClient . SetJazzId ( identResult . UserId ) } else { panic ( errorFromResponse ( resp ) ) } if request . Body != nil { return nil , nil } } else { return nil , & JazzError { Msg : " " } } jClient . Log . Println ( " " ) resp , err = jClient . httpClient . Do ( request ) if err != nil { return nil , err } return resp , nil }
Perform an http requests with this client
Authentication is performed automatically
In some instances both the response and error are nil in which case you must repeat your request
Perform an http requests with this client Authentication is performed automatically In some instances both the response and error are nil in which case you must repeat your request
// Set the user agent to firefox in order to get a guest token // Request didn't require any further authentication. Return the result. // If credentials are provided then do the dance to become authenticated // Unauthorized, authorize now //redirectUri := forwardTo.RedirectUri // The credentials did not work, abort with an error // Last step is to discover the Jazz ID for the current user // If the initial request was a POST or PUT then send the special //  signal that the caller should repeat their request now that they //  are authenticated.