Changes for page 3. Власні API методи
Last modified by Ashterix on 2024/05/16 12:35
Summary
-
Page properties (1 modified, 0 added, 0 removed)
Details
- Page properties
-
- Content
-
... ... @@ -1,46 +1,47 @@ 1 -{{box cssClass="floatinginfobox" width="400px" title="**Contents**"}}1 +{{box cssClass="floatinginfobox" title="**Content**"}} 2 2 {{toc/}} 3 3 {{/box}} 4 4 5 5 You can easily add your own API methods to the RPC server. 6 6 7 -To do this, simply create any classin your application that istobecome an API Service;this class must implement the interface {{code language="none"}}Ufo\JsonRpcBundle\ApiMethod\Interfaces\IRpcService{{/code}}. This interface does not impose any logic on the class; it is only necessary for Symfony to understand that it shouldbetreatedas a service available to the RPC server.7 +To do this, you just need to create any class that will become an API Service anywhere in your application. This class must implement the interface {{code language="none"}}Ufo\JsonRpcBundle\ApiMethod\Interfaces\IRpcService{{/code}}. This interface does not impose any logic on the class; it is only necessary for Symfony to understand that it should treat it as a service available to the RPC server. 8 8 9 -Implement any public method in your class and it will automatically be available as an API Service. 9 +Implement any public method in your class, and it will automatically be available as an API Service. 10 10 11 -(% id="cke_bm_318406S" style="display:none" %) (%%) After following the previous instructions, you will havenew methods available in the API. By default, method names consistof {{code language="none"}}<className>.<methodName>{{/code}}.11 +(% id="cke_bm_318406S" style="display:none" %) (%%) After following the previous instructions, new methods will already be available in the API. By default, method names are composed of {{code language="none"}}<className>.<methodName>{{/code}}. 12 12 13 13 = Naming Classes = 14 14 15 -If nee ded, you can createmultiple classes that will be available as API Services.16 -Considering their naming **<className>.<methodName>**, I recommendapproaching thechoice ofmethod name as a simple indication of what it does,and the class name as the namespace of the API method.15 +If necessary, you can create several classes that will be available as API Services. 16 +Considering their naming {{code language="none"}}<className>.<methodName>{{/code}}, I recommend treating the method name as a simple indication of what it does and the class name as the namespace of the API method. 17 17 18 -{{success}} 19 -== **Best Practice** == 18 +(% class="box successmessage" %) 19 +((( 20 +== **Best Practices** == 20 20 21 -The **//Messenger//** c lass contains methods **//sendEmail()//**, **sendSms()**; the **CartResolver** class contains methods **//addProduct()//**, **getProducts()**, **calculateDiscount()**.22 +The class **//Messenger//** contains methods **//sendEmail()//**, **sendSms()**; the class **CartResolver** contains methods **//addProduct()//**, **getProducts()**, **calculateDiscount()**. 22 22 23 23 In the API, the following methods will be available: 24 24 25 -* ** //CartResolver.addProduct//**26 -* ** //CartResolver.getProducts//**27 -* ** //CartResolver.calculateDiscount//**28 -* ** //Messenger.sendEmail//**29 -* ** //Messenger.sendSms//**26 +* **CartResolver.addProduct** 27 +* **CartResolver.getProducts** 28 +* **CartResolver.calculateDiscount** 29 +* **Messenger.sendEmail** 30 +* **Messenger.sendSms** 30 30 31 - Just by looking at the method names, it's clear what your server can do.32 +Even just by looking at the method names, it is clear what your server can do. 32 32 33 -This approach allows fortheuseof identicalmethod names in different classes:34 +This approach allows you to use the same method names in different classes: 34 34 35 -* ** //ProductService.create//**36 -* ** //ProductService.getName//**37 -* ** //UserService.create//**38 -* ** //UserService.getName//**39 - {{/success}}36 +* **ProductService.create** 37 +* **ProductService.getName** 38 +* **UserService.create** 39 +* **UserService.getName** 40 +))) 40 40 41 -= GettingStarted=42 += Simple Start = 42 42 43 -The following example demonstrate showeasyit is to add your methods to the API.44 +The following example should demonstrate the ease of adding your methods to the API. 44 44 45 45 (% class="row" %) 46 46 ((( ... ... @@ -57,30 +57,28 @@ 57 57 public function __construct( 58 58 // connecting some dependencies to retrieve data 59 59 ) {} 60 - 61 + 61 61 public function getUserNameByUuid( 62 62 string $userId 63 - ): string 64 - { 65 - // some logic get user info by id 64 + ): string { 65 + // some logic to get user info by id 66 66 return 'some result'; 67 67 } 68 - 68 + 69 69 public function sendEmail( 70 - string $email, 71 - string $text, 70 + string $email, 71 + string $text, 72 72 string $subject = 'Message without subject' 73 - ): bool 74 - { 75 - // some logic send email 73 + ): bool { 74 + // some logic to send email 76 76 return true; 77 77 } 78 78 } 79 79 {{/code}} 80 80 81 -To the right is an example of the documentation that the server will generate for this class.80 +To the right, you see an example of the documentation that the server will generate for this class. 82 82 83 - Basicinformationregardingparameter names, their types, and optionality is obtained thanks to{{codelanguage="none"}}ReflectionClass{{/code}}, with the order of method arguments,their typehintsbeing crucial.82 +The main information about parameter names, their types, and optionality is obtained thanks to (% class="box code" %)ReflectionClass(%%), with the important points being the order of method arguments and their type hints. 84 84 ))) 85 85 86 86 (% class="col-xs-12 col-sm-6" %) ... ... @@ -87,47 +87,47 @@ 87 87 ((( 88 88 {{code language="json" layout="LINENUMBERS" title="== Documentation =="}} 89 89 { 90 - 91 - 92 - 93 - 94 - 95 - 96 - 97 - 98 - 99 - 89 + "methods": { 90 + "ExampleApi.getUserNameByUuid": { 91 + "name": "ExampleApi.getUserNameByUuid", 92 + "description": "", 93 + "parameters": { 94 + "userId": { 95 + "type": "string", 96 + "name": "userId", 97 + "description": "", 98 + "optional": false 100 100 } 101 101 }, 102 - 103 - 101 + "returns": "string", 102 + "responseFormat": "string" 104 104 }, 105 - 106 - 107 - 108 - 109 - 110 - 111 - 112 - 113 - 104 + "ExampleApi.sendEmail": { 105 + "name": "ExampleApi.sendEmail", 106 + "description": "", 107 + "parameters": { 108 + "email": { 109 + "type": "string", 110 + "name": "email", 111 + "description": "", 112 + "optional": false 114 114 }, 115 - 116 - 117 - 118 - 119 - 114 + "text": { 115 + "type": "string", 116 + "name": "text", 117 + "description": "", 118 + "optional": false 120 120 }, 121 - 122 - 123 - 124 - 125 - 126 - 120 + "subject": { 121 + "type": "string", 122 + "name": "subject", 123 + "description": "", 124 + "optional": true, 125 + "default": "Message without subject" 127 127 } 128 128 }, 129 - 130 - 128 + "returns": "boolean", 129 + "responseFormat": "boolean" 131 131 } 132 132 } 133 133 } ... ... @@ -145,10 +145,10 @@ 145 145 ((( 146 146 {{code language="json" layout="LINENUMBERS" title="Request"}} 147 147 { 148 - 149 - 150 - 151 - 147 + "id": "example_request", 148 + "method": "ExampleApi.getUserNameByUuid", 149 + "params": { 150 + "userId": "1111" 152 152 } 153 153 } 154 154 {{/code}} ... ... @@ -158,9 +158,9 @@ 158 158 ((( 159 159 {{code language="json" layout="LINENUMBERS" title="Response"}} 160 160 { 161 - 162 - 163 - 160 + "id": "example_request", 161 + "result": "some result", 162 + "jsonrpc": "2.0" 164 164 } 165 165 {{/code}} 166 166 ))) ... ... @@ -172,12 +172,12 @@ 172 172 ((( 173 173 {{code language="json" layout="LINENUMBERS" title="Request"}} 174 174 { 175 - 176 - 177 - 178 - 179 - 180 - sendmail by API"174 + "id": "example_request", 175 + "method": "ExampleApi.sendEmail", 176 + "params": { 177 + "email": "user@example.com", 178 + "subject": "This is a test mail", 179 + "text": "Hi! This is a test email sent by the API" 181 181 } 182 182 } 183 183 {{/code}} ... ... @@ -187,9 +187,9 @@ 187 187 ((( 188 188 {{code language="json" layout="LINENUMBERS" title="Response"}} 189 189 { 190 - 191 - 192 - 189 + "id": "example_request", 190 + "result": true, 191 + "jsonrpc": "2.0" 193 193 } 194 194 {{/code}} 195 195 ))) ... ... @@ -197,14 +197,14 @@ 197 197 198 198 (% class="box warningmessage" %) 199 199 ((( 200 -To simplify the understandingof the documentation, allsubsequent examples will be provided using a single method (//**sendEmail**//).199 +To simplify the documentation, all further examples will be provided using a single method (//**sendEmail**//). 201 201 ))) 202 202 203 -= Method and Parameter Descriptions=202 += Description of Methods and Parameters = 204 204 205 -The documenter relies on all available data relatedto the method and its arguments (names,types ofinput and output data, docblocks). Thus, the descriptionsof methods and parameters can be enrichedthroughdocblocks.204 +The documenter relies on all available data belonging to the method and its arguments (names, input and output data types, docblocks). Thus, the description of methods and parameters can be enriched using docblocks. 206 206 207 -Let's add a description of the method and parameters.206 +Let's add a description for the method and its parameters. 208 208 209 209 (% class="row" %) 210 210 ((( ... ... @@ -213,7 +213,6 @@ 213 213 {{code language="php" layout="LINENUMBERS" title="== Code =="}} 214 214 <?php 215 215 // ... 216 - 217 217 /** 218 218 * A method for sending an email message 219 219 * @param string $email The email address ... ... @@ -222,21 +222,17 @@ 222 222 * @return bool 223 223 */ 224 224 public function sendEmail( 225 - string $email, 226 - string $text, 223 + string $email, 224 + string $text, 227 227 string $subject = 'Message without subject' 228 - ): bool 229 - { 230 - // some logic send email 226 + ): bool { 227 + // some logic to send email 231 231 return true; 232 232 } 233 - 234 234 // ... 235 235 {{/code}} 236 236 237 237 Note the documentation now displays additional information about the method and its parameters. 238 - 239 - 240 240 ))) 241 241 242 242 (% class="col-xs-12 col-sm-6" %) ... ... @@ -243,33 +243,33 @@ 243 243 ((( 244 244 {{code language="json" layout="LINENUMBERS" title="== Documentation =="}} 245 245 { 246 - 247 - 248 - 249 - 250 - 251 - 252 - 253 - 254 - 255 - 240 + "methods": { 241 + "ExampleApi.sendEmail": { 242 + "name": "ExampleApi.sendEmail", 243 + "description": "A method for sending an email message", 244 + "parameters": { 245 + "email": { 246 + "type": "string", 247 + "name": "email", 248 + "description": "The email address", 249 + "optional": false 256 256 }, 257 - 258 - 259 - 260 - 261 - 251 + "text": { 252 + "type": "string", 253 + "name": "text", 254 + "description": "Message body", 255 + "optional": false 262 262 }, 263 - 264 - 265 - 266 - 267 - 268 - 257 + "subject": { 258 + "type": "string", 259 + "name": "subject", 260 + "description": "Optional message subject parameter", 261 + "optional": true, 262 + "default": "Message without subject" 269 269 } 270 270 }, 271 - 272 - 265 + "returns": "boolean", 266 + "responseFormat": "boolean" 273 273 } 274 274 } 275 275 } ... ... @@ -277,18 +277,17 @@ 277 277 ))) 278 278 ))) 279 279 280 -= RPC attributes =274 += RPC Attributes = 281 281 282 -For more flexible co nfiguration of your API methods, JsonRpcBundle usessuch a tool as[[phpattributes>>https://www.php.net/manual/en/language.attributes.overview.php]].276 +For more flexible customization of your API methods, JsonRpcBundle uses [[PHP attributes>>url:https://www.php.net/manual/en/language.attributes.overview.php]]. 283 283 284 -With specialized attributes, you can: 278 +With the help of specialized attributes, you can: 285 285 286 -* Set aliases for methods;287 -* Specify the response format for methods (if the response is in the form of an array,anobject, oracollection of objects);288 -* Set up validationof input parameters;289 -* Configurecachingofresponses.280 +* set aliases for methods; 281 +* specify the response format for methods (if the response is in the form of an array, object, or collection of objects); 282 +* configure input parameter validation; 283 +* set up response caching. 290 290 291 - Learnmore about these attributes:285 +Read more about these attributes: 292 292 293 293 {{children/}} 294 -