Exemple #1
0
// getIEPGURLResolver returns iepgURLResolver object.
// the mock object in '__test_iepg_url_resolver' local variable in the request can be used for testing.
func getIEPGURLResolver(req *wcg.Request) *iepgURLResolver {
	if req.IsTest() {
		if v := req.Local("__test_iepg_url_resolver"); v != nil {
			if v1, ok := v.(*iepgURLResolver); ok {
				return v1
			}
		}
	}
	return defaultIEPGURLResolver
}
Exemple #2
0
// NewContext a wrapper for appengine.NewContext
func NewContext(req *wcg.Request) context.Context {
	ctx := req.Local("__gaetest__context")
	if ctx != nil {
		return ctx.(context.Context)
	}
	ctx = req.Local("__gaebg_context")
	if ctx != nil {
		return ctx.(context.Context)
	}
	return appengine.NewContext(req.HTTPRequest())
}
func sendToBigQuery(req *wcg.Request) error {
	var errors = make([]error, 0)
	var svc *bigquery.Service
	var records []*wcg.LogRecord
	var tmp interface{}
	data := make(map[string][]*bigquery.TableDataInsertAllRequestRows)

	if tmp = req.Local(requestLocalLogCacheKey); tmp == nil {
		return nil
	}
	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
}
Exemple #4
0
// NewService returns a new bigquery service accessor instance
func NewService(req *wcg.Request) (*bigquery.Service, error) {
	var ctx context.Context
	tmp := req.Local("__gaetest__context")
	if tmp != nil {
		ctx = tmp.(context.Context)
	} else {
		ctx = appengine.NewContext(req.HTTPRequest())
	}

	client, err := NewHTTPClient(ctx)
	if err != nil {
		return nil, err
	}
	svc, err := bigquery.New(client)
	if err != nil {
		return nil, err
	}
	return svc, nil
}