I tried to create API list.csv in Python from swagger.yaml

Environment

By the way, I wrote an article on Qiita for the first time.

Premise

MacBook Pro macOS Catalina 10.15.7

Introduction

I have installed the following.

  1. Docker

reference:

Setting

  1. Enable VS Code sync settings

work

  1. Create a repository on Github
  2. At hand, copy microsoft / vscode-remote-try-python (git clone)
  3. Change the remote destination to your repository with the folder name and git remote set-url

Error handling

Because an error occurred when installing the library or using Jupiter, For the time being, I commented out to leave the user as root.

Library installation

Use PyYAML library and CSV library. It's very easy and wonderful because you just write it in requirements.txt and rebuild And Rerun.

requirements.txt


flask
pyyaml
csv
ipykernel

coding

Open yaml, Collect the contents you want to write to CSV in order, Save as CSV accomplished.

Since yaml is read as a dictionary type, The point was to describe the loop and variable acquisition so that they would meet the desired conditions.

open-api-yaml-to-csv.py


import yaml
import csv

#Store the contents of yaml file in dictionary type variable
with open("oidc-swagger.yaml", "r") as yf:
    data = yaml.safe_load(yf)

#Extract necessary information from the dictionary and store it as an array
api_list_array = []
i = 0
api_list_array.append(["#", "operationId", "METHOD", "PATH"])
for path in data['paths'].keys():
    if path != "/swagger":
        for verb in data['paths'][path].keys():
            # if 'operationId' in data['paths'][path][verb]:
            name = data['paths'][path][verb]['operationId']
            i = i+1
            # print(i,name, verb, path)
            api_list_array.append([i, name, verb, path])

#Save the array as CSV
with open('list.csv', 'w') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerows(api_list_array)

This was my first post. I had a hard time building the environment, so I'll add a little more.

Recommended Posts

I tried to create API list.csv in Python from swagger.yaml
I tried to create Quip API
I tried to implement PLSA in Python
I tried to implement permutation in Python
I tried to implement PLSA in Python 2
I tried using UnityCloudBuild API from Python
I tried to implement ADALINE in Python
I tried to implement PPO in Python
I want to create a window in Python
I tried to implement TOPIC MODEL in Python
I tried to implement selection sort in python
I tried to graph the packages installed in Python
I tried to implement a pseudo pachislot in Python
I tried to implement Dragon Quest poker in Python
I tried to implement GA (genetic algorithm) in Python
I tried to summarize how to use pandas in python
I tried to create a class that can easily serialize Json in Python
Python beginners tried it in 3 days from OS installation to running Twitter API
I tried to refactor the template code posted in "Getting images from Flickr API with Python" (Part 2)
I tried to touch Python (installation)
I tried to implement a one-dimensional cellular automaton in Python
Create folders from '01' to '12' with python
I tried "How to get a method decorated in Python"
Create Gmail in Python without API
I tried to touch Tesla's API
I tried to implement the mail sending function in Python
I tried to make a stopwatch using tkinter in python
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
I tried Line notification in Python
I tried to get various information from the codeforces API
I tried to implement blackjack of card game in Python
[Python] Create API to send Gmail
I tried to create an article in Wiki.js with SQLAlchemy
I tried to create a RESTful API by connecting the explosive Python framework FastAPI to MySQL.
I tried to create a Python script to get the value of a cell in Microsoft Excel
I tried to create a list of prime numbers with python
I tried to implement a misunderstood prisoner's dilemma game in Python
Various ways to create an array of numbers from 1 to 10 in Python.
[Python] I tried to get various information using YouTube Data API!
I tried to create a linebot (implementation)
I tried to summarize Python exception handling
I tried to create a linebot (preparation)
Python3 standard input I tried to summarize
I want to use jar from python
I tried using Bayesian Optimization in Python
I wanted to solve ABC159 in Python
I tried to touch the COTOHA API
I tried to make a Web API
[Python] I tried to calculate TF-IDF steadily
I tried to touch Python (basic syntax)
Convert from Markdown to HTML in Python
API explanation to touch mastodon from python
Connect to coincheck's Websocket API from Python
[Python] I tried to summarize the set type (set) in an easy-to-understand manner.
[Data science basics] I tried saving from csv to mysql with python
I tried to extract characters from subtitles (OpenCV: Google Cloud Vision API)
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
I tried to implement Bayesian linear regression by Gibbs sampling in python
I tried to get the authentication code of Qiita API with Python.
I tried to develop a Formatter that outputs Python logs in JSON
I tried to solve AtCoder's depth-first search (DFS) in Python (result: TLE ...)