You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+25-38
Original file line number
Diff line number
Diff line change
@@ -49,40 +49,23 @@ end
49
49
50
50
The key thing to note here is that the `CoreAPI::Base` reference is provided as a string rather than the constant itself. You can provide the constant here but using a string will ensure that API can be reloaded in development when classes are unloaded.
51
51
52
-
### Creating a controller and endpoint
52
+
### Creating an endpoint
53
53
54
-
A controller is a collection of actions (or endpoints) that will perform actions and return data as appropriate. An API can have as many controllers as you need and a controller can have as many endpoints as needed.
55
-
56
-
Begin by creating a new `controllers` directory in your `app/apis/core_api` directory. Within that, add a file for your first controller. In this example, we'll make a controller for managing products and thus we'll call it the `Products` controller. This controller should inherit from `Rapid::Controller`.
54
+
An endpoint is an action that can be invoked by your consumers. It might return a list, create an resource or anything that takes your fancy. Begin by creating a new `endpoints` director in your `app/apis/core_api` directory. We'll begin by making an endpoint that will simply return a list of products that we sell. Make a file called `product_list_endpoint.rb` in your new `endpoints` directory.
57
55
58
56
```ruby
59
57
moduleCoreAPI
60
-
moduleControllers
61
-
moduleProducts<Rapid::Controller
58
+
moduleEndpints
59
+
classProductListEndpoint < Rapid::Endpoint
62
60
63
-
name 'Products'
64
-
description 'Allows you to list & manages products in the product database'
61
+
name 'List products'
62
+
description 'Returns a list of all product names in our catalogue'
65
63
66
-
end
67
-
end
68
-
end
69
-
```
70
-
71
-
As with the API, this is a very basic implementation of a controller. A controller isn't much use without an endpoint through so we can add an endpoint here.
64
+
field :product_names, [:string]
72
65
73
-
```ruby
74
-
moduleCoreAPI
75
-
moduleControllers
76
-
moduleProducts<Rapid::Controller
77
-
78
-
endpoint :listdo
79
-
name 'List products'
80
-
description 'Returns a full list of products'
81
-
field :products, [:string]
82
-
action do
83
-
product_names =Products.all.map(&:name)
84
-
response.add_field :products, product_names
85
-
end
66
+
defcall
67
+
product_names =Product.order(:name).pluck(:name)
68
+
response.add_field :product_names, product_names
86
69
end
87
70
88
71
end
@@ -92,19 +75,17 @@ end
92
75
93
76
This is a very simple endpoint. Walking through each section...
94
77
95
-
- Firstly, we define the name of the endpoint which, in this case, is `list`. This will be addressed as `products/list` when requests are made to it.
96
-
97
-
- Then we define the name and description for it. This will appear in the schema & documentation.
78
+
- We begin by defining the name and description for it. This will appear in the schema & documentation.
98
79
99
80
- Then we add a field which we will expect to be returned when this action is invoked. In this case, we're creating a field called `products` and specifying that it will be an array of strings that will be returned.
100
81
101
-
- Then we define an action which will actually be executed when this endpoint is executed. This action has access to the request and the response. The `request` object contains information about the request being made and the `response` object allows you to influence what is returned to the consumer.
82
+
- Then we define the `call` method which will actually be executed when this endpoint is called. In here, you have access to the request and the response. The `request` object contains information about the request being made and the `response` object allows you to influence what is returned to the consumer.
102
83
103
-
- Finally, we use `response.add_field` to add data for the `products` field that we defined earlier. In this case, an array of product names.
84
+
- Finally, we use `response.add_field` to add data for the `product_names` field that we defined earlier. In this case, an array of product names.
104
85
105
86
#### A note about types
106
87
107
-
When you define a field (or an argument) you must define a `type`. A type is what type of object that the consumer can expect to receive (or the server will expect to receive in the case of arguments). A type can be provided as a symbol to reference a scalar or a class that inherits from `Rapid::Scalar` (for scalars), `Rapid::Object` (for objects), `Rapid::Enum` (for enums) or `Rapid::Polymorph` (for polymorphs).
88
+
When you define a field (or an argument) you must define a `type`. A type is what type of object that the consumer can expect to receive (or is expected to send in the case of arguments). A type can be provided as a symbol to reference a known scaler, or a class that inherits from `Rapid::Scalar` (for scalars), `Rapid::Object` (for objects), `Rapid::Enum` (for enums) or `Rapid::Polymorph` (for polymorphs).
108
89
109
90
The following scalars are built-in:
110
91
@@ -125,7 +106,7 @@ module CoreAPI
125
106
classBase < Rapid::API
126
107
127
108
routes do
128
-
get 'products', controller:Controllers::Products, endpoint::list
109
+
get 'products', endpoint:Endpoints::ListProductsEndpoint
129
110
end
130
111
131
112
end
@@ -176,16 +157,22 @@ By default, Rapid will try to find a value for your fields by calling a method n
176
157
Once you have created your object class, you will need to update your endpoint to reference the object.
177
158
178
159
```ruby
179
-
endpoint :listdo
160
+
classProductListEndpoint < Rapid::Endpoint
161
+
162
+
# ...
163
+
180
164
field :products, [Objects::Product]
181
-
action do |request, response|
182
-
response.add_field :products, Products.all.to_a
165
+
166
+
defcall
167
+
products =Product.order(:name)
168
+
response.add_field :products, products.to_a
183
169
end
170
+
184
171
end
185
172
```
186
173
187
174
If you make the request now, you should receive an array of objects (hashes) rather than strings now.
@@ -43,7 +43,7 @@ Let's take a look through this line by line:
43
43
- Firstly, we define the name & description for the authenticator. This is used for documentation.
44
44
- Next, we choose the type of authenticator. Rapid only currently supports `:bearer`. This is only used for documentation purposes as well.
45
45
- Then, we define a potential error that this authenticator might raise. In this case, the API token provided may be invalid and we'll raise that error.
46
-
- Finally, we define an action which will be invoked. This is responsible for setting the `request.identity` property or raising an error if authentication has failed. You don't **have** to set a `request.identity` if you don't wish (anonymous access?) but unless you raise an error in here the endpoint execution will continue.
46
+
- Finally, we define the `call` method which will be invoked when the authenticator is used. This is responsible for setting the `request.identity` property or raising an error if authentication has failed. You don't **have** to set a `request.identity` if you don't wish (anonymous access?) but unless you raise an error in here the endpoint execution will continue.
Copy file name to clipboardexpand all lines: doc/consuming.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ This document outlines the conventions used for consuming APIs built with Rapid.
4
4
5
5
## Requests
6
6
7
-
The address of each API endpoint is made up from two parts - a controller name and an endpoint name. For example, a list of users would have a controller name of `users` and an endpoint name of `list`. This is sent to the API in the URL of the request. For example:
7
+
Consumers can make requests to any resource provided by the API (and listed as a route).
Copy file name to clipboardexpand all lines: doc/errors.md
+19-6
Original file line number
Diff line number
Diff line change
@@ -34,27 +34,34 @@ Errors can be raised by calling `raise_error` and providing either the name of t
34
34
This example shows how to raise a class-defined error. You should also provide any fields that are required for the error as shown below with `errors`.
@@ -64,6 +71,7 @@ If any exception occurs during your application lifecycle, it will be returned t
64
71
65
72
```ruby
66
73
classValidationError < Rapid::Error
74
+
67
75
# We can define things as normal for the error...
68
76
code :validation_error
69
77
http_status 416
@@ -83,15 +91,20 @@ end
83
91
This will only be caught if the `ValidationError` error has been specified as a potential error for the endpoint where the exception is raised. So, you need to make sure to specify these if you want exceptions to be caught automatically. An endpoint might look this like:
0 commit comments