Skip to main content

Create a tracker

This page describes how to create a tracker.

Overview

Trackers describe at a high level the progress of a resource consumption, such as user X watched 50% (or 6 episodes) of anime Y, the consumption is IN PROGRESS, meaning it is yet to be COMPLETED and it's not ON HOLD, the consumed resources so far are the episodes [A ... F], and consumption started on May 22nd 2022 and was last updated on May 27th 2022.

Sample

The following sample shows how to create a tracker.

Replace [USER-OR-AUDIENCE] with the resource name of the user or audience the tracker belong to, e.g. users/123 or audiences/123.

Replace [RESOURCE] with the resource name of the resource to be tracked, e.g. animes/123.

tip

Tip: do not forget to replace CLIENT-ID and CLIENT-SECRET with valid IAM Service Account client credentials.

package main

import (
"context"
"log"

"golang.org/x/oauth2/clientcredentials"

"google.golang.org/api/option"
"google.golang.org/protobuf/types/known/timestamppb"

gapic "github.com/animeapis/api-go-client/tracker/v1alpha1"
tracker "github.com/animeapis/go-genproto/tracker/v1alpha1"
)

var (
TrackerParent = "[USER-OR-AUDIENCE]"
Resource = "[RESOURCE]"

ClientID = "[CLIENT-ID]"
ClientSecret = "[CLIENT-SECRET]"
)

var (
TokenURL = "https://accounts.animeshon.com/o/oauth2/token"
Endpoint = "tracker.animeapis.com:443"
)

func main() {
ctx := context.Background()

config := &clientcredentials.Config{
ClientID: ClientID,
ClientSecret: ClientSecret,
TokenURL: TokenURL,
}

options := []option.ClientOption{
option.WithEndpoint(Endpoint),
option.WithTokenSource(config.TokenSource(ctx)),
}

client, err := gapic.NewClient(ctx, options...)
if err != nil {
log.Fatalf("NewClient: %s", err)
}

request := &tracker.CreateTrackerRequest{
Parent: TrackerParent,
Tracker: &tracker.Tracker{
Resource: Resource,
StartTime: timestamppb.Now(),
EndTime: timestamppb.Now(),
State: tracker.State_IN_PROGRESS,
},
}

response, err := client.CreateTracker(ctx, request)
if err != nil {
log.Fatalf("CreateTracker: %s", err)
}

log.Printf("name : %s", response.GetName())
log.Printf("resource : %s", response.GetResource())
log.Printf("completed : %v", response.GetCompletedResources())
log.Printf("start time: %s", response.GetStartTime().AsTime().String())
log.Printf("end time : %s", response.GetEndTime().AsTime().String())
log.Printf("progress : %.2f", response.GetProgressPercentage().GetValue())
log.Printf("state : %s", response.GetState().String())
}
View on GitHub