Ejemplo n.º 1
0
func ObtainBearerToken(code string, stvToken string) (*sync.SyncTask, error) {
	tokenUrl := "https://runkeeper.com/apps/token"
	formData := make(map[string][]string)
	formData["grant_type"] = []string{"authorization_code"}
	formData["code"] = []string{code}
	formData["client_id"] = []string{RkClientId}
	formData["client_secret"] = []string{RkSecret}
	formData["redirect_uri"] = []string{RedirectUriRk}
	client := new(http.Client)
	response, err := client.PostForm(tokenUrl, formData)
	responseJson := make(map[string]string)
	if err == nil {
		responseBody, _ := ioutil.ReadAll(response.Body)
		json.Unmarshal(responseBody, &responseJson)
		token := responseJson["access_token"]
		db := sync.CreateSyncDbRepo(DbConnectionString)

		var task *sync.SyncTask
		if stvToken != "" && stvToken != "undefined" {
			task, err = db.FindSyncTaskByToken(stvToken)
		} else {
			task, err = db.FindSyncTaskByToken(token)
		}
		if task == nil || err != nil {
			syncTask := sync.CreateSyncTask("", "", -1, Environment)
			syncTask.RunkeeperToken = token
			syncTask.LastSeenTimestamp = nowMinusOneHourInUnix()
			db.StoreSyncTask(*syncTask)
			return syncTask, nil
		} else { //existing task
			if task.RunkeeperToken != token {
				task.RunkeeperToken = token
				db.UpdateSyncTask(*task)
			} else {
				log.Printf("Token %s is already stored for task id: %d", token, task.Uid)
			}
			return task, nil
		}
	} else {
		fmt.Print(err)
	}
	return nil, errors.New("Not happened")
}
Ejemplo n.º 2
0
// syncTaskJob would do whatever syncing is necessary in the background
func syncTaskJob(j *que.Job) error {
	var synctask sync.SyncTask
	err := json.Unmarshal(j.Args, &synctask)
	if err != nil {
		log.WithField("args", string(j.Args)).Error("Unable to unmarshal job arguments into SyncTask")
		return err
	}

	log.WithField("SyncTask", synctask).Info("Processing Synctask!")

	stvClientImpl := stv.CreateStravaClient(synctask.StravaToken)
	rkClientImpl := rk.CreateRKClient(synctask.RunkeeperToken)
	_, _, err = synctask.Sync(stvClientImpl, rkClientImpl)
	if err != nil {
		log.WithField("args", string(j.Args)).WithField("QueId", j.ID).Error("Error while syncing synctask.")
		return err
	}

	j.Delete()
	j.Done()

	return nil
}