From f2dd1047a1f51f384d7f8ab024f9f1b9b81dae13 Mon Sep 17 00:00:00 2001 From: Kilian Beraud Date: Wed, 29 Jan 2025 10:15:16 +0100 Subject: [PATCH] =?UTF-8?q?-=20Cr=C3=A9ation=20API=20recherche?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 1 + composer.lock | 172 ++++++++++++++++++++++++++- config/services.yaml | 1 + src/Controller/APITestController.php | 25 ++++ src/services/GoogleService.php | 31 +++++ templates/api_test/index.html.twig | 20 ++++ 6 files changed, 249 insertions(+), 1 deletion(-) create mode 100644 src/Controller/APITestController.php create mode 100644 src/services/GoogleService.php create mode 100644 templates/api_test/index.html.twig diff --git a/composer.json b/composer.json index e5e90bb..366a982 100644 --- a/composer.json +++ b/composer.json @@ -14,6 +14,7 @@ "symfony/dotenv": "7.0.*", "symfony/flex": "^2", "symfony/framework-bundle": "7.0.*", + "symfony/http-client": "7.0.*", "symfony/runtime": "7.0.*", "symfony/twig-bundle": "7.0.*", "symfony/webpack-encore-bundle": "^2.2", diff --git a/composer.lock b/composer.lock index a8f09b4..f870b51 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "cb84a3e42b9bd770cec109b31198f139", + "content-hash": "9192d88f9ef21b003cb3086f6110fa18", "packages": [ { "name": "doctrine/cache", @@ -2836,6 +2836,176 @@ ], "time": "2023-12-29T15:37:40+00:00" }, + { + "name": "symfony/http-client", + "version": "v7.0.10", + "source": { + "type": "git", + "url": "https://github.com/symfony/http-client.git", + "reference": "3ae495c67ba9c3b504fecd070a6c28b4143088cf" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/http-client/zipball/3ae495c67ba9c3b504fecd070a6c28b4143088cf", + "reference": "3ae495c67ba9c3b504fecd070a6c28b4143088cf", + "shasum": "" + }, + "require": { + "php": ">=8.2", + "psr/log": "^1|^2|^3", + "symfony/http-client-contracts": "^3.4.1", + "symfony/service-contracts": "^2.5|^3" + }, + "conflict": { + "php-http/discovery": "<1.15", + "symfony/http-foundation": "<6.4" + }, + "provide": { + "php-http/async-client-implementation": "*", + "php-http/client-implementation": "*", + "psr/http-client-implementation": "1.0", + "symfony/http-client-implementation": "3.0" + }, + "require-dev": { + "amphp/amp": "^2.5", + "amphp/http-client": "^4.2.1", + "amphp/http-tunnel": "^1.0", + "amphp/socket": "^1.1", + "guzzlehttp/promises": "^1.4|^2.0", + "nyholm/psr7": "^1.0", + "php-http/httplug": "^1.0|^2.0", + "psr/http-client": "^1.0", + "symfony/dependency-injection": "^6.4|^7.0", + "symfony/http-kernel": "^6.4|^7.0", + "symfony/messenger": "^6.4|^7.0", + "symfony/process": "^6.4|^7.0", + "symfony/stopwatch": "^6.4|^7.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\HttpClient\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Provides powerful methods to fetch HTTP resources synchronously or asynchronously", + "homepage": "https://symfony.com", + "keywords": [ + "http" + ], + "support": { + "source": "https://github.com/symfony/http-client/tree/v7.0.10" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-07-17T06:06:58+00:00" + }, + { + "name": "symfony/http-client-contracts", + "version": "v3.5.2", + "source": { + "type": "git", + "url": "https://github.com/symfony/http-client-contracts.git", + "reference": "ee8d807ab20fcb51267fdace50fbe3494c31e645" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/http-client-contracts/zipball/ee8d807ab20fcb51267fdace50fbe3494c31e645", + "reference": "ee8d807ab20fcb51267fdace50fbe3494c31e645", + "shasum": "" + }, + "require": { + "php": ">=8.1" + }, + "type": "library", + "extra": { + "thanks": { + "url": "https://github.com/symfony/contracts", + "name": "symfony/contracts" + }, + "branch-alias": { + "dev-main": "3.5-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Contracts\\HttpClient\\": "" + }, + "exclude-from-classmap": [ + "/Test/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Generic abstractions related to HTTP clients", + "homepage": "https://symfony.com", + "keywords": [ + "abstractions", + "contracts", + "decoupling", + "interfaces", + "interoperability", + "standards" + ], + "support": { + "source": "https://github.com/symfony/http-client-contracts/tree/v3.5.2" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-12-07T08:49:48+00:00" + }, { "name": "symfony/http-foundation", "version": "v7.0.0", diff --git a/config/services.yaml b/config/services.yaml index 73b35dd..2d4ad79 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -6,6 +6,7 @@ parameters: services: + App\Service\GoogleBooksService: ~ # default configuration for services in *this* file _defaults: autowire: true # Automatically injects dependencies in your services. diff --git a/src/Controller/APITestController.php b/src/Controller/APITestController.php new file mode 100644 index 0000000..939022a --- /dev/null +++ b/src/Controller/APITestController.php @@ -0,0 +1,25 @@ +googleBooksService = $googleBooksService; + } + + #[Route('/api/search', name: 'api_search')] + public function search(): JsonResponse + { + $results = $this->googleBooksService->searchBooks('Le loup des steppes'); + return $this->json($results); + } +} diff --git a/src/services/GoogleService.php b/src/services/GoogleService.php new file mode 100644 index 0000000..aa04fd1 --- /dev/null +++ b/src/services/GoogleService.php @@ -0,0 +1,31 @@ +client = $client; + } + + public function searchBooks(string $query, string $lang = 'fr'): array + { + $url = 'https://www.googleapis.com/books/v1/volumes'; + $response = $this->client->request('GET', $url, [ + 'query' => [ + 'q' => $query, + 'langRestrict' => $lang, + ], + ]); + + // Convertir la réponse JSON en tableau PHP + $dataArray = $response->toArray(); + dump($dataArray); + return $dataArray; + } +} diff --git a/templates/api_test/index.html.twig b/templates/api_test/index.html.twig new file mode 100644 index 0000000..323f2e9 --- /dev/null +++ b/templates/api_test/index.html.twig @@ -0,0 +1,20 @@ +{% extends 'base.html.twig' %} + +{% block title %}Hello APITestController!{% endblock %} + +{% block body %} + + +
+

Hello {{ controller_name }}! ✅

+ + This friendly message is coming from: + +
+{% endblock %}