|
| 1 | +# Rest API |
| 2 | + |
| 3 | +## POST /query |
| 4 | + |
| 5 | +Receives an SQL query and forwards it to the `gitbase` server. |
| 6 | + |
| 7 | +The request body can have: |
| 8 | + |
| 9 | +* `query`: An SQL statement string. Do not include `LIMIT` here. |
| 10 | +* `limit`: Number, will be added as SQL `LIMIT` to the query. Optional. |
| 11 | + |
| 12 | +The success response will contain: |
| 13 | + |
| 14 | +* `status`: HTTP status code. |
| 15 | +* `data`: Rows, array of JSON objects. |
| 16 | +* `meta`: JSON object, with these fields: |
| 17 | + * `headers`: Array of strings with the names of the requested columns. |
| 18 | + * `types`: Array of strings with the types of each column. Note: these are the types reported by MySQL, so for example a type `BIT` will be a boolean in the `data` JSON. |
| 19 | + |
| 20 | +A failure response will contain: |
| 21 | + |
| 22 | +* `status`: HTTP status code. |
| 23 | +* `errors`: Array of JSON objects, with the fields below: |
| 24 | + * `status`: HTTP status code. |
| 25 | + * `title`: Error description. |
| 26 | + * `mysqlCode`: Error code reported by MySQL. May not be present for some errors. |
| 27 | + |
| 28 | + |
| 29 | +Some examples follow. A basic query: |
| 30 | + |
| 31 | +```bash |
| 32 | +curl -X POST \ |
| 33 | + http://localhost:8080/query \ |
| 34 | + -H 'content-type: application/json' \ |
| 35 | + -d '{ |
| 36 | + "query": "SELECT name, hash, is_remote(name) AS is_remote FROM refs", |
| 37 | + "limit": 20 |
| 38 | +}' |
| 39 | +``` |
| 40 | + |
| 41 | +```json |
| 42 | +{ |
| 43 | + "status": 200, |
| 44 | + "data": [ |
| 45 | + { |
| 46 | + "hash": "66fd81178abfa342f873df5ab66639cca43f5104", |
| 47 | + "is_remote": false, |
| 48 | + "name": "HEAD" |
| 49 | + }, |
| 50 | + { |
| 51 | + "hash": "66fd81178abfa342f873df5ab66639cca43f5104", |
| 52 | + "is_remote": false, |
| 53 | + "name": "refs/heads/master" |
| 54 | + }, |
| 55 | + { |
| 56 | + "hash": "66fd81178abfa342f873df5ab66639cca43f5104", |
| 57 | + "is_remote": true, |
| 58 | + "name": "refs/remotes/origin/master" |
| 59 | + } |
| 60 | + ], |
| 61 | + "meta": { |
| 62 | + "headers": [ |
| 63 | + "name", |
| 64 | + "hash", |
| 65 | + "is_remote" |
| 66 | + ], |
| 67 | + "types": [ |
| 68 | + "TEXT", |
| 69 | + "TEXT", |
| 70 | + "BIT" |
| 71 | + ] |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +A failure: |
| 77 | + |
| 78 | +```bash |
| 79 | +curl -X POST \ |
| 80 | + http://localhost:8080/query \ |
| 81 | + -H 'content-type: application/json' \ |
| 82 | + -d '{ |
| 83 | + "query": "SELECT * FROM somewhere", |
| 84 | + "limit": 20 |
| 85 | +}' |
| 86 | +``` |
| 87 | + |
| 88 | +```json |
| 89 | +{ |
| 90 | + "status": 400, |
| 91 | + "errors": [ |
| 92 | + { |
| 93 | + "status": 400, |
| 94 | + "title": "unknown error: table not found: somewhere", |
| 95 | + "mysqlCode": 1105 |
| 96 | + } |
| 97 | + ] |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +For a query with uast nodes the protobuf response is unmarshalled and the json is returned: |
| 102 | + |
| 103 | +```bash |
| 104 | +curl -X POST \ |
| 105 | + http://localhost:8080/query \ |
| 106 | + -H 'content-type: application/json' \ |
| 107 | + -d '{ |
| 108 | + "query": "SELECT hash, content, uast(blobs.content, 'go') FROM blobs WHERE hash='fd30cea52792da5ece9156eea4022bdd87565633'", |
| 109 | + "limit": 20 |
| 110 | +}' |
| 111 | +``` |
| 112 | + |
| 113 | +```json |
| 114 | +{ |
| 115 | + "status": 200, |
| 116 | + "data": [ |
| 117 | + { |
| 118 | + "content": "package server\n\nimport (\n\t\"net/http\"\n\n\t\"github.com/src-d/gitbase-playground/server/handler\"\n\n\t\"github.com/go-chi/chi\"\n\t\"github.com/go-chi/chi/middleware\"\n\t\"github.com/pressly/lg\"\n\t\"github.com/rs/cors\"\n\t\"github.com/sirupsen/logrus\"\n)\n\n// Router returns a Handler to serve the backend\nfunc Router(\n\tlogger *logrus.Logger,\n\tstatic *handler.Static,\n\tversion string,\n) http.Handler {\n\n\t// cors options\n\tcorsOptions := cors.Options{\n\t\tAllowedOrigins: []string{\"*\"},\n\t\tAllowedMethods: []string{\"GET\", \"POST\", \"PUT\", \"OPTIONS\"},\n\t\tAllowedHeaders: []string{\"Location\", \"Authorization\", \"Content-Type\"},\n\t\tAllowCredentials: true,\n\t}\n\n\tr := chi.NewRouter()\n\n\tr.Use(middleware.Recoverer)\n\tr.Use(cors.New(corsOptions).Handler)\n\tr.Use(lg.RequestLogger(logger))\n\n\tr.Get(\"/version\", handler.APIHandlerFunc(handler.Version(version)))\n\n\tr.Get(\"/static/*\", static.ServeHTTP)\n\tr.Get(\"/*\", static.ServeHTTP)\n\n\treturn r\n}\n", |
| 119 | + "hash": "fd30cea52792da5ece9156eea4022bdd87565633", |
| 120 | + "uast(blobs.content, \"go\")": [ |
| 121 | + { |
| 122 | + "InternalType": "File", |
| 123 | + "Properties": { |
| 124 | + "Package": "1" |
| 125 | + }, |
| 126 | + "Children": [ |
| 127 | + { |
| 128 | + "InternalType": "Ident", |
| 129 | + "Properties": { |
| 130 | + "internalRole": "Name" |
| 131 | + }, |
| 132 | + "Token": "server", |
| 133 | + "StartPosition": { |
| 134 | + "Offset": 9, |
| 135 | + "Line": 1, |
| 136 | + "Col": 10 |
| 137 | + }, |
| 138 | + "Roles": [ |
| 139 | + 18, |
| 140 | + 1 |
| 141 | + ] |
| 142 | + }, |
| 143 | + { |
| 144 | + "InternalType": "GenDecl", |
| 145 | + "Properties": { |
| 146 | + "Lparen": "24", |
| 147 | + "Tok": "import", |
| 148 | + "internalRole": "Decls" |
| 149 | + }, |
| 150 | + "Children": [ |
| 151 | + { |
| 152 | + "InternalType": "ImportSpec", |
| 153 | + "Properties": { |
| 154 | + "EndPos": "0", |
| 155 | + "internalRole": "Specs" |
| 156 | + }, |
| 157 | + |
| 158 | + [...] |
| 159 | + |
| 160 | + } |
| 161 | + ], |
| 162 | + "meta": { |
| 163 | + "headers": [ |
| 164 | + "hash", |
| 165 | + "content", |
| 166 | + "uast(blobs.content, \"go\")" |
| 167 | + ], |
| 168 | + "types": [ |
| 169 | + "TEXT", |
| 170 | + "TEXT", |
| 171 | + "JSON" |
| 172 | + ] |
| 173 | + } |
| 174 | +} |
| 175 | +``` |
| 176 | + |
0 commit comments