|
1 |
| -Arrest-MySQL |
2 |
| -============ |
| 1 | +#Arrest MySQL |
3 | 2 |
|
4 |
| -Arrest MySQL is a "plug-n-play" RESTful API for your MySQL database. |
| 3 | +Arrest MySQL is a "plug-n-play" RESTful API for your MySQL database. Arrest MySQL provides a REST API that maps directly to your database stucture with no configuation. |
5 | 4 |
|
| 5 | +For example lets suppose you have set up Arrest MySQL at http://api.example.com and your database has a table in it called "customers". To get a list of customers you would simply need to do: |
| 6 | + |
| 7 | +```GET http://api.example.com/customers``` |
| 8 | + |
| 9 | +Where "customers" is the table name. As a response you would get a JSON formatted list of customers. Or say you only want to get one customer, then you would do this: |
| 10 | + |
| 11 | +```GET http://api.example.com/customers/123``` |
| 12 | + |
| 13 | +Where "123" here is the ID of the customer. For more information on using Arrest MySQL see the Usage section below. |
| 14 | + |
| 15 | +##Requirements |
| 16 | + |
| 17 | +1. Apache Server with PHP 5+ |
| 18 | +2. MySQL 5+ |
| 19 | + |
| 20 | +##30 Second Installation |
| 21 | + |
| 22 | +Simply put these files into a folder somewhere on your server. Then edit config.php and fill in your database details and you are good to go. |
| 23 | + |
| 24 | +##Usage |
| 25 | + |
| 26 | +If you edit index.php you will see how incredibly simple it is to set up Arrest MySQL. Note that you are left to **provide your own authentication** for your API when using Arrest MySQL. |
| 27 | + |
| 28 | +```php |
| 29 | +<?php |
| 30 | +require('config.php'); |
| 31 | +require('lib/arrest-mysql.php'); |
| 32 | + |
| 33 | +try { |
| 34 | + |
| 35 | + /** |
| 36 | + * Note: You will need to provide a base_uri as the second param if this file |
| 37 | + * resides in a subfolder e.g. if the URL to this file is http://example.com/some/sub/folder/index.php |
| 38 | + * then the base_uri should be "some/sub/folder" |
| 39 | + */ |
| 40 | + $arrest = new ArrestMySQL($db_config); |
| 41 | + |
| 42 | + /** |
| 43 | + * By default it is assumed that the primary key of a table is "id". If this |
| 44 | + * is not the case then you can set a custom index by using the |
| 45 | + * set_table_index($table, $field) method |
| 46 | + */ |
| 47 | + //$arrest->set_table_index('my_table', 'some_index'); |
| 48 | + |
| 49 | + $arrest->rest(); |
| 50 | + |
| 51 | +} catch (Exception $e) { |
| 52 | + echo $e; |
| 53 | +} |
| 54 | +?> |
| 55 | +``` |
| 56 | + |
| 57 | +###API Design |
| 58 | + |
| 59 | +The actual API design is very straight forward and follows the design patterns of most other API's. |
| 60 | + |
| 61 | +``` |
| 62 | +create > POST /table |
| 63 | +read > GET /table[/id] |
| 64 | +update > PUT /table/id |
| 65 | +delete > DELETE /table/id |
| 66 | +``` |
| 67 | + |
| 68 | +To put this into practice below are some example of how you would use an Arrest MySQL API: |
| 69 | + |
| 70 | +``` |
| 71 | +// Get all rows from the "customers" table |
| 72 | +GET http://api.example.com/customers |
| 73 | +// Get a single row from the "customers" table (where "123" is the ID) |
| 74 | +GET http://api.example.com/customers/123 |
| 75 | +// Get 50 rows from the "customers" table |
| 76 | +GET http://api.example.com/customers?limit=50 |
| 77 | +// Get 50 rows from the "customers" table ordered by the "date" field |
| 78 | +GET http://api.example.com/customers?limit=50&order_by=date&order=desc |
| 79 | +
|
| 80 | +// Create a new row in the "customers" table where the POST data corresponds to the database fields |
| 81 | +POST http://api.example.com/customers |
| 82 | +
|
| 83 | +// Update customer "123" in the "customers" table where the PUT data corresponds to the database fields |
| 84 | +PUT http://api.example.com/customers/123 |
| 85 | +
|
| 86 | +// Delete customer "123" from the "customers" table |
| 87 | +DELETE http://api.example.com/customers/123 |
| 88 | +``` |
| 89 | + |
| 90 | +###Responses |
| 91 | + |
| 92 | +All responses are in the JSON format. For example a GET response from the "customers" table might look like: |
| 93 | + |
| 94 | +```json |
| 95 | +[ |
| 96 | + { |
| 97 | + "id": "114", |
| 98 | + "customerName": "Australian Collectors, Co.", |
| 99 | + "contactLastName": "Ferguson", |
| 100 | + "contactFirstName": "Peter", |
| 101 | + "phone": "123456", |
| 102 | + "addressLine1": "636 St Kilda Road", |
| 103 | + "addressLine2": "Level 3", |
| 104 | + "city": "Melbourne", |
| 105 | + "state": "Victoria", |
| 106 | + "postalCode": "3004", |
| 107 | + "country": "Australia", |
| 108 | + "salesRepEmployeeNumber": "1611", |
| 109 | + "creditLimit": "117300" |
| 110 | + }, |
| 111 | + ... |
| 112 | +] |
| 113 | +``` |
| 114 | + |
| 115 | +Successful POST, PUT, and DELETE responses will look like |
| 116 | + |
| 117 | +```json |
| 118 | +{ |
| 119 | + "success": { |
| 120 | + "message": "Success", |
| 121 | + "code": 200 |
| 122 | + } |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +Errors are in the format: |
| 127 | + |
| 128 | +```json |
| 129 | +{ |
| 130 | + "error": { |
| 131 | + "message": "No Content", |
| 132 | + "code": 204 |
| 133 | + } |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +The following codes and message are avaiable: |
| 138 | + |
| 139 | +* 200 Success |
| 140 | +* 204 No Content |
| 141 | +* 404 Not Found |
| 142 | + |
| 143 | +##Credits |
| 144 | + |
| 145 | +Arrest MySQL was created by [Gilbert Pellegrom](http://gilbert.pellegrom.me) from [Dev7studios](http://dev7studios.com). |
| 146 | + |
| 147 | +Please contribute by [reporting bugs](Arrest-MySQL/issues) and submitting [pull requests](Arrest-MySQL/pulls). |
| 148 | + |
| 149 | +##License (MIT) |
| 150 | + |
| 151 | +Copyright © 2013 Dev7studios |
| 152 | + |
| 153 | +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation |
| 154 | +files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, |
| 155 | +modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software |
| 156 | +is furnished to do so, subject to the following conditions: |
| 157 | + |
| 158 | +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 159 | + |
| 160 | +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 161 | +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 162 | +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR |
| 163 | +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
0 commit comments