From df031a7563db5148a512a0cdadd99711a6170208 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 19 Jan 2024 10:15:55 +0100 Subject: [PATCH] feat(api): add a curl example to the api_token creation --- app/assets/stylesheets/code_example.scss | 22 +++++++++++++++++++ .../administrateurs/api_tokens_controller.rb | 12 ++++++++++ .../api_tokens/create.html.haml | 17 +++++++++++++- 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 app/assets/stylesheets/code_example.scss diff --git a/app/assets/stylesheets/code_example.scss b/app/assets/stylesheets/code_example.scss new file mode 100644 index 000000000..93936618f --- /dev/null +++ b/app/assets/stylesheets/code_example.scss @@ -0,0 +1,22 @@ +@import "constants"; +@import "colors"; + +.code-example { + background-color: var(--background-contrast-grey); + + .example-header { + display: flex; + background-color: var(--background-alt-grey); + align-items: center; + + .lang { + margin-right: auto; + padding: $default-padding; + } + + } + + pre { + padding: 0 $default-padding; + } +} diff --git a/app/controllers/administrateurs/api_tokens_controller.rb b/app/controllers/administrateurs/api_tokens_controller.rb index 8c178d0ef..b75650a34 100644 --- a/app/controllers/administrateurs/api_tokens_controller.rb +++ b/app/controllers/administrateurs/api_tokens_controller.rb @@ -30,6 +30,8 @@ module Administrateurs @api_token.update!(name:, write_access:, allowed_procedure_ids:, authorized_networks:, expires_at:) + + @curl_command = curl_command(@packed_token, @api_token.procedure_ids.first) end def edit @@ -60,6 +62,16 @@ module Administrateurs private + def curl_command(packed_token, procedure_id) + <<~EOF + curl \\ + -H 'Content-Type: application/json' \\ + -H 'Authorization: Bearer #{packed_token}' \\ + --data '{ "query": "{ demarche(number: #{procedure_id}) { title } }" }' \\ + '#{api_v2_graphql_url}' + EOF + end + def all_params [:name, :access, :target, :targets, :networkFiltering, :networks, :lifetime, :customLifetime] .index_with { |param| params[param] } diff --git a/app/views/administrateurs/api_tokens/create.html.haml b/app/views/administrateurs/api_tokens/create.html.haml index b82663a6e..c82e15f12 100644 --- a/app/views/administrateurs/api_tokens/create.html.haml +++ b/app/views/administrateurs/api_tokens/create.html.haml @@ -14,7 +14,22 @@ %p{ data: { controller: 'clipboard', clipboard_text_value: @packed_token } } %code= @packed_token - %button.fr-btn.fr-btn-primary.fr-btn-small.fr-ml-2w{ data: { action: 'clipboard#copy' }, title: 'Copier dans le presse-papier' } Copier + %button.fr-btn.fr-btn-primary.fr-btn-small.fr-ml-2w{ data: { action: 'clipboard#copy', clipboard_target: 'toHide' }, title: 'Copier dans le presse-papier' } Copier + %button.fr-btn.fr-btn-primary.fr-btn-small.fr-ml-2w.hidden{ data: { clipboard_target: 'success' } } Copié ! + + %p.fr-mt-4w Voici un premier exemple d'utilisation de votre jeton d'API. Il vous permet de récupérer le titre de l'une de vos démarches : + + .code-example{ data: { controller: 'clipboard', clipboard_text_value: @curl_command } } + .example-header + %span.lang bash + %button.fr-btn.fr-btn--icon-left.fr-icon-clipboard-line.fr-btn--sm.fr-btn--tertiary-no-outline{ data: { action: 'clipboard#copy', clipboard_target: 'toHide' }, title: 'Copier dans le presse-papier' } Copier + %button.fr-btn.fr-btn--icon-left.fr-icon-checkbox-line.fr-btn--sm.fr-btn--tertiary-no-outline.hidden{ data: { clipboard_target: 'success' } } Copié ! + %pre + %code= @curl_command + + %p.fr-text--sm.fr-text-mention--grey + Pour continuer, vous pouvez consulter + = link_to("notre documentation sur l'API", API_DOC_URL, **external_link_attributes) %p.fr-mt-4w Résumé des informations du jeton : %ul