- Création API recherche

This commit is contained in:
2025-01-29 10:15:16 +01:00
parent 3797ef0582
commit f2dd1047a1
6 changed files with 249 additions and 1 deletions

View File

@ -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",

172
composer.lock generated
View File

@ -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",

View File

@ -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.

View File

@ -0,0 +1,25 @@
<?php
namespace App\Controller;
use App\Service\GoogleBooksService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
class APITestController extends AbstractController
{
private GoogleBooksService $googleBooksService;
public function __construct(GoogleBooksService $googleBooksService)
{
$this->googleBooksService = $googleBooksService;
}
#[Route('/api/search', name: 'api_search')]
public function search(): JsonResponse
{
$results = $this->googleBooksService->searchBooks('Le loup des steppes');
return $this->json($results);
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace App\Service;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class GoogleBooksService
{
private HttpClientInterface $client;
public function __construct(HttpClientInterface $client)
{
$this->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;
}
}

View File

@ -0,0 +1,20 @@
{% extends 'base.html.twig' %}
{% block title %}Hello APITestController!{% endblock %}
{% block body %}
<style>
.example-wrapper { margin: 1em auto; max-width: 800px; width: 95%; font: 18px/1.5 sans-serif; }
.example-wrapper code { background: #F5F5F5; padding: 2px 6px; }
</style>
<div class="example-wrapper">
<h1>Hello {{ controller_name }}! ✅</h1>
This friendly message is coming from:
<ul>
<li>Your controller at <code><a href="{{ '/var/www/html/src/Controller/APITestController.php'|file_link(0) }}">src/Controller/APITestController.php</a></code></li>
<li>Your template at <code><a href="{{ '/var/www/html/templates/api_test/index.html.twig'|file_link(0) }}">templates/api_test/index.html.twig</a></code></li>
</ul>
</div>
{% endblock %}