예제 #1
0
파일: wl.go 프로젝트: robdimsdale/tardy
func newClient(cmd *cobra.Command) wl.Client {
	var l logger.Logger
	if verbose {
		l = logger.NewLogger(logger.DEBUG)
	} else {
		l = logger.NewLogger(logger.INFO)
	}

	if accessToken == "" {
		accessToken = os.Getenv(accessTokenEnvVariable)
	}

	if accessToken == "" {
		l.Error(
			"exiting",
			errors.New("accessToken not found. Either provide the flag -"+accessTokenLongFlag+" or set the environment variable "+accessTokenEnvVariable))
		os.Exit(2)
	}

	if clientID == "" {
		clientID = os.Getenv(clientIDEnvVariable)
	}

	if clientID == "" {
		l.Error(
			"exiting",
			errors.New("clientID not found. Either provide the flag -"+clientIDLongFlag+" or set the environment variable "+clientIDEnvVariable))
		os.Exit(2)
	}

	return oauth.NewClient(accessToken, clientID, wl.APIURL, l)
}
예제 #2
0
func (h handler) Tasks(w http.ResponseWriter, r *http.Request) {
	session, err := h.store.Get(r, "session-name")
	if err != nil {
		h.logger.Error("", err)
		http.Error(w, err.Error(), 500)
		return
	}

	accessTokenInterface := session.Values["accessToken"]
	if accessTokenInterface == nil {
		err := fmt.Errorf("accessToken not found in session")
		h.logger.Error("", err)
		http.Error(w, err.Error(), 500)
		return
	}

	accessToken, ok := accessTokenInterface.(string)
	if !ok {
		err := fmt.Errorf("failed to convert %v into string", accessTokenInterface)
		h.logger.Error("", err)
		http.Error(w, err.Error(), 500)
		return
	}

	if accessToken == "" {
		err := fmt.Errorf("accessToken empty in session")
		h.logger.Error("", err)
		http.Error(w, err.Error(), 500)
		return
	}

	client := oauth.NewClient(
		accessToken,
		h.clientID,
		wl.APIURL,
		logger.NewLogger(logger.INFO),
	)

	completed := true
	completedTasks, err := client.CompletedTasks(completed)
	if err != nil {
		fmt.Printf("err getting tasks: %s\n", err.Error())
	}

	tasks, err := tardyTasks(completedTasks)
	if err != nil {
		fmt.Printf("err converting tasks: %s\n", err.Error())
	}

	err = json.NewEncoder(w).Encode(tasks)
	if err != nil {
		fmt.Printf("err serializing completed: %s\n", err.Error())
	}
}