Пример #1
0
func (ts *Source) refreshToken() error {
	log.Debugf("refreshing the token from %q", refreshURL)

	data, err := json.Marshal(ts.token)
	if err != nil {
		log.Errorf("%s: %s", constants.ErrJSONEncoding, err)
		return constants.ErrJSONEncoding
	}
	req, err := http.NewRequest("POST", refreshURL, bytes.NewBuffer(data))
	if err != nil {
		log.Errorf("%s: %s", constants.ErrCreatingHTTPRequest, err)
		return constants.ErrCreatingHTTPRequest
	}
	req.Header.Set("Content-Type", "application/json")
	res, err := (&http.Client{}).Do(req)
	if err != nil {
		log.Errorf("%s: %s", constants.ErrDoingHTTPRequest, err)
		return constants.ErrDoingHTTPRequest
	}
	defer res.Body.Close()
	if err := json.NewDecoder(res.Body).Decode(ts.token); err != nil {
		log.Errorf("%s: %s", constants.ErrJSONDecodingResponseBody, err)
		return constants.ErrJSONDecodingResponseBody
	}
	log.Debug("token was refreshed successfully")

	return nil
}
Пример #2
0
// Sync syncs the tree with the server.
func (nt *Tree) Sync() error {
	postURL := nt.client.GetMetadataURL("changes")
	c := &changes{
		Checkpoint: nt.Checkpoint,
	}
	jsonBytes, err := json.Marshal(c)
	if err != nil {
		log.Errorf("%s: %s", constants.ErrJSONEncoding, err)
		return constants.ErrJSONEncoding
	}
	req, err := http.NewRequest("POST", postURL, bytes.NewBuffer(jsonBytes))
	if err != nil {
		log.Errorf("%s: %s", constants.ErrCreatingHTTPRequest, err)
		return constants.ErrCreatingHTTPRequest
	}
	req.Header.Set("Content-Type", "application/json")
	res, err := nt.client.Do(req)
	if err != nil {
		log.Errorf("%s: %s", constants.ErrDoingHTTPRequest, err)
		return constants.ErrDoingHTTPRequest
	}
	if err := nt.client.CheckResponse(res); err != nil {
		return err
	}

	// return format should be:
	// {"checkpoint": str, "reset": bool, "nodes": []}
	// {"checkpoint": str, "reset": false, "nodes": []}
	// {"end": true}
	defer res.Body.Close()
	bodyBytes, err := ioutil.ReadAll(res.Body)
	if err != nil {
		log.Errorf("%s: %s", constants.ErrReadingResponseBody, err)
		return constants.ErrReadingResponseBody
	}
	for _, lineBytes := range bytes.Split(bodyBytes, []byte("\n")) {
		var cr changesResponse
		if err := json.Unmarshal(lineBytes, &cr); err != nil {
			log.Errorf("%s: %s", constants.ErrJSONDecodingResponseBody, err)
			return constants.ErrJSONDecodingResponseBody
		}
		if cr.Checkpoint != "" {
			log.Debugf("changes returned Checkpoint: %s", cr.Checkpoint)
			nt.Checkpoint = cr.Checkpoint
		}
		if cr.Reset {
			log.Debug("reset is required")
			return constants.ErrMustFetchFresh
		}
		if cr.End {
			break
		}
		if err := nt.updateNodes(cr.Nodes); err != nil {
			return err
		}
	}

	return nil
}
Пример #3
0
// Token returns an oauth2.Token. If the cached token (in (*Source).path) has
// expired, it will fetch the token from the server and cache it before
// returning it.
func (ts *Source) Token() (*oauth2.Token, error) {
	if !ts.token.Valid() {
		log.Debug("token is not valid, it has probably expired")
		if err := ts.refreshToken(); err != nil {
			return nil, err
		}

		if err := ts.saveToken(); err != nil {
			return nil, err
		}
	}

	return ts.token, nil
}
Пример #4
0
func (ts *Source) saveToken() error {
	log.Debugf("saving the token to %s", ts.path)
	f, err := os.Create(ts.path)
	if err != nil {
		log.Errorf("%s: %s", constants.ErrCreateFile, ts.path)
		return constants.ErrCreateFile
	}
	if err := json.NewEncoder(f).Encode(ts.token); err != nil {
		log.Errorf("%s: %s", constants.ErrJSONEncoding, err)
		return constants.ErrJSONEncoding
	}

	log.Debug("token saved successfully")
	return nil
}
Пример #5
0
func (ts *Source) readToken() error {
	log.Debugf("reading the token from %s", ts.path)
	f, err := os.Open(ts.path)
	if err != nil {
		log.Errorf("%s: %s", constants.ErrOpenFile, ts.path)
		return constants.ErrOpenFile
	}
	if err := json.NewDecoder(f).Decode(ts.token); err != nil {
		log.Errorf("%s: %s", constants.ErrJSONDecoding, err)
		return constants.ErrJSONDecoding
	}

	log.Debug("token loaded successfully")
	return nil
}
Пример #6
0
func (nt *Tree) loadOrFetch() error {
	var err error
	if err = nt.loadCache(); err != nil {
		log.Debug(err)
		if err = nt.fetchFresh(); err != nil {
			return err
		}
	}

	if err = nt.Sync(); err != nil {
		switch err {
		case constants.ErrMustFetchFresh:
			if err = nt.fetchFresh(); err != nil {
				return err
			}
			return nt.Sync()
		default:
			return err
		}
	}

	return nil
}