api-schema.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import VERSION from '../version'
  7. import { QueryParamInfo, QueryParamType, QueryDefinition, CommonQueryParamsInfo, QueryList } from './api';
  8. import { ModelServerConfig as ServerConfig } from '../config';
  9. import { MultipleQuerySpec } from './api-web-multiple';
  10. export const shortcutIconLink = `<link rel='shortcut icon' href='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAnUExURQAAAMIrHrspHr0oH7soILonHrwqH7onILsoHrsoH7soH7woILwpIKgVokoAAAAMdFJOUwAQHzNxWmBHS5XO6jdtAmoAAACZSURBVDjLxZNRCsQgDAVNXmwb9f7nXZEaLRgXloXOhwQdjMYYwpOLw55fBT46KhbOKhmRR2zLcFJQj8UR+HxFgArIF5BKJbEncC6NDEdI5SatBRSDJwGAoiFDONrEJXWYhGMIcRJGCrb1TOtDahfUuQXd10jkFYq0ViIrbUpNcVT6redeC1+b9tH2WLR93Sx2VCzkv/7NjfABxjQHksGB7lAAAAAASUVORK5CYII=' />`
  11. export function getApiSchema() {
  12. return {
  13. openapi: '3.0.0',
  14. info: {
  15. version: VERSION,
  16. title: 'ModelServer',
  17. description: 'The ModelServer is a service for accessing subsets of macromolecular model data.',
  18. },
  19. tags: [
  20. {
  21. name: 'General',
  22. }
  23. ],
  24. paths: getPaths(),
  25. components: {
  26. parameters: {
  27. id: {
  28. name: 'id',
  29. in: 'path',
  30. description: 'Id of the entry (i.e. 1tqn).',
  31. required: true,
  32. schema: {
  33. type: 'string',
  34. },
  35. style: 'simple'
  36. },
  37. }
  38. }
  39. }
  40. }
  41. function getPaths() {
  42. const ret: any = {};
  43. for (const { name, definition } of QueryList) {
  44. ret[`${ServerConfig.apiPrefix}/v1/{id}/${name}`] = getQueryInfo(definition);
  45. }
  46. const queryManySummary = 'Executes multiple queries at the same time and writes them as separate data blocks.';
  47. const queryManyExample: MultipleQuerySpec = {
  48. queries: [
  49. { entryId: '1cbs', query: 'residueInteraction', params: { atom_site: [{ label_comp_id: 'REA' }], radius: 5 } },
  50. { entryId: '1tqn', query: 'full' }
  51. ],
  52. encoding: 'cif'
  53. };
  54. ret[`${ServerConfig.apiPrefix}/v1/query-many`] = {
  55. get: {
  56. tags: ['General'],
  57. summary: queryManySummary,
  58. operationId: 'query-many',
  59. parameters: [{
  60. name: 'query',
  61. in: 'query',
  62. description: 'URI encoded JSON object with the query definiton.',
  63. required: true,
  64. schema: {
  65. type: 'string',
  66. },
  67. example: JSON.stringify(queryManyExample),
  68. style: 'simple'
  69. }],
  70. responses: {
  71. 200: {
  72. description: 'Separate CIF data blocks with the result',
  73. content: {
  74. 'text/plain': {},
  75. 'application/octet-stream': {},
  76. }
  77. }
  78. }
  79. },
  80. post: {
  81. tags: ['General'],
  82. summary: queryManySummary,
  83. operationId: 'query-many-post',
  84. parameters: [],
  85. requestBody: {
  86. content: {
  87. 'application/json': {
  88. schema: { type: 'object' },
  89. example: queryManyExample
  90. }
  91. }
  92. },
  93. responses: {
  94. 200: {
  95. description: 'Separate CIF data blocks with the result',
  96. content: {
  97. 'text/plain': {},
  98. 'application/octet-stream': {},
  99. }
  100. }
  101. }
  102. }
  103. };
  104. return ret;
  105. }
  106. function getQueryInfo(def: QueryDefinition) {
  107. const jsonExample: any = {};
  108. def.jsonParams.forEach(p => {
  109. if (!p.exampleValues || !p.exampleValues.length) return;
  110. jsonExample[p.name] = p.exampleValues[0];
  111. });
  112. return {
  113. get: {
  114. tags: ['General'],
  115. summary: def.description,
  116. operationId: def.name,
  117. parameters: [
  118. { $ref: '#/components/parameters/id' },
  119. ...def.restParams.map(getParamInfo),
  120. ...CommonQueryParamsInfo.map(getParamInfo)
  121. ],
  122. responses: {
  123. 200: {
  124. description: def.description,
  125. content: {
  126. 'text/plain': {},
  127. 'application/octet-stream': {},
  128. }
  129. }
  130. }
  131. },
  132. post: {
  133. tags: ['General'],
  134. summary: def.description,
  135. operationId: def.name + '-post',
  136. parameters: [
  137. { $ref: '#/components/parameters/id' },
  138. ...CommonQueryParamsInfo.map(getParamInfo)
  139. ],
  140. requestBody: {
  141. content: {
  142. 'application/json': {
  143. schema: { type: 'object' },
  144. example: jsonExample
  145. }
  146. }
  147. },
  148. responses: {
  149. 200: {
  150. description: def.description,
  151. content: {
  152. 'text/plain': {},
  153. 'application/octet-stream': {},
  154. }
  155. }
  156. }
  157. }
  158. };
  159. }
  160. function getParamInfo(info: QueryParamInfo) {
  161. return {
  162. name: info.name,
  163. in: 'query',
  164. description: info.description,
  165. required: !!info.required,
  166. schema: {
  167. type: info.type === QueryParamType.String ? 'string' : info.type === QueryParamType.Integer ? 'integer' : 'number',
  168. enum: info.supportedValues ? info.supportedValues : void 0,
  169. default: info.defaultValue
  170. },
  171. style: 'simple'
  172. };
  173. }