I tried hitting the Qiita API from go

Introduction

With a sense of mission to study the Go language, I decided to play with it.

golang + Qiita API

environment

・ Go version go1.13 darwin / amd64

URI and function

Path HTTP method function
/qiita/api/v1/user-info GET Returns a list of articles for the specified user in descending order of creation date and time.

Package used

** ・ Gin ** gin is a web framework made by go. It is an excellent one that also has a json response processing function.

go get github.com/gin-gonic/gin

file organization

❯ tree .
.
├── main.go
└── src
    └── controller
        └── controller.go

2 directories, 2 files

controller It summarizes the task processing called from main.

controller.go


package controller

import (
	"encoding/json"
	"io/ioutil"
	"log"
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
)

type Item struct {
	Title     string    `json:"title"`
	CreatedAt time.Time `json:"created_at"`
}

//Hit the Qiita API
func QiitaGET(c *gin.Context) {
	resp, err := http.Get("http://qiita.com/api/v2/users/{Own account name}/items?page=1&per_page=10")
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}

	var data []Item

	if err := json.Unmarshal(body, &data); err != nil {
		log.Fatal(err)
	}

	c.JSON(http.StatusOK, gin.H{"item": data})
}

main Described the main process.

main.go


package main

import (
	"./src/controller"
	"github.com/gin-gonic/gin"
)

func main() {
	router := gin.Default()

	v1 := router.Group("/qiita/api/v1")
	{
		v1.GET("/user-info", controller.QiitaGET)
	}
	router.Run(":9000")
}

Run

❯ go run main.go
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /qiita/api/v1/user-info   --> _/Users/********/Desktop/Qiita_PoC_API/src/controller.QiitaGET (3 handlers)
[GIN-debug] Listening and serving HTTP on :9000

When hitting the API, I used postMan. スクリーンショット 2020-10-12 23.22.24.png

reference

Qiita API

Recommended Posts

I tried hitting the Qiita API from go
I touched the Qiita API
I tried hitting the API with echonest's python client
I tried the Naro novel API 2
I tried the Naruro novel API
I tried using the checkio API
I tried the site "Deploy Azure virtual machine from Go"
I tried to get various information from the codeforces API
I tried using UnityCloudBuild API from Python
I tried to touch the COTOHA API
I tried using the BigQuery Storage API
I tried calling the prediction API of the machine learning model from WordPress
I tried using the Google Cloud Vision API
I tried to touch the API of ebay
I tried to get the authentication code of Qiita API with Python.
Qiita Job I tried to analyze the job offer
I tried to detect the iris from the camera image
I tried using the API of the salmon data project
I tweeted from the terminal!
I tried the changefinder library!
[First COTOHA API] I tried to summarize the old story
I tried to create API list.csv in Python from swagger.yaml
I tried using the Python library from Ruby with PyCall
I tried face recognition from the video (OpenCV: python version)
I tried saving the DRF API request history with django-request
I tried touching the multi-vendor router control API library NAPALM
[Python] I tried collecting data using the API of wikipedia
I tried the Google Cloud Vision API for the first time
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
[For beginners] I tried using the Tensorflow Object Detection API
I tried the TensorFlow tutorial 1st
I tried to create Quip API
I tried the TensorFlow tutorial 2nd
I tried to touch Tesla's API
I tried Grumpy (Go running Python).
Use the Flickr API from Python
I tried task queuing from Celery
I tried to move the ball
I tried to estimate the interval.
I tried scraping the ranking of Qiita Advent Calendar with Python
I tried using the COTOHA API (there is code on GitHub)
I made a Twitter Bot with Go x Qiita API x Lambda
I tried sending an email from the Sakura server with flask-mail
Continuation ・ I tried touching the multi-vendor router control API library NAPALM
I tried to cut out a still image from the video
[Qiita API] [Statistics • Machine learning] I tried to summarize and analyze the articles posted so far.
I tried hitting the Google API with Ruby and Python-Make the database a Spreadsheet and manage it with Google Drive
I tried the TensorFlow tutorial MNIST 3rd
I tried scraping conversation data from Askfm
I tried the asynchronous server of Django 3.0
I tried using Twitter api and Line api
I tried to execute SQL from the local environment using Looker SDK
I tried to summarize the umask command
I tried tensorflow for the first time
I tried to extract characters from subtitles (OpenCV: Google Cloud Vision API)
I tried to recognize the wake word
I tried LeetCode every day 26. Remove Duplicates from Sorted Array (Python, Go)
I tried using YOUTUBE Data API V3
I stumbled on the Hatena Keyword API
I tried the OSS visualization tool, superset
I tried to summarize the graphical modeling.