Hey there, fellow Go enthusiast! Ready to dive into the world of Tumblr API integration? You're in for a treat. We'll be using the tumblrclient.go
package to make our lives easier. Tumblr's API is a powerhouse, letting you do everything from fetching user info to creating posts programmatically. Let's get cracking!
Before we jump in, make sure you've got:
tumblrclient.go
package (a quick go get
will do the trick)First things first, let's create a new Go project:
mkdir tumblr-integration && cd tumblr-integration go mod init tumblr-integration
Now, let's import the necessary packages in our main.go
:
package main import ( "fmt" "github.com/tumblr/tumblrclient.go" ) func main() { // We'll fill this in soon! }
Time to get our hands dirty with some authentication:
client := tumblrclient.NewClient( "YOUR_CONSUMER_KEY", "YOUR_CONSUMER_SECRET", "YOUR_TOKEN", "YOUR_TOKEN_SECRET", ) if err := client.Authenticate(); err != nil { panic("Auth failed: " + err.Error()) }
Pro tip: Don't actually panic in production code. Handle errors gracefully!
Let's start with some basic operations:
user, err := client.GetUserInfo() if err != nil { fmt.Println("Error fetching user info:", err) return } fmt.Printf("Hello, %s!\n", user.Name)
posts, err := client.GetPosts("yourblogname.tumblr.com", nil) if err != nil { fmt.Println("Error fetching posts:", err) return } fmt.Printf("Found %d posts\n", len(posts.Posts))
params := map[string]string{ "type": "text", "title": "My awesome Go-powered post", "body": "Look ma, I'm posting from Go!", } post, err := client.CreatePost("yourblogname.tumblr.com", params) if err != nil { fmt.Println("Error creating post:", err) return } fmt.Printf("Created post with ID: %s\n", post.Id)
Tumblr uses offset-based pagination. Here's how to handle it:
offset := 0 limit := 20 for { params := map[string]string{ "offset": fmt.Sprintf("%d", offset), "limit": fmt.Sprintf("%d", limit), } posts, err := client.GetPosts("yourblogname.tumblr.com", params) if err != nil { fmt.Println("Error fetching posts:", err) break } // Process posts... if len(posts.Posts) < limit { break // We've reached the end } offset += limit }
Tumblr supports various post types. Here's how to create a photo post:
params := map[string]string{ "type": "photo", "caption": "Check out this awesome Go gopher!", "source": "http://example.com/gopher.jpg", } post, err := client.CreatePost("yourblogname.tumblr.com", params) // Handle error and success...
Write unit tests for your API calls. Here's a quick example using the httptest
package:
func TestGetUserInfo(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Write([]byte(`{"response": {"user": {"name": "TestUser"}}}`)) })) defer server.Close() // Use the test server URL instead of the real Tumblr API client := tumblrclient.NewClient("", "", "", "") client.BaseURL = server.URL user, err := client.GetUserInfo() if err != nil { t.Fatalf("Expected no error, got %v", err) } if user.Name != "TestUser" { t.Errorf("Expected user name 'TestUser', got '%s'", user.Name) } }
When deploying your Tumblr API integration:
And there you have it! You're now equipped to build a robust Tumblr API integration in Go. Remember, the tumblrclient.go
package documentation is your friend for more advanced usage. Now go forth and create something awesome!
Happy coding, Gophers! 🚀