// Returns a Context Object. func NewContext(req *wcg.Request) appengine.Context { ctx := req.Local("__gaetest__context") if ctx != nil { return ctx.(appengine.Context) } return appengine.NewContext(req.HttpRequest()) }
func getResponseHelper(req *wcg.Request) ResponseHelper { app := req.Local(LOCAL_APP_KEY).(*App) if strings.HasPrefix(req.URL().Path, "/api") { return app.Api } return app.Page }
func GetCurrentApp(req *wcg.Request) *App { if app := req.Local(LOCAL_APP_KEY); app != nil { return app.(*App) } else { return nil } }
func sendToBigQuery(req *wcg.Request) error { var errors = make([]error, 0) var svc *bigquery.Service var records []*wcg.LogRecord data := make(map[string][]*bigquery.TableDataInsertAllRequestRows) if tmp := req.Local(requestLocalLogCacheKey); tmp == nil { return nil } else { records = tmp.([]*wcg.LogRecord) } svc, err := bq.NewService(req) if err != nil { return err } for _, r := range records { tableId, row, err := bqTransform(r) if err != nil { errors = append(errors, err) continue } if rows, ok := data[tableId]; ok { rows = append(rows, row) } else { data[tableId] = []*bigquery.TableDataInsertAllRequestRows{row} } } for tableId, rows := range data { ret, err := svc.Tabledata.InsertAll( LogSinkConfig.BigqueryProject, LogSinkConfig.BigqueryDataset, tableId, &bigquery.TableDataInsertAllRequest{ Kind: "bigquery#tableDataInsertAllRequest", Rows: rows, }).Do() if err != nil { errors = append(errors, err) continue } if len(ret.InsertErrors) > 0 { errors = append(errors, fmt.Errorf("InsertErrors: %s", ret.InsertErrors[0])) continue } } if len(errors) > 0 { return logTransmissionError(errors) } return nil }
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 }