swagger-ui icon indicating copy to clipboard operation
swagger-ui copied to clipboard

How to use swagger for my own REST API's

Open jhambj opened this issue 11 years ago • 26 comments

please help me understand how do I use the swagger to document my own REST API's. I am completely clueless though I just read many things about swagger. I got swagger downloaded from github too. Thanks in advance and waiting for a quick response.

jhambj avatar Dec 06 '14 12:12 jhambj

Do you have a swagger documentation of your API ready?

webron avatar Dec 06 '14 12:12 webron

Hi, No, can you send me any document which I can refer to make the same

jhambj avatar Dec 06 '14 12:12 jhambj

The pet store sample which swagger-ui automatically loads is such a document (http://petstore.swagger.wordnik.com/api/api-docs/). Do you have an existing API? If so, which language/framework do you use?

webron avatar Dec 06 '14 12:12 webron

I have developed a REST web service using Data virtualization tool which I want to document using Swagger. Its a GET service with inputs as query string and a specified URL to hit the same. Response is XML and I am awared of that structure so how should I initiate making the swagger documentation of my API. Thanks

jhambj avatar Dec 06 '14 12:12 jhambj

Please bare with me as I am completely new to this. Thanks

jhambj avatar Dec 06 '14 12:12 jhambj

I don't follow. Do you have a working API? Which development language do you use?

webron avatar Dec 06 '14 13:12 webron

Yes , I do have a working API and I am using a Data virtualization tool i.e built on java to make API.

jhambj avatar Dec 06 '14 13:12 jhambj

I'm not familiar with the tool you're talking about. Can you give me a link to that tool so I can see what it's about? If it's built using Java, do you know if it's based on JAX-RS or some other REST framework?

webron avatar Dec 06 '14 13:12 webron

Tool name is Denodo. and I have API's URL's ready, so how can I use the same??

jhambj avatar Dec 06 '14 18:12 jhambj

I'm not really familiar with the tool, and I'm trying to see if you can use a library or if you'll need to manually document your API.

Do you have a sample REST endpoint code you can share?

webron avatar Dec 06 '14 18:12 webron

Hi , actually code is being written in VQL(virtual query language) so as of now I cant find any way of using any library to generate API doc. But I have manually documented for one of the API, looks like this:

{
  "swagger": "2.0",
  "info": {
    "description": "This is  XYZ",
    "version": "1.0.0",
    "title": "Swagger DDSW",
    "termsOfService": "http:\/\/helloreverb.com\/terms\/",
    "contact": {
      "name": "[email protected]"
    },
    "license": {
      "name": "Apache 2.0",
      "url": "http:\/\/www.apache.org\/licenses\/LICENSE-2.0.html"
    }
  },
  "host": "arldatavd01.ecorp.cat.com:9090\/server",
  "basePath": "\/ddsw",
  "schemes": [
    "http"
  ],
  "paths": {
    "\/ddsw_authenticate": {
      "get": {
        "tags": [
          "ddsw_authenticate"
        ],
        "summary": "Authenticate a Dealer",
        "description": "Authorized cws id along witht a valid dealercode can be passed to authenticate it against a dealercode",
        "operationId": "ddswAuthenticateDealer",
        "produces": [
          "application\/json",
          "application\/xml"
        ],
        "parameters": [
          {
            "in": "query",
            "name": "cws_id",
            "description": "Authorized cws id",
            "required": true,
            "type": "string"
          },
          {
            "in": "query",
            "name": "Dealer_code",
            "description": "Valid Dealer code",
            "required": true,
            "type": "string"
          }
        ],
        "responses": {
          "404": {
            "description": "Unauthorized dealer"
          },
          "200": {
            "description": "successful operation",
            "schema": {
              "$ref": "#\/definitions\/ddsw_authenticate"
            }
          }
        }
      }
    }
  },
  "definitions": {
    "ddsw_authenticate": {
      "required": [
        "cwsid",
        "dealer_code"
      ],
      "properties": {
        "cwsid": {
          "type": "String",
          "xml": {
            "name": "cwsid"
          }
        },
        "dealer_code": {
          "type": "string",
          "xml": {
            "name": "dealer_code"
          }
        }
      },
      "xml": {
        "name": "User"
      }
    }
  }
}

so please help me now how to proceed further, just wanted to get it tested for this particular API

jhambj avatar Dec 09 '14 04:12 jhambj

In the index.html, create a javascript variable with the value of the spec. Something like,

var spec = { .... };

Then, add the spec to the SwaggerUi declaration:

      window.swaggerUi = new SwaggerUi({
        url: url,
        spec: spec,
        dom_id: "swagger-ui-container",
        supportedSubmitMethods: ['get', 'post', 'put', 'delete'],
        onComplete: function(swaggerApi, swaggerUi){
          log("Loaded SwaggerUI");
          if(typeof initOAuth == "function") {
            /*
            initOAuth({
              clientId: "your-client-id",
              realm: "your-realms",
              appName: "your-app-name"
            });
            */
          }
          $('pre code').each(function(i, e) {
            hljs.highlightBlock(e)
          });
        },
        onFailure: function(data) {
          log("Unable to Load SwaggerUI");
        },
        docExpansion: "none",
        sorter : "alpha"
      });

Open the index.html in your browser and you should see your spec.

webron avatar Dec 09 '14 06:12 webron

what should be the value of spec, considering swagger documentation that I wrote and mentioned in above comment

jhambj avatar Dec 09 '14 06:12 jhambj

because for petstore sample app available on swagger UI demo is not carrying any spec value in index.html

jhambj avatar Dec 09 '14 06:12 jhambj

The value should be exactly the spec you pasted.

The pet store sample uses a hosted/generated spec. The solution I gave you is just easier to test locally without hosting it.

webron avatar Dec 09 '14 06:12 webron

okay, Got it. Thanks. will try and acknowledge

jhambj avatar Dec 09 '14 06:12 jhambj

Hi, Its working.Thanks for that, but I am not able to get response though if I run that resposne generated URL from my browser, it works. Attaching the screenshot. Will it be a firewall issue? 2014-12-09 12-38-35_swagger ui

jhambj avatar Dec 09 '14 07:12 jhambj

Your application needs to have CORS support enabled for the UI to work.

webron avatar Dec 09 '14 09:12 webron

how do I do that?

jhambj avatar Dec 09 '14 10:12 jhambj

That depends on the tool you use. A 'regular' Java Web application would use a filter like the one in all the samples we have, but I don't know how your application is running. Another option is to use a proxy server that enables CORS support, I'm sure Google will offer various options there.

webron avatar Dec 09 '14 10:12 webron