RESTful API

RESTful API 是一種API規範,使用的是raml(RESTful API Modeling Language)文件,講到這個,也得扯到wsdl文件,它是用來描述soap(簡單對象傳輸協議)API的,但是這個協議比較重,所以現在推行的是RESTful。

然後的話,這個怎麼創建怎麼使用呢?

我用的是Anypoint Platform平臺上的Design Center來創建API,並且進行模擬測試的。

 

先上文件再說,不然我都不知道說些啥,我感覺我想說多點東西的,但寫的時候就沒幾個字了。

#%RAML 1.0
version: v1
title: American Flights API

types:
  AmericanFlight: !include /exchange_modules/68ef9520-24e9-4cf2-b2f5-620025690913/training-american-flight-data-type/1.0.1/AmericanFlightDataType.raml

traits:
  client-id-required:
    headers:
      client_id:
        type: string
      client_secret:
        type: string
    responses:
      401:
        description: Unauthorized, The client_id or client_secret are not valid or the client does not have access.
      429:
        description: The client used all of it's request quota for the current period.
      500:
        description: An error ocurred, see the specific message (Only if it is a WSDL enpoint).
      503:
        description: Contracts Information Unreachable.

/flights:
  is: [client-id-required]
  get:
    queryParameters:
      destination:
        required: false
        enum: 
          - SFO
          - LAX
          - CLE
    responses:
      200:
        body:
          application/json:
            type: AmericanFlight[]
            examples:
              output: !include /exchange_modules/68ef9520-24e9-4cf2-b2f5-620025690913/training-american-flights-example/1.0.1/AmericanFlightsExample.raml
  post:
    body:
      application/json:
        type: AmericanFlight
        examples:
          input: !include examples/AmericanFlightExample.raml
    responses:
      201:
        body:
          application/json:
            example: 
              message: Flight added(but not really)

  /{ID}:
    is: [client-id-required]
    get:
      responses:
        200:
          body:
            application/json:
              type: AmericanFlight
              examples:
                output: !include examples/AmericanFlightExample.raml
    delete:
      responses:
        200:
          body:
            application/json:
              example:
                message: Flight deleted (but not really)
    put:
      body:
        application/json:
          type: AmericanFlight
          examples:
            input: !include examples/AmericanFlightNoIDExample.raml
      responses:
        200:
          body:
            application/json:
              example:
                message: Flight updated (but not really)

raml的格式就是十分簡單清晰,以#%RAML 1.0爲開頭,然後下面是title,然後的話,就可以寫相應的路徑內容。每一層是一個層級,層次感很明確,回車之後tab就可以到下面去。然

整個結構就是,

先定義raml的文本信息,

然後的話,就猶如聲明變量一樣,定義一些類型types,方便之後使用,定義一些請求過來的方法體要用到的類型,可以進行相應的類型匹配,這個可以直接寫,也可以通過 !incude 引用raml文件或者引用json文件。

然後的話,可以添加一些強制參數,下面這個是我的raml的強制參數圖,這個是強制在headers那裏必須要有client_id和client_secret的這兩個參數,不然的話,是無法call通的。

然後接下來就是寫相應的資源,比如 /flights: 這個就是定義一個資源,接下來,如上面寫的

 

/flights:
  is: [client-id-required]
  get:

加了這個,相當於把那個必須要client_id和client_secret參數放到這個資源上面,和is同級的get就是相應的method,所以能替換這個的還可以是post、put、delete等這些method,

然後的話,現在變的地方就多了,你可以寫queryParameter,然後往下面添加參數,參數可以設置成必須和非必須的,通過required的true或false來進行設置,參數可以設置成可選列表;如果此時你是post方法的話,那麼就要寫body,然後在下面寫body的類型,通常是application/json,然後type就要被用上了

post:
    body:
      application/json:
        type: AmericanFlight
        examples:
          input: !include examples/AmericanFlightExample.raml

type上寫入類型名,然後examples是一個示例請求,通過input: !include來包含,當然也可以是json,一樣的。example也是可以的,主要是看它的請求。

然後的話,再下一層就要回退到和body同層,或者是結構上的method的下一層,寫response,然後響應的話,就會有個響應碼,對應的響應碼可以有對應的body返回,body下面一樣要寫格式,如application/json,如果要返回的示例是在raml或者json中,那麼一樣要寫type,然後引入相應的示例。

大概整個raml文件就是這樣,然後的話,在Anypoint Platform中,可以進行mocking service,然後可以使用postman工具等來call這個API,還是比較方便的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章