2

I am very new to Python. I am trying to read a swagger file. I want to know

  1. How many paths do I have in this swagger?
  2. For each path, How many methods do I have (get, put, post)?
  3. For each method, what is the request and response?

For a string I pass in, if the string is in the response, then i would like to know, the basepath/path/method

This is what i have so far:

from swagger_parser import SwaggerParser

def operations_in_swagger (d1):
    d =d1['paths']
    print "Operations in swagger with thier operations"
    for i in range(0,len(d)):
        r=d.keys()[i]
        s=d[r].keys()
        for j in range(0, len(s)):
            s1=d[r].keys()[j]
            print s1 +  d1['basePath'] + d.keys()[i]

parser=SwaggerParser(swagger_path='/Users/path/x.json')  # Init with file
df = parser.specification
operations_in_swagger(df)

2 Answers 2

1

At least solving 2 of your questions:

  1. How many resource-URIs (paths) ?
  2. Which HTTP-methods per path (operations) ?

From the docs on Paths and Operations:

In Swagger terms, paths are endpoints (resources) that your API exposes, such as /users or /reports/summary, and operations are the HTTP methods used to manipulate these paths, such as GET, POST or DELETE.

from swagger_parser import SwaggerParser

# condensed format (all operations in line with the path) 
def operations_per_path(specification):
    base = specification['basePath']
    paths = specification['paths']
    ops = []
    for p in paths:
        methods = paths[p].keys() #  methods are keys inside a single path-dict
        ops.append(f"{'|'.join(methods).upper()} {base}{p}")
    return ops


def count_operations(specification):
    paths = specification['paths']
    return sum([len(paths[p].keys()) for p in paths])


parser = SwaggerParser(swagger_path='Downloads/swagger-sample.json')  # Init with file
spec = parser.specification

print("Operations in swagger (resource-URIs with their HTTP-methods):")
ops_count = count_operations(spec)
paths = operations_per_path(spec)
print(f"\t{ops_count} operations, {len(paths)} paths")
print("\n".join(paths))

Applied to the Petstore example-swagger it prints:

Operations in swagger (resource-URIs with their HTTP-methods):
    20 operations, 14 paths
POST|PUT /v2/pet
GET /v2/pet/findByStatus
GET /v2/pet/findByTags
GET|POST|DELETE /v2/pet/{petId}
POST /v2/pet/{petId}/uploadImage
GET /v2/store/inventory
POST /v2/store/order
GET|DELETE /v2/store/order/{orderId}
POST /v2/user
POST /v2/user/createWithArray
POST /v2/user/createWithList
GET /v2/user/login
GET /v2/user/logout
GET|PUT|DELETE /v2/user/{username}

Neither the third question for each method's request and response, nor the puzzle to find the related request (method, URI) to a given response part is solved yet.

Sign up to request clarification or add additional context in comments.

2 Comments

This is a great way to start. Do you have any idea how to get the properties for each definitions?
@Aziz What do you mean by "properties", give an example or better: post a new question and leave the link here.
0

Not sure if you still need this, but maybe someone does.

The reason your code does not work (aside from the lack of parentheses on the print statements) is that you are trying to index a dict_keys type object, which I guess should be clear by the error you get.

Anyways, one way of doing what you need is to just transform your dict_keys into a list and try to index that:

def operations_in_swagger(d1):
    d = d1['paths']
    for i in range(0,len(d)):
        r=list(d.keys())[i]
        s=list(d[r].keys())
        for j in range(0, len(s)):
            s1=s[j]
            print(s1 + d1['basePath'] + r)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.