Golang Context.Data Examples

Golang Context.Data - 30 examples found. These are the top rated real world Golang examples of github.com/gin-gonic/gin.Context.Data extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
func ProviderGetEpisode(ctx *gin.Context) {
	provider := ctx.Params.ByName("provider")
	showId := ctx.Params.ByName("showId")
	seasonNumber, _ := strconv.Atoi(ctx.Params.ByName("season"))
	episodeNumber, _ := strconv.Atoi(ctx.Params.ByName("episode"))

	log.Println("Searching links for TVDB Id:", showId)

	show, err := tvdb.NewShowCached(showId, "en")
	if err != nil {
		ctx.Error(err)
		return
	}
	episode := show.Seasons[seasonNumber].Episodes[episodeNumber-1]

	log.Printf("Resolved %s to %s", showId, show.SeriesName)

	searcher := providers.NewAddonSearcher(provider)
	torrents := searcher.SearchEpisodeLinks(show, episode)
	if ctx.Request.URL.Query().Get("resolve") == "true" {
		for _, torrent := range torrents {
			torrent.Resolve()
		}
	}
	data, err := json.MarshalIndent(providerDebugResponse{
		Payload: searcher.GetEpisodeSearchObject(show, episode),
		Results: torrents,
	}, "", "    ")
	if err != nil {
		xbmc.AddonFailure(provider)
		ctx.Error(err)
	}
	ctx.Data(200, "application/json", data)
}
Example #2
0
func HandleQuery(query string, c *gin.Context) {
	rawQuery, err := base64.StdEncoding.DecodeString(query)
	if err == nil {
		query = string(rawQuery)
	}

	result, err := DB(c).Query(query)
	if err != nil {
		c.JSON(400, NewError(err))
		return
	}

	format := getQueryParam(c, "format")
	filename := getQueryParam(c, "filename")

	if filename == "" {
		filename = fmt.Sprintf("pgweb-%v.%v", time.Now().Unix(), format)
	}

	if format != "" {
		c.Writer.Header().Set("Content-disposition", "attachment;filename="+filename)
	}

	switch format {
	case "csv":
		c.Data(200, "text/csv", result.CSV())
	case "json":
		c.Data(200, "applicaiton/json", result.JSON())
	case "xml":
		c.XML(200, result)
	default:
		c.JSON(200, result)
	}
}
Example #3
0
func Count(context *gin.Context) {
	appenginecontext := appengine.NewContext(context.Request)
	image := context.Param("image")
	imageId, err := strconv.Atoi(image)
	if err != nil {
		context.AbortWithError(http.StatusInternalServerError, err)
		appenginecontext.Debugf("%s", err)
		return
	}

	var counter Counter
	key := datastore.NewKey(appenginecontext, "counters", "", int64(imageId), nil)
	if err := datastore.Get(appenginecontext, key, &counter); err != nil {
		context.AbortWithError(http.StatusInternalServerError, err)
		appenginecontext.Debugf("%s", err)
		return
	}

	counter.Hits = counter.Hits + 1
	counter.Last = time.Now()

	_, err = datastore.Put(appenginecontext, key, &counter)
	if err != nil {
		context.AbortWithError(http.StatusInternalServerError, err)
		appenginecontext.Debugf("%s", err)
		return
	}

	output, _ := base64.StdEncoding.DecodeString("R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")
	context.Data(200, "image/gif", output)
}
Example #4
0
// FriendshipShow takes a given ID from gin.Context
// @returns a specific friendship JSON object
func FriendshipShow(c *gin.Context) {
	friendship := models.Friendship{}

	if database.DBCon.First(&friendship, c.Param("id")).RecordNotFound() {
		c.AbortWithError(http.StatusNotFound, appError.RecordNotFound).
			SetMeta(appError.RecordNotFound)
		return
	}

	var fd models.FriendshipData

	database.DBCon.First(&friendship.User, friendship.UserID)
	database.DBCon.First(&friendship.Friend, friendship.FriendID)
	database.DBCon.First(&fd, friendship.FriendshipDataID)

	if friendship.UserID == fd.PositiveUserID {
		friendship.Balance = fd.Balance
	} else {
		friendship.Balance = -fd.Balance
	}

	data, err := jsonapi.Marshal(friendship)

	if err != nil {
		c.AbortWithError(http.StatusInternalServerError, err).
			SetMeta(appError.JSONParseFailure)
		return
	}

	c.Data(http.StatusOK, "application/vnd.api+json", data)
}
Example #5
0
func rpcEndpoint(c *gin.Context) {
	op := c.MustGet("nuget.op").(string)
	query := c.MustGet("nuget.search").(*NuGetQuery)

	oplog := log.WithFields(log.Fields{
		"op":    op,
		"query": query,
	})

	switch op {
	case "$metadata":
		c.Data(200, XmlMimeType, []byte(NuGetMetadata))
		break
	case "Packages":
		data := c.MustGet("nuget.op.data").(*PackagesCommand)
		Packages(c, data, query, oplog.WithField("data", data))
	case "FindPackagesById":
		data := c.MustGet("nuget.op.data").(*FindPackagesByIdCommand)
		FindPackagesById(c, data, query, oplog.WithField("data", data))
	case "Search":
		data := c.MustGet("nuget.op.data").(*SearchCommand)
		Search(c, data, query, oplog.WithField("data", data))
	case "GetUpdates":
		data := c.MustGet("nuget.op.data").(*GetUpdatesCommand)
		GetUpdates(c, data, query, oplog.WithField("data", data))
	default:
		c.Status(400)
	}
}
Example #6
0
// FriendshipIndex takes in query params through
// gin.Context and is restricted to the currentUser
// @returns an array of friendship JSON objects
func FriendshipIndex(c *gin.Context) {
	friendships := []models.Friendship{}
	var curUser models.User
	database.DBCon.First(&curUser, c.Keys["CurrentUserID"])

	database.DBCon.Model(&curUser).Related(&friendships, "Friendships")

	// Get user and friend and friendshipData
	// TODO: n + 1 query problem here, so we'll figure this out later
	for i := range friendships {
		var fd models.FriendshipData
		database.DBCon.First(&friendships[i].Friend, friendships[i].FriendID)
		database.DBCon.First(&fd, friendships[i].FriendshipDataID)

		if curUser.ID == fd.PositiveUserID {
			friendships[i].Balance = fd.Balance
		} else {
			friendships[i].Balance = -fd.Balance
		}

		friendships[i].User = curUser
	}

	data, err := jsonapi.Marshal(friendships)

	if err != nil {
		c.AbortWithError(http.StatusInternalServerError, err).
			SetMeta(appError.JSONParseFailure)
		return
	}

	c.Data(http.StatusOK, "application/vnd.api+json", data)
}