[PYTHON] Create a pseudo REST API server using GitHub Pages

Introduction

Prepare a pseudo API server using Qiita --GitHub Actions In this article, I suggested that the fact that the json file can be automatically updated using Actions and that it can be read by Pages means that it can be operated as a pseudo API server. However, with this, even though only a small part of the huge amount of data is desired, it is necessary to acquire all of it each time and trace it from the key.

So why not split the json file, how?

REST API I checked REST API from Qiita --0 I understand that the REST API that I often hear in the streets is URL routing that conforms to data, so I will divide it based on this.

sample.json (Data source: Processed data obtained from Hokkaido Open Data Portal)



{
    "data": [
        {
            "no": "1",
            "date": "2020-01-28T00:00",
            "place": "Wuhan, China",
            "age": "Forties",
            "sex": "Female"
        },
        {
            "no": "2",
            "date": "2020-02-14T00:00",
            "place": "Sapporo",
            "age": "50s",
            "sex": "male"
        }
    ],
    "last_update": "2020-03-14T23:14:01.849130+09:00"
}

The hosting URL is http: // {HOSTING} /.

Data to be acquired REST API URL
sample.All json data http://{HOSTING}/sample
sample.json last_update http://{HOSTING}/sample/last_update
sample.The first element of json data http://{HOSTING}/sample/data/0
sample.The second element of json data http://{HOSTING}/sample/data/1
sample.No of the second element of json data http://{HOSTING}/sample/data/1/no

If it is, it is a REST translation.

Create a pseudo REST API on GitHub Pages

On GitHub Pages, directory access is directed to index.html. In other words, if you access http: // {HOSTING} / sample, write all the data of sample.json in the root /sample/index.html of the gh-pages branch, and the JSON string of the desired data will be returned. It is a translation. http://{HOSTING}/sample/last_updateの場合はルート/sample/last_update/index.htmlに「2020-03-14T23:14:01.849130+09:00」という文字列を書き込んでおく。

If you host with the above procedure, you can say that you have a pseudo REST API server.

Implementation

Kanahiro/gh-pages-rest-api In this script, main.py is executed by Actions when pushed to master. main.py reads the json file in the json folder and puts index.html in gh-pages in the subdivided directory as described above.

The above sample.json is stored in the json folder of the above repository. The hosting URL for gh-pages is https \ //kanahiro.github.io/gh-pages-rest-api/

Data to be acquired REST API URL
sample.All json data https://kanahiro.github.io/gh-pages-rest-api/sample
sample.json last_update https://kanahiro.github.io/gh-pages-rest-api/sample/last_update
sample.The first element of json data https://kanahiro.github.io/gh-pages-rest-api/sample/data/0
sample.The second element of json data https://kanahiro.github.io/gh-pages-rest-api/sample/data/1
sample.No of the second element of json data https://kanahiro.github.io/gh-pages-rest-api/sample/data/1/no

It will be automatically hosted after pushing to master as follows: The only request you can make is GET, but it can be said that the REST API is implemented in GitHub Pages.

And as introduced in Qiita --Preparing a pseudo API server using GitHub Actions, if the json file can be updated automatically by scheduling by Actions , Becomes a REST API server where data is updated automatically.

Reuse

  1. Fork the above repository
  2. Put the json you want to host in the json folder
  3. Push to master (→ data is automatically output to gh-pages)

Notes

――Since I just made it, I haven't tried to read it properly on the front side. ――I have just made it, so I don't know what kind of problem it has. --Json key recommends only alphabetic characters (unconfirmed how URL is escaped in Japanese)

Recommended Posts

Create a pseudo REST API server using GitHub Pages
Prepare a pseudo API server using GitHub Actions
Let's create a REST API using SpringBoot + MongoDB
Create a (simple) REST server
Create a CRUD API using FastAPI
Easy to create API server using go-json-rest module
How to create a Rest Api in Django
Create a REST API using the model learned in Lobe and TensorFlow Serving.
Learning neural networks using Chainer-Creating a Web API server
Create a simple textlint server
Build a web API server at explosive speed using hug
Create a real-time auto-reply bot using the Twitter Streaming API
Get Salesforce data using REST API
Create an API that returns data from a model using turicreate
Create a python GUI using tkinter
Create a nested dictionary using defaultdict
Create a TalkBot easily using Discord.py and A3RT's Talk API (pya3rt).
Create a Unix Domain Socket server
Create API using hug with mod_wsgi
Create github pages with lektor Part 1
[Python] I wrote a REST API using AWS API Gateway and Lambda.
I made a Chatbot using LINE Messaging API and Python (2) ~ Server ~
Try to create a Qiita article with REST API [Environmental preparation]
Create a REST API to operate dynamodb with the Django REST Framework
Create a C wrapper using Boost.Python
How to create a clone from Github
Easily build a DNS server using Twisted
Create a graph using the Sympy module
[Python] Create a Batch environment using AWS-CDK
Set up a mail server using Twisted
Create an application using the Spotify API
Create a dataframe from excel using pandas
FX data collection using OANDA REST API
Create a GIF file using Pillow in Python
Create a beauty pageant support app using PyLearn2
Create a phylogenetic tree from Biopyton using ClustalW2
Write a TCP server using the SocketServer module
Create a binary data parser using Kaitai Struct
Output repository list using Github API on Mac
Try to create an HTTP server using Node.js
Create a visitor notification system using Raspberry Pi
Create a Mac app using py2app and Python3! !!
Create a MIDI file in Python using pretty_midi
Create a "Hello World" (HTTP) server with Tornado
Create a web server in Go language (net/http) (2)
Create an API server quickly with Python + Falcon
Create a GUI on the terminal using curses