This post will introduce you to devtogo and will show you some usage examples.
I've been using Go as my main programming language for about six months at work. It's been very enjoyable and I always learn something new every day.
I wrote devtogo as a means for me to learn how to write some good opensource Go code. It's also my very first opensource project that I am publishing. Much of my work is sitting in private repositories, so I thought that I may as well expose this for the world to use.
Try it out and contribute/interact with the Github repo.
ShiraazMoollatjie / devtogo
A REST API Wrapper for the dev.to api in go
devto-go
devto-go is a REST API Wrapper for the dev.to api written in go.
Usage
Import the package into your go file:
import "github.com/ShiraazMoollatjie/devtogo"
Thereafter, create a client and specify your API token:
cl := devtogo.NewClient(devtogo.WithApiKey("MY_API_KEY"))
It is also possible to not use an API key for anonymous operations.
Retrieving articles
To retrieve a list of articles, simply use the Articles
function:
articles, err := cl.Articles(devtogo.Defaults())
It is also possible for us to add query parameters. For example, it's useful to retrieve articles for a specific tag
.
The way to do this would be:
al, err := cl.Articles(devtogo.Arguments{
"tag": "go",
})
To retrieve a single article, you need to specify the article id
:
article, err := client.GetArticle("167919")
Overview
Devtogo is an rest api wrapper for the dev.to api. It can be used to publish articles or even search for articles of a specific tag. There are many usecases for using the dev.to api.
Creating a client
A client can be created anonymously or via credentials. The only credentials supported at the moment are api keys
and one can can obtain them by having a look at the authentication section of the api.
To create a client, we simply use the NewClient
function and specify the api key.
cl := devtogo.NewClient(devtogo.WithApiKey("MY_API_KEY"))
Retrieving articles
It's possible to retrieve either a list of articles or a single article. For this post, we are going to retrieve all the articles that are tagged with go
al, err := cl.GetArticles(devtogo.Arguments{
"tag": "go",
})
Retrieving articles owned by you
If we are an authenticated user, we can retrieve our own list of published or unpublished articles. This is useful for obtaining a list of our drafts that we want to publish later.
al, err := cl.GetMyUnpublishedArticles(devtogo.Defaults())
if err != nil {
panic(err)
}
Creating and updating articles
To create an article, we use the CreateArticle
function. This will create a new article on the dev.to platform.
np, err := cl.CreateArticle(devtogo.CreateArticle{
Title: "My new dev.to post",
Tags: []string{"go"},
BodyMarkdown: "my long markdown article that is preferably read from a file",
})
In the above example, we create an article that is in a draft
state with a title
, some tags
and some markdown.
To publish this article on create, we set the Published
property to true.
Supposed we want to update this article by publishing it, we simply use the UpdateArticle
function.
ua, err := cl.UpdateArticle(np.ID, devtogo.CreateArticle{
Title: "My updates dev.to post using the API",
BodyMarkdown: "my new updated content",
Published: true,
})
Conclusion
In this article, we learnt how to use the devtogo to interact with the dev.to platform using the Go programming language. We also about how to use devtogo.
Full example
This is a full working example that retrieves the latest Go articles.
package main
import (
"fmt"
"github.com/ShiraazMoollatjie/devtogo"
"log"
"os"
)
func main() {
apiToken := os.Getenv("DEVTO_TOKEN")
if apiToken == "" {
log.Fatalf("Set the DEVTO_TOKEN")
}
dtgc := devtogo.NewClient(devtogo.WithApiKey(apiToken))
al, err := dtgc.GetArticles(devtogo.Arguments{
"tag": "go",
})
if err != nil {
log.Fatalf("Cannot retrieve articles: %+v", err)
}
for _,a :=range al {
fmt.Printf("Title: %s, Url: %s\n", a.Title, a.URL)
}
}
Top comments (0)