Hey there, fellow Go enthusiast! Ready to supercharge your productivity with Any.do? Let's dive into building an awesome API integration that'll have you managing tasks like a pro. We'll cover everything from basic setup to some nifty advanced features, so buckle up!
Before we jump in, make sure you've got:
Let's kick things off by creating a new Go project:
mkdir anydo-integration cd anydo-integration go mod init github.com/yourusername/anydo-integration
Now, let's grab the packages we'll need:
go get github.com/go-resty/resty/v2
First things first, let's get you authenticated. Head over to Any.do and snag your API key. Got it? Great! Now let's put it to use:
package main import ( "github.com/go-resty/resty/v2" ) const ( baseURL = "https://sm-prod2.any.do/api/v2" apiKey = "your-api-key-here" ) func main() { client := resty.New(). SetBaseURL(baseURL). SetHeader("X-API-KEY", apiKey) // We'll use this client for all our requests }
Now that we're all set up, let's start making some requests!
resp, err := client.R(). SetResult([]Task{}). Get("/me/tasks") if err != nil { log.Fatalf("Error fetching tasks: %v", err) } tasks := resp.Result().(*[]Task) fmt.Printf("Got %d tasks\n", len(*tasks))
newTask := Task{ Title: "Learn Go", DueDate: time.Now().Add(24 * time.Hour), } resp, err := client.R(). SetBody(newTask). SetResult(&Task{}). Post("/me/tasks") if err != nil { log.Fatalf("Error creating task: %v", err) } createdTask := resp.Result().(*Task) fmt.Printf("Created task: %s\n", createdTask.Title)
taskID := "your-task-id" updateTask := map[string]interface{}{ "title": "Master Go", } resp, err := client.R(). SetBody(updateTask). SetResult(&Task{}). Put("/me/tasks/" + taskID) if err != nil { log.Fatalf("Error updating task: %v", err) } updatedTask := resp.Result().(*Task) fmt.Printf("Updated task: %s\n", updatedTask.Title)
taskID := "your-task-id" resp, err := client.R(). Delete("/me/tasks/" + taskID) if err != nil { log.Fatalf("Error deleting task: %v", err) } fmt.Println("Task deleted successfully")
The resty
library makes handling responses a breeze. We've been using SetResult()
to automatically parse JSON responses into structs. For error handling:
resp, err := client.R(). SetResult(&Task{}). Get("/me/tasks/non-existent-id") if err != nil { log.Fatalf("Request failed: %v", err) } if resp.IsError() { log.Fatalf("API error: %s", resp.String()) }
resp, err := client.R(). SetResult([]List{}). Get("/me/lists") if err != nil { log.Fatalf("Error fetching lists: %v", err) } lists := resp.Result().(*[]List) fmt.Printf("Got %d lists\n", len(*lists))
resp, err := client.R(). SetResult([]Tag{}). Get("/me/tags") if err != nil { log.Fatalf("Error fetching tags: %v", err) } tags := resp.Result().(*[]Tag) fmt.Printf("Got %d tags\n", len(*tags))
Here's a quick example of exponential backoff:
func exponentialBackoff(operation func() error) error { backoff := 100 * time.Millisecond for i := 0; i < 5; i++ { err := operation() if err == nil { return nil } time.Sleep(backoff) backoff *= 2 } return fmt.Errorf("operation failed after 5 attempts") }
Don't forget to test your integration! Here's a quick example using the httptest
package:
func TestFetchTasks(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Write([]byte(`[{"id": "123", "title": "Test Task"}]`)) })) defer server.Close() client := resty.New().SetBaseURL(server.URL) resp, err := client.R().SetResult([]Task{}).Get("/me/tasks") assert.NoError(t, err) assert.Equal(t, http.StatusOK, resp.StatusCode()) tasks := resp.Result().(*[]Task) assert.Len(t, *tasks, 1) assert.Equal(t, "Test Task", (*tasks)[0].Title) }
And there you have it! You've just built a rock-solid Any.do API integration in Go. From basic CRUD operations to advanced features and best practices, you're now equipped to take your task management to the next level.
Remember, this is just the beginning. Feel free to explore more of the Any.do API and extend this integration to fit your specific needs. Happy coding, and may your tasks always be organized!
Now go forth and conquer those tasks with your shiny new Go integration!