func getService(responseFile string) (*products.Service, *httptest.Server, error) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		slurp, err := ioutil.ReadFile(path.Join("testdata", responseFile))
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		res, err := http.ReadResponse(bufio.NewReader(strings.NewReader(string(slurp))), r)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		defer res.Body.Close()
		bs, err := ioutil.ReadAll(res.Body)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		w.WriteHeader(res.StatusCode)
		log.Print(string(bs))
		fmt.Fprint(w, string(bs))
	}))

	service, err := products.New(http.DefaultClient)
	if err != nil {
		return service, nil, err
	}
	service.BaseURL = ts.URL
	service.User = os.Getenv("STORE2_USER")
	service.Password = os.Getenv("STORE2_PASSWORD")
	return service, ts, nil
}
Exemple #2
0
func GetProductsService() (*products.Service, error) {
	client, err := GetHttpClient()
	if err != nil {
		return nil, err
	}
	service, err := products.New(client)
	if err != nil {
		return nil, err
	}
	if url := GetBaseURL(); url != "" {
		service.BaseURL = url
	}
	service.User = getUsername()
	service.Password = getPassword()
	return service, nil
}