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.
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.
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.
――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