|
| 1 | +Launch a process through the HTTP API |
| 2 | +===================================== |
| 3 | + |
| 4 | +This recipe lets another application (an ERP, a CI job, a webhook...) trigger a process, with parameters, through an |
| 5 | +HTTP request. Here, the calling application asks to fetch the communes of a postal code from a REST API. |
| 6 | + |
| 7 | +```yaml |
| 8 | +# config/packages/process/app.fetch_communes.yaml |
| 9 | +clever_age_process: |
| 10 | + configurations: |
| 11 | + app.fetch_communes: |
| 12 | + description: 'Fetch the communes of a postal code' |
| 13 | + help: 'Ex: bin/console cleverage:process:execute app.fetch_communes -c codePostal:"46800"' |
| 14 | + public: true # Default value: private processes cannot be launched through the HTTP API |
| 15 | + options: |
| 16 | + ui: |
| 17 | + source: API Carto IGN |
| 18 | + target: Logs |
| 19 | + tasks: |
| 20 | + request: |
| 21 | + service: '@CleverAge\RestProcessBundle\Task\RequestTask' |
| 22 | + options: |
| 23 | + client: apicarto_ign |
| 24 | + url: '/codes-postaux/communes/{codePostal}' |
| 25 | + method: 'GET' |
| 26 | + url_parameters: { codePostal: '{{ codePostal }}' } # Contextualized option, set by the HTTP call |
| 27 | + outputs: [decode] |
| 28 | + |
| 29 | + decode: |
| 30 | + service: '@CleverAge\ProcessBundle\Task\TransformerTask' |
| 31 | + options: |
| 32 | + transformers: |
| 33 | + json_decode: ~ # Generic transformer defined in the application |
| 34 | + outputs: [log] |
| 35 | + |
| 36 | + log: |
| 37 | + service: '@CleverAge\ProcessBundle\Task\Reporting\LoggerTask' |
| 38 | + options: |
| 39 | + level: info |
| 40 | + message: 'Communes fetched' |
| 41 | + context: ['input'] |
| 42 | +``` |
| 43 | +
|
| 44 | +First, create a technical user for the calling application, e.g. |
| 45 | +`bin/console cleverage:ui-process:user-create erp@example.com 'a-long-random-password'`. Then, in the UI, open |
| 46 | +"Users > User List", edit this user and click on "Generate token". Copy the token displayed in the flash message: it |
| 47 | +will never be displayed again. |
| 48 | + |
| 49 | +The calling application can now queue the process: |
| 50 | + |
| 51 | +```bash |
| 52 | +curl --location 'https://your-domain.com/http/process/execute' \ |
| 53 | +--header 'Authorization: Bearer myBearerToken' \ |
| 54 | +--header 'Content-Type: application/json' \ |
| 55 | +--data '{ |
| 56 | + "code": "app.fetch_communes", |
| 57 | + "context": {"codePostal": "46800"} |
| 58 | +}' |
| 59 | +``` |
| 60 | + |
| 61 | +Response: |
| 62 | + |
| 63 | +```json |
| 64 | +"Process has been added to queue. It will start as soon as possible." |
| 65 | +``` |
| 66 | + |
| 67 | +How it works: |
| 68 | +- The `Authorization: Bearer` header is checked against the hashed token of the users |
| 69 | + (see [authentication](../reference/06-http_api.md#authentication)). A missing or invalid token returns a `401`. |
| 70 | +- `code` must be the code of an existing, public process, otherwise a `422` is returned with the violation message |
| 71 | + (see [parameters](../reference/06-http_api.md#parameters)). |
| 72 | +- `context` values are injected into the contextualized options, like `-c codePostal:"46800"` on the console. With |
| 73 | + form data instead of JSON, send them as `--form 'context[codePostal]="46800"'`. |
| 74 | +- By default, the process is queued to the `execute_process` transport and executed by a |
| 75 | + `messenger:consume execute_process` worker (see [messenger](../reference/08-messenger.md)): the HTTP response does |
| 76 | + not wait for the end of the process. Add `"queue": false` to execute it during the request: the response is then |
| 77 | + `200` if the process succeeded, or `500` with the exception message. |
| 78 | +- The execution, its logs and its report are available in "Process > Executions", like any other execution |
| 79 | + (see [process executions & logs](../reference/04-process_executions_and_logs.md)). |
| 80 | + |
| 81 | +The `apicarto_ign` client is a [RestProcessBundle](https://github.com/cleverage/rest-process-bundle) client registered |
| 82 | +in the application (a `CleverAge\RestProcessBundle\Client\Client` service with `$uri: 'https://apicarto.ign.fr/api'`, |
| 83 | +tagged `cleverage.rest.client`), and `json_decode` a generic transformer (see |
| 84 | +[generic transformers](https://github.com/cleverage/process-bundle/blob/main/docs/reference/03-generic_transformers_definition.md)). |
| 85 | +To send a file to the process instead, see the file upload example of the |
| 86 | +[form with file upload](form_file_upload.md) cookbook. |
0 commit comments