togglapi

package module
v0.4.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 3, 2016 License: Apache-2.0 Imports: 11 Imported by: 3

README

Toggl API for go

A go wrapper for the Toggl API

github.com/andreaskoch/togglapi is a simple wrapper for the Toggl API (https://github.com/toggl/toggl_api_docs).

Build Status

Installation

Download github.com/andreaskoch/togglapi:

go get github.com/andreaskoch/togglapi

Supported API methods

github.com/andreaskoch/togglapi currently only supports the following methods of the Toggl API:

  • Clients
    • CreateClient(client Client) (Client, error)
    • GetClients() ([]Client, error)
  • Workspaces
    • GetWorkspaces() ([]Workspace, error)
  • Projects
    • CreateProject(project Project) (Project, error)
    • GetProjects(workspaceID int) ([]Project, error)
  • Time Entries
    • CreateTimeEntry(timeEntry TimeEntry) (TimeEntry, error)
    • GetTimeEntries(start, end time.Time) ([]TimeEntry, error)

I might add the missing methods in the future, but if you need them now please add them and send me a pull-request.

Usage

package main

import (
	"time"

	"github.com/andreaskoch/togglapi"
)

func main() {
	apiToken := "Your-API-Token"
	baseURL := "https://www.toggl.com/api/v8"
	api := togglapi.NewAPI(baseURL, apiToken)

  // workspaces
	workspaces, workspacesError := api.GetWorkspaces()
	...

  // clients
	clients, clientsError := api.GetClients()
  ...

  // projects by workspace
  for _, workspace := range workspaces {
		projects, projectsError := api.GetProjects(workspace.ID)
		...
	}

	// time entries
	stop := time.Now()
	start := stop.AddDate(0, -1, 0)
	timeEntries, timeEntriesError := api.GetTimeEntries(start, stop)
  ...
}

You can also have a look at the example command line utility: example/main.go

cd $GOPATH/src/github.com/andreaskoch/togglapi/example
go run main.go Your-Toggl-API-Token

Development

Run the unit tests:

cd $GOPATH/src/github.com/andreaskoch/togglapi
make test

Create code coverage reports:

cd $GOPATH/src/github.com/andreaskoch/togglapi
make coverage

Licensing

TogglCSV is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.

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 := 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("")
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAPI

func NewAPI(baseURL, token string) model.TogglAPI

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)
}
Output:

func NewClientAPI added in v0.2.0

func NewClientAPI(baseURL, token string) model.ClientAPI

NewClientAPI create a new client for the Toggl client API.

func NewProjectAPI added in v0.2.0

func NewProjectAPI(baseURL, token string) model.ProjectAPI

NewProjectAPI create a new client for the Toggl project API.

Example

If you are only interested in the Project API you can instantiate a ProjectAPI using the NewProjectAPI function.

apiToken := "Your-Toggl-API-Token"
baseURL := "https://www.toggl.com/api/v8"

workspaceAPI := NewWorkspaceAPI(baseURL, apiToken)
workspaces, workspacesError := workspaceAPI.GetWorkspaces()
if workspacesError != nil {
	fmt.Fprintf(os.Stderr, "Failed to get workspaces: %s", workspacesError)
	return
}

projectRepository := NewProjectAPI(baseURL, apiToken)
for _, workspace := range workspaces {

	projects, projectsError := projectRepository.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)
	}
}
Output:

func NewTimeEntryAPI added in v0.2.0

func NewTimeEntryAPI(baseURL, token string) model.TimeEntryAPI

NewTimeEntryAPI 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 TimeEntryAPI using the NewTimeEntryAPI function.

apiToken := "Your-Toggl-API-Token"
baseURL := "https://www.toggl.com/api/v8"
timeEntryRepository := NewTimeEntryAPI(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)
}
Output:

func NewWorkspaceAPI added in v0.2.0

func NewWorkspaceAPI(baseURL, token string) model.WorkspaceAPI

NewWorkspaceAPI create a new client for the Toggl workspace API.

Example

If you are only interested in the Workspace API you can instantiate a WorkspaceAPI using the NewWorkspaceAPI function.

apiToken := "Your-Toggl-API-Token"
baseURL := "https://www.toggl.com/api/v8"
workspaceRepository := NewWorkspaceAPI(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)
}
Output:

Types

type API

API provides functions for interacting with the Toggl API.

type ClientAPI added in v0.2.0

type ClientAPI struct {
	// contains filtered or unexported fields
}

ClientAPI provides functions for interacting with Toggls' client API.

func (*ClientAPI) CreateClient added in v0.2.0

func (repository *ClientAPI) CreateClient(client model.Client) (model.Client, error)

CreateClient creates a new client.

func (*ClientAPI) GetClients added in v0.2.0

func (repository *ClientAPI) GetClients() ([]model.Client, error)

GetClients returns all clients for the given workspace.

type ProjectAPI added in v0.2.0

type ProjectAPI struct {
	// contains filtered or unexported fields
}

ProjectAPI provides functions for interacting with Toggls' project API.

func (*ProjectAPI) CreateProject added in v0.2.0

func (repository *ProjectAPI) CreateProject(project model.Project) (model.Project, error)

CreateProject creates a new project.

func (*ProjectAPI) GetProjects added in v0.2.0

func (repository *ProjectAPI) 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 TimeEntryAPI added in v0.2.0

type TimeEntryAPI struct {
	// contains filtered or unexported fields
}

TimeEntryAPI provides functions for interacting with Toggls' time entry API.

func (*TimeEntryAPI) CreateTimeEntry added in v0.2.0

func (repository *TimeEntryAPI) CreateTimeEntry(timeEntry model.TimeEntry) (model.TimeEntry, error)

CreateTimeEntry creates a new time entry.

func (*TimeEntryAPI) GetTimeEntries added in v0.2.0

func (repository *TimeEntryAPI) 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 WorkspaceAPI added in v0.2.0

type WorkspaceAPI struct {
	// contains filtered or unexported fields
}

WorkspaceAPI provides functions for interacting with Toggls' workspace API.

func (*WorkspaceAPI) GetWorkspaces added in v0.2.0

func (repository *WorkspaceAPI) 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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL