Skip to main content

Working with Swagger

Introduction

In server-api we have made use of Swagger which is a powerful tool which helps in documenting and testing the APIs of a server. It provides us with various features and tools which make testing very simple.

To get started go to this link : api.thesaveapp.com

  • Sending requests to APIs:
    • For testing an API we simply click on the endpoint which we want to test. Every endpoint can have multiple methods associated with it (post, delete, get,update). For example lets consider post method for about-vidoes api.

image-1681197844382.png

    • On opening this we will see a sample JSON object containing the content expected by the endpoint. This content depends on the schema defined for the method of an endpoint (here createSchema).

image-1681197797239.png

    • We can edit the contents of the parameters using the Try it out button.
    • After entering the desired content we can send the request to the endpoint using the Execute button.
    • After the execution is complete we can see the response sent by the endpoint in the Responses section.
  • Logging In
    • As discussed earlier, each api can have security enabled which makes it accessible to the users only when they  are logged in.
    • To access these apis we first need to authenticate in swagger using the authenticate api

image-1681266508870.png

    • After successful login you will receive an access token, copy the token (without quotes) and click on the authorize button on the top left corner of the page which will open the pop up window shown below. Paste the copied token in the value section and authorize.

 

image-1681266557295.png

 

    • With this the login is successful and we can now access the secured apis as well.

API methods

  • get : 
    • The get method is used to retrieve a single record from the database. 
    • It takes the id of the record and returns the record

image-1681266899332.png

  • find : 
    • Find method has multiple parameters which provide multiple options in filtering and retrieving the required data
    • all (boolean) : setting all to true fetches all the records from the database
    • fetchTotal (boolean) : returns the total count if set to true, else returns -1
    • fromDB (boolean) : fetches data directly from the database when set to true, in case of api data fetched into the in-memory database (discussed later in indexes api definition under API definition section)
    • page (integer) : specifies the page number.
    • size (integer) : specifies the size of each page.
    • sort : data is sorted based on the given attribute in sort.
    • sortType : specifies the type of sort , it can be either asc or desc. The value is asc by default
    • search : In this parameter we can pass a search value in the following format. 
      • "attribute;value;operator"
      • search can also have an array of this type of string to match multiple fields. for example                                    search : ["title;instagram;like","createdBy;41"]
      • attribute : this can be any attribute of the data (eg : key, name, createdBy etc.,)
      • value : the records whose attributes have the given value are selected as the result.
      • operator : This specifies the type of operation that will take place. The value is eq by default. The valid values for operator along with their meaning is given below: 
        1. ​ gt: >
        2. gte:>=
        3. lt: <
        4. ​ lte: <=
        5. ne: !=
        6. eq: ,==
        7. not:!
        8. between: (a,b)
        9. notBetween:!(a,b)
        10. in: in opearator (1,2,3,5,6,)
        11. notIn: not in opeartor (1,2,3,4,5,6)
        12. like: to match a value can also use regex in query
        13. notLike: not matching value
      1.  
  • delete :
    • Takes the id of the record as parameter and deletes the record with the given ID.
  • update :
    • Takes two parameters.
      1. Id : ID of the record to be updated.
      2. data : The data to be updated.