Documentation
¶
Overview ¶
Package togglapi provides access to Toggls' time tracking API. The togglapi package provides functions for creating and retrieving workspaces, clients, projects and time entries.
To learn more about the Toggl API visit: https://github.com/toggl/toggl_api_docs
Example ¶
This example shows how you can use the Toggl API to print all your workspaces names, client names, project names and the time entries of the last month.
baseURL := "https://www.toggl.com/api/v8"
apiToken := "Toggl-API-Token"
api := togglapi.NewAPI(baseURL, apiToken)
// print workspace names
fmt.Println("Workspaces:")
workspaces, workspacesError := api.GetWorkspaces()
if workspacesError != nil {
fmt.Fprintf(os.Stderr, "Failed to get workspaces: %s", workspacesError)
return
}
for _, workspace := range workspaces {
fmt.Println(workspace.Name)
}
fmt.Println("")
// print client names
fmt.Println("Clients:")
clients, clientsError := api.GetClients()
if clientsError != nil {
fmt.Fprintf(os.Stderr, "Failed to get clients: %s", clientsError)
return
}
for _, client := range clients {
fmt.Println(client.Name)
}
fmt.Println("")
// print project names
fmt.Println("Projects:")
for _, workspace := range workspaces {
projects, projectsError := api.GetProjects(workspace.ID)
if projectsError != nil {
fmt.Fprintf(os.Stderr, "Failed to get projects: %s", projectsError)
return
}
for _, project := range projects {
fmt.Println(project.Name)
}
}
fmt.Println("")
// print time entries
fmt.Println("Time Entries:")
stop := time.Now()
start := stop.AddDate(0, -1, 0)
timeEntries, timeEntriesError := api.GetTimeEntries(start, stop)
if timeEntriesError != nil {
fmt.Fprintf(os.Stderr, "Failed to get timeEntries: %s", timeEntriesError)
return
}
for _, timeEntry := range timeEntries {
fmt.Printf("%s - %s: %s\n", timeEntry.Start, timeEntry.Stop, timeEntry.Description)
}
fmt.Println("")
Index ¶
- func NewAPI(baseURL, token string) model.TogglAPI
- func NewClientRepository(baseURL, token string) model.ClientRepository
- func NewProjectRepository(baseURL, token string) model.ProjectRepository
- func NewTimeEntryRepository(baseURL, token string) model.TimeEntryRepository
- func NewWorkspaceRepository(baseURL, token string) model.WorkspaceRepository
- type API
- type RESTClientRepository
- type RESTProjectRepository
- type RESTRequester
- type RESTTimeEntryRepository
- type RESTWorkspaceRepository
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewAPI ¶
NewAPI create a new instance of the Toggl API.
Example ¶
To initialize a new Toggl API instance use the NewAPI function and pass Toggl API URL (e.g. "https://www.toggl.com/api/v8") and your personal API token (e.g. "12jkhjh2j3jkj23").
baseURL := "https://www.toggl.com/api/v8"
apiToken := "Your-API-Token"
api := NewAPI(baseURL, apiToken)
stop := time.Now()
start := stop.AddDate(0, -1, 0)
timeEntries, timeEntriesError := api.GetTimeEntries(start, stop)
if timeEntriesError != nil {
fmt.Fprintf(os.Stderr, "Failed to get timeEntries: %s", timeEntriesError)
return
}
for _, timeEntry := range timeEntries {
fmt.Printf("%s - %s: %s\n", timeEntry.Start, timeEntry.Stop, timeEntry.Description)
}
func NewClientRepository ¶
func NewClientRepository(baseURL, token string) model.ClientRepository
NewClientRepository create a new client for the Toggl client API.
func NewProjectRepository ¶
func NewProjectRepository(baseURL, token string) model.ProjectRepository
NewProjectRepository create a new client for the Toggl project API.
Example ¶
If you are only interested in the Project API you can instantiate a ProjectRepository using the NewProjectRepository function.
apiToken := "Your-Toggl-API-Token"
baseURL := "https://www.toggl.com/api/v8"
projectRepository := NewProjectRepository(baseURL, apiToken)
workspaces, workspacesError := projectRepository.GetWorkspaces()
if workspacesError != nil {
fmt.Fprintf(os.Stderr, "Failed to get workspaces: %s", workspacesError)
return
}
for _, workspace := range workspaces {
projects, projectsError := api.GetProjects(workspace.ID)
if projectsError != nil {
fmt.Fprintf(os.Stderr, "Failed to get projects: %s", projectsError)
return
}
for _, project := range projects {
fmt.Println(project.Name)
}
}
func NewTimeEntryRepository ¶
func NewTimeEntryRepository(baseURL, token string) model.TimeEntryRepository
NewTimeEntryRepository create a new client for the Toggl time entry API.
Example ¶
If you are only interested in the Time Entry API you can instantiate a TimeEntryRepository using the NewTimeEntryRepository function.
apiToken := "Your-Toggl-API-Token"
baseURL := "https://www.toggl.com/api/v8"
timeEntryRepository := NewTimeEntryRepository(baseURL, apiToken)
stop := time.Now()
start := stop.AddDate(0, -1, 0)
timeEntries, timeEntriesError := timeEntryRepository.GetTimeEntries(start, stop)
if timeEntriesError != nil {
fmt.Fprintf(os.Stderr, "Failed to get timeEntries: %s", timeEntriesError)
return
}
for _, timeEntry := range timeEntries {
fmt.Printf("%s - %s: %s\n", timeEntry.Start, timeEntry.Stop, timeEntry.Description)
}
func NewWorkspaceRepository ¶
func NewWorkspaceRepository(baseURL, token string) model.WorkspaceRepository
NewWorkspaceRepository create a new client for the Toggl workspace API.
Example ¶
If you are only interested in the Workspace API you can instantiate a WorkspaceRepository using the NewWorkspaceRepository function.
apiToken := "Your-Toggl-API-Token"
baseURL := "https://www.toggl.com/api/v8"
workspaceRepository := NewWorkspaceRepository(baseURL, apiToken)
workspaces, workspacesError := workspaceRepository.GetWorkspaces()
if workspacesError != nil {
fmt.Fprintf(os.Stderr, "Failed to get workspaces: %s", workspacesError)
return
}
for _, workspace := range workspaces {
fmt.Println(workspace.Name)
}
Types ¶
type API ¶
type API struct {
model.WorkspaceRepository
model.ProjectRepository
model.TimeEntryRepository
model.ClientRepository
}
API provides functions for interacting with the Toggl API.
type RESTClientRepository ¶
type RESTClientRepository struct {
// contains filtered or unexported fields
}
RESTClientRepository provides functions for interacting with Toggls' client API.
func (*RESTClientRepository) CreateClient ¶
CreateClient creates a new client.
func (*RESTClientRepository) GetClients ¶
func (repository *RESTClientRepository) GetClients() ([]model.Client, error)
GetClients returns all clients for the given workspace.
type RESTProjectRepository ¶
type RESTProjectRepository struct {
// contains filtered or unexported fields
}
RESTProjectRepository provides functions for interacting with Toggls' project API.
func (*RESTProjectRepository) CreateProject ¶
func (repository *RESTProjectRepository) CreateProject(project model.Project) (model.Project, error)
CreateProject creates a new project.
func (*RESTProjectRepository) GetProjects ¶
func (repository *RESTProjectRepository) GetProjects(workspaceID int) ([]model.Project, error)
GetProjects returns all projects for the given workspace.
type RESTRequester ¶
type RESTRequester interface {
// Request sends an HTTP request with the given parameters (method, route, payload)
// to an REST API and returns the APIs' response or an error if the request failed.
Request(method, route string, payload io.Reader) ([]byte, error)
}
The RESTRequester interface provides a function for sending HTTP requests to REST APIs.
type RESTTimeEntryRepository ¶
type RESTTimeEntryRepository struct {
// contains filtered or unexported fields
}
RESTTimeEntryRepository provides functions for interacting with Toggls' time entry API.
func (*RESTTimeEntryRepository) CreateTimeEntry ¶
func (repository *RESTTimeEntryRepository) CreateTimeEntry(timeEntry model.TimeEntry) (model.TimeEntry, error)
CreateTimeEntry creates a new time entry.
func (*RESTTimeEntryRepository) GetTimeEntries ¶
func (repository *RESTTimeEntryRepository) GetTimeEntries(start, end time.Time) ([]model.TimeEntry, error)
GetTimeEntries returns all time entries created between the given start and end date. Returns nil and an error if the time entries could not be retrieved.
type RESTWorkspaceRepository ¶
type RESTWorkspaceRepository struct {
// contains filtered or unexported fields
}
RESTWorkspaceRepository provides functions for interacting with Toggls' workspace API.
func (*RESTWorkspaceRepository) GetWorkspaces ¶
func (repository *RESTWorkspaceRepository) GetWorkspaces() ([]model.Workspace, error)
GetWorkspaces returns all workspaces for the current user.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package date provides functions for parsing and formatting dates according the requirements of Toggl (ISO 8601).
|
Package date provides functions for parsing and formatting dates according the requirements of Toggl (ISO 8601). |
|
Package main implements a simple sample implementation of the Toggl API for go (github.com/andreaskoch/togglapi).
|
Package main implements a simple sample implementation of the Toggl API for go (github.com/andreaskoch/togglapi). |
|
Package model contains the models and interface for the Toggl API
|
Package model contains the models and interface for the Toggl API |