Client API Development Summary

Source: Internet
Author: User

Now that someone's watching, let's share it. # # API Standard notation excerpt: Http://www.startupcto.com/backend-tech/building-an-api-best-practicesYou ' ll generally Want to wrap all your APIs responses in a ' envelope ' which specifies metadata about the apicall. '//Sample JSON Envelope {"status": {"code": 10000, "message": ' Success '}, "response": {...} results ...}} "' Doing This allows for client handler code to behave the same-in-all API calls, since it gets a responses back in a Universal format. Semantically better, recommended: "//Sample JSON envelope{" status ": {" code ": 10000," message ": ' Success '}," data ": {... r Esults ...}} "Visual editing Checksum: http://jsoneditoronline.org/## Note-JSON must be standardized, or iOS JSON library can not read-if support JSONP, you add a callback. -Status Code Description: code:10000 similar to the business or functional modules to classify, so that when the debug fast positioning-try to comply with rest## to explain a problem http://ruby-china.org/topics/15164#reply5 " Xiaogui 7 Floor, 19 hours ago like #5楼 @lytsingsun first say a few personal suggestions: 1, return JSON data, it is best to pack a layer of verification outside, can let the client quickly know, this data is not law, whether it is wrong, 2, code reuse, mainly to see the actual situation of your project; 3. It is best to be able to provide clients with adequate communication, how to make both formal and convenient; 4, performance, is not only load balancing, cache, database read and write separation, code optimization; "1th: The status returned above, code and MeSsage, according to code to judge the problem, generally will be directly alert out code:message or in the log this is Xiaogui said outside package a layer of check the meaning of the other there is no need for special description of the # # About the HTTP status code and the API Status.code description **http status code is directly in the request, this generally refers to the state of the server, the API is assumed that each request is 200, in the client is not 200 of the request unified processing exception, and 200 of the request, First, the status gets the code is 0 (assuming 0 is the request to return correctly), if it is 0 read data, and then complete the corresponding business logic processing. **-responsibility is a single, the server is the server, which is the business logic of the business logic-less HTTP status code, the HTTP status code uniformly encapsulated in a class for handling exceptions (Ajax or iOS ASI or afnetworking)- The same can indicate the status of the state, priority custom Status code # # Why in the response JSON there is no need to add HTTP status code every request can get the status code, and the above I gave the JSON is in the response, Then we can still get all the information of this request when the HTTP requests are completed if it is still added to the request in the response, it is repeated, it is really unnecessary, not dry in a different perspective, In general, when we use the HTTP library to encapsulate processing HTTP status code, try to reduce the code of each request, it is impossible for every request to "Judge code==200 ... And then..... "# # Technology Options-Express or Koa-rails-api-grape-[Sinatra] (http://sinatrarb.com/)-Go Sinatra-like framework [Beego] (http://beego.me /) # # Development Best Practices-API code and MSG can write a gem, directly inherit into the API-API test and mock, preferably can be mock out of static, can be API and mobile side development simultaneously-based on test Generation API Document * * But I have not found a good implementation, but also ask you to guide **## API best Practice or see the Open API Bar-http://developer.github.com/v3/-[Weibo API] (http://open.weibo.com/wiki/ Weibo API)----------------------# # when .... # # # association priority for example, get news interface return data as follows: ' ' {"" Data ": [{" Nid ": 2," Mid ": 1," text ":" This is a test. "," Images ": [" http://121.199.40.172:8086 /images/news/12.png "," Http://121.199.40.172:8086/images/news/13.png ",...]," videos ": ["/http 121.199.40.172:8086/videos/news/12.amr "," Http://121.199.40.172:8086/videos/news/13.amr ",...]," like ": 0," comment ": 0," publish_time ":" 2013-01-01 12:30:10 "},...]," status ": {" code ":" 0 "," MSG ":" Success "}}" "Returns an array of objects, at first glance is appropriate, But we found 1 news that corresponded to several comments, and if the comment doesn't appear in this interface, I'm going to send a new request, so the question comes, shouldn't it? Personally feel that the good API, if the news of the data is not particularly much, should be the latest comments, such as 5, 10 strip # # Update: Use cors to solve cross-domain problems using cors, solve cross-domain problems, but also in practice, but the route configured in App.js need more rigorous " /support cross-domain App.use (require (' cors ')); "The General Mobile test takes a server, and then accesses the API server when it is cross-domain so that it can be tested in the mobile browser. and PHONEGAP packaging is local HTML so do not cross-domain, but for testing convenience, the server is also recommended to add cors. -[Best-practices-for-a-pragmatic-restful-api] (http://www.vinaysahni.com/ BEST-PRACTICES-FOR-A-PRAGMATIC-RESTFUL-API)------------This is my summary of the Ruby-china in the last year, turn around to show you the original address: https:// ruby-china.org/topics/15173 welcome attention to my public number "node full stack"! [Node full stack. png] (//dn-cnoDE.QBOX.ME/FTALXSAUUKYDGDZCUA5Y6BAIDUMC)

Client API Development Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.