Exemple #1
0
// Initialize extracts the cmd line params and creates the rets.Requester
func (cfg *Config) Initialize() (rets.Requester, error) {
	transport := wirelog.NewHTTPTransport()

	if cfg.WireLog != "" {
		wirelog.LogToFile(transport, cfg.WireLog, true, true)
		fmt.Println("wire logging enabled:", cfg.WireLog)
	}
	// should we throw an err here too?
	return rets.DefaultSession(
		cfg.Username,
		cfg.Password,
		cfg.UserAgent,
		cfg.UserAgentPw,
		cfg.Version,
		transport,
	)
}
Exemple #2
0
// DefaultSession configures the default rets session
func DefaultSession(user, pwd, userAgent, userAgentPw, retsVersion string, transport http.RoundTripper) (Requester, error) {
	if transport == nil {
		transport = wirelog.NewHTTPTransport()
	}

	client := http.Client{
		Transport: transport,
	}

	jar, err := cookiejar.New(nil)
	if err != nil {
		return nil, err
	}
	client.Jar = jar
	// 4) send the request
	request := func(ctx context.Context, req *http.Request) (*http.Response, error) {
		return ctxhttp.Do(ctx, &client, req)
	}
	// 3) www auth
	wwwAuth := (&WWWAuthTransport{
		Requester: request,
		Username:  user,
		Password:  pwd,
	}).Request
	// 2) apply ua auth headers per request, if there is a pwd
	uaAuth := (&UserAgentAuthentication{
		Requester:         wwwAuth,
		UserAgent:         userAgent,
		UserAgentPassword: userAgentPw,
		GetRETSVersion:    CreateRETSVersioner(retsVersion),
		GetSessionID:      CreateSessionIDer(client.Jar),
	}).Request
	// 1) apply default headers first (outermost wrapping)
	headers := func(ctx context.Context, req *http.Request) (*http.Response, error) {
		req.Header.Set(UserAgent, userAgent)
		req.Header.Set(RETSVersion, retsVersion)
		req.Header.Set(Accept, "*/*")
		return uaAuth(ctx, req)
	}
	return headers, nil
}