Exemple #1
0
func QueryHandler(rw http.ResponseWriter, req *http.Request) {
	query := req.FormValue("name")
	c := appengine.NewContext(req)
	client, err := serviceaccount.NewClient(c, bigquery.BigqueryScope)
	if err != nil {
		fmt.Fprintf(rw, "%s", err.Error())
		return
	}

	service, err := bigquery.New(client)
	if err != nil {
		fmt.Fprintf(rw, "%s", err.Error())
		return
	}

	response, err := service.Jobs.Query(PROJECT_ID, &bigquery.QueryRequest{
		Kind:  "bigquery#queryRequest",
		Query: "SELECT * FROM " + DATASET_ID + "." + TABLE_ID + " WHERE staff CONTAINS \"" + query + "\" LIMIT 100",
	}).Do()
	if err != nil {
		fmt.Fprintf(rw, "%s", err.Error())
		return
	}

	animes := response.Schema.Fields
	for _, row := range response.Rows {
		for i, cell := range row.F {
			if cell.V != nil && animes[i].Name != "staff" {
				fmt.Fprintf(rw, "%v: %v", animes[i].Name, cell.V)
			}
		}
	}
}
// Creates a new Google Cloud Storage Client
func newStorageService(c appengine.Context) (*storage.Service, error) {
	var httpClient *http.Client
	var err error

	if appengine.IsDevAppServer() {
		httpClient, err = devserviceaccount.NewClient(c, devStorageKeyPath, scope)
	} else {
		httpClient, err = serviceaccount.NewClient(c, scope)
	}

	if err != nil {
		return nil, err
	}

	service, err := storage.New(httpClient)
	if err != nil {
		return nil, err
	}

	if err = initDefaultBucket(service); err != nil {
		return nil, err
	}

	return service, nil
}
func handleFunc(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	c.Infof("env: %v", os.Environ())
	client, err := serviceaccount.NewClient(c, "https://www.googleapis.com/auth/devstorage.read_write")
	c.Infof("client=%+v err=%+v", client, err)
	cc := cloud.WithContext(context.Background(), appengine.AppID(c), client)
	bucket, err := storage.BucketInfo(cc, appengine.AppID(c)+".appspot.com")
	c.Infof("bucket=%+v err=%+v", bucket, err)
}
Exemple #4
0
func NewService(c appengine.Context) (service *bigquery.Service, err error) {
	client, err := serviceaccount.NewClient(c, bigquery.BigqueryScope)
	if err != nil {
		return
	}

	service, err = bigquery.New(client)
	if err != nil {
		return
	}

	return service, nil
}
Exemple #5
0
func BuildBigQueryTables(req *Request) error {
	if !LogSinkConfig.IsBigQueryEnabled() {
		return fmt.Errorf("BigQuery is not enabled, please check bigquery_project and bigquery_dataset configuration.")
	}

	client, err := serviceaccount.NewClient(NewContext(req), bigquery.BigqueryScope)
	if err != nil {
		return err
	}
	service, err := bigquery.New(client)
	if err != nil {
		return err
	}
	projectId := LogSinkConfig.BigqueryProject
	datasetId := LogSinkConfig.BigqueryDataset
	for tableId, tbl := range LogSinkConfig.schema {
		tbl.TableReference.ProjectId = projectId
		tbl.TableReference.DatasetId = datasetId

		tbl1, err := service.Tables.Get(projectId, datasetId, tableId).Do()
		if err == nil {
			req.Logger.Info("Table %q already exists on \"%s/%s\"", tableId, projectId, datasetId)
			if len(tbl.Schema.Fields) < len(tbl1.Schema.Fields) {
				req.Logger.Fatal("You cannot remove fields from table %q!!", tableId)
				continue
			}
			if len(tbl.Schema.Fields) == len(tbl1.Schema.Fields) {
				req.Logger.Info("No filed changes on %q", tableId)
				continue
			}
			req.Logger.Info("Trying to patch the exising table %q in %s/%s", tableId, LogSinkConfig.BigqueryProject, LogSinkConfig.BigqueryDataset)
			upd := service.Tables.Patch(LogSinkConfig.BigqueryProject, LogSinkConfig.BigqueryDataset, tableId, tbl)
			_, err = upd.Do()
			if err != nil {
				req.Logger.Error("Could not patch %q in %s/%s: %v", tableId, LogSinkConfig.BigqueryProject, LogSinkConfig.BigqueryDataset, err)
				continue
			}
			req.Logger.Info("Table %q has been successfully updated in %s/%s", tableId, LogSinkConfig.BigqueryProject, LogSinkConfig.BigqueryDataset)
		} else {
			req.Logger.Info("Trying to create a new table %q in %s/%s", tableId, LogSinkConfig.BigqueryProject, LogSinkConfig.BigqueryDataset)
			ins := service.Tables.Insert(LogSinkConfig.BigqueryProject, LogSinkConfig.BigqueryDataset, tbl)
			_, err = ins.Do()
			if err != nil {
				req.Logger.Error("Could not create a new table %q in %s/%s: %v", tableId, LogSinkConfig.BigqueryProject, LogSinkConfig.BigqueryDataset, err)
				continue
			}
			req.Logger.Info("Table %q has been successfully created in %s/%s", tableId, LogSinkConfig.BigqueryProject, LogSinkConfig.BigqueryDataset)
		}
	}
	return nil
}
Exemple #6
0
func NewGAELogSinkWithContext(format string, ctx appengine.Context) *GAELogSink {
	var bq *bigquery.Service
	if LogSinkConfig.IsBigQueryEnabled() {
		client, err := serviceaccount.NewClient(ctx, bigquery.BigqueryScope)
		if err == nil {
			bq, _ = bigquery.New(client)
		}
	}
	return &GAELogSink{
		formatter: NewLogRecordFormatter(format),
		ctx:       ctx,
		bigquery:  bq,
	}
}
Exemple #7
0
func NewService(req *wcg.Request) (*bigquery.Service, error) {
	var ctx appengine.Context
	tmp := req.Local("__gaetest__context")
	if tmp != nil {
		ctx = tmp.(appengine.Context)
	} else {
		ctx = appengine.NewContext(req.HttpRequest())
	}

	client, err := serviceaccount.NewClient(ctx, bigquery.BigqueryScope)
	if err != nil {
		return nil, err
	}
	svc, err := bigquery.New(client)
	if err != nil {
		return nil, err
	}
	return svc, nil
}
// shorten shortens a new url and redirects to the main page
func shorten(c appengine.Context, w http.ResponseWriter, r *http.Request) error {
	client, err := serviceaccount.NewClient(c, "https://www.googleapis.com/auth/urlshortener")
	if err != nil {
		return fmt.Errorf("error creating authorized client: %v", err)
	}
	service, err := urlshortener.New(client)
	if err != nil {
		return fmt.Errorf("error creating urlshortener service: %v", err)
	}
	result, err := service.Url.Insert(&urlshortener.Url{
		LongUrl: r.FormValue("url"),
	}).Do()
	if err != nil {
		return fmt.Errorf("error posting new url: %v", err)
	}
	c.Infof("urlshortener API response: %v", result)
	http.Redirect(w, r, "/", 303)
	return nil
}
// handle renders the main page template with a submission form and the history of shortened urls.
func handle(c appengine.Context, w http.ResponseWriter, r *http.Request) error {
	client, err := serviceaccount.NewClient(c, "https://www.googleapis.com/auth/urlshortener")
	if err != nil {
		return fmt.Errorf("error creating authorized client: %v", err)
	}
	service, err := urlshortener.New(client)
	if err != nil {
		return fmt.Errorf("error creating urlshortener service: %v", err)
	}
	result, err := service.Url.List().Do()
	if err != nil {
		return fmt.Errorf("error getting history: %v", err)
	}
	c.Infof("urlshortener API response: %v", result)
	if err := mainTemplate.Execute(w, result); err != nil {
		return fmt.Errorf("error executing template: %v", err)
	}
	return nil
}
Exemple #10
0
func NewService(c appengine.Context) *Service {
	s := &Service{}
	s.TableData = &TabledataService{s: s}
	s.Tables = &TablesService{s: s}

	if appengine.IsDevAppServer() {
		err := setServiceAccountForDev(c, s)
		if err != nil {
			panic(fmt.Sprintf("%s", err.Error()))
		}
		return s
	}

	client, err := serviceaccount.NewClient(c, BIGQUERY_SCOPE)
	if err != nil {
		panic(fmt.Sprintf("%s", err.Error()))
	}
	service, err := bigquery.New(client)
	if err != nil {
		panic(fmt.Sprintf("%s", err.Error()))
	}
	s.bq = service
	return s
}