Giter VIP home page Giter VIP logo

openfoodfacts-laravel's Introduction

Laravel Open Food Facts API

Latest Version on Packagist Code Coverage Quality Score

This package provides a convenient wrapper to the Open Food Facts API for Laravel applications.

Requirements

  • PHP 8.1+
  • Laravel 9.x, 10.x, 11.x

Installation

You can install the package via composer:

composer require openfoodfacts/openfoodfacts-laravel

Legacy support

  • PHP 8.0 (Laravel <=9.x): composer require "openfoodfacts/openfoodfacts-laravel:^0.3"
  • PHP 7.2-7.4.x (Laravel 5.7-8.x): composer require "openfoodfacts/openfoodfacts-laravel:^0.2"

Usage

Find product details by barcode

OpenFoodFacts::barcode('20203467');

it returns an array with product details:

Array
(
    [product_name] => Cantuccini with hazelnuts
    [image_url] => https://static.openfoodfacts.org/images/products/20203467/front_fr.4.400.jpg
    ...    
)    

Find products that match a search term:

$collection = OpenFoodFacts::find('Coca Cola Zero');

// returns a Illuminate\Support\Collection of arrays with details of each product found

Contributing

You're very welcome to contribute. We coordinate on the Open Food Facts slack, on the #PHP channel : https://slack.openfoodfacts.org Please see CONTRIBUTING for details.

License

The MIT License (MIT). Please see License File for more information.

Third party applications

If you use this SDK, feel free to open a PR to add your application in this list.

Authors

openfoodfacts-laravel's People

Contributors

deepsource-autofix[bot] avatar deepsourcebot avatar dependabot-preview[bot] avatar dependabot[bot] avatar epalmans avatar github-actions[bot] avatar quadrubo avatar teolemon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

openfoodfacts-laravel's Issues

Do not cache product not found responses when calling OpenFoodFacts::barcode()

Epic: #

Description

I tried to pass a product bar code number that did not existed yet, then added it by contributing through the OpenFoodFact Android App, then called the API again and still no product found.

I flushed my cache, called the method again, and this time the correct response is returned.

Acceptance criteria

Only cache results for product found, do not cache product not found results (to allow subsequent retries to hopefully find the product if a contributor add it afterward).

What would a demo look like

  1. Setup a Laravel cache driver (file, database, ...)
  2. Call OpenFoodFacts::barcode($code);, where $code is a bar code of a product not in the database yet
  3. Add the product (either through the mobile app or the website)
  4. Call OpenFoodFacts::barcode($code);, where $code is a bar code of the product just added
  5. No exception "Product not found", the result is the product JSON response

Notes

I noticed the cache is set after the JSON response it fetched. I did not dived into it in details, but if it is possible to figure out if the product is not found at this point, maybe we should prevent caching at this level?

https://github.com/openfoodfacts/openfoodfacts-php/blob/develop/src/Api.php#L431-L435

Edit: In fact, I just tried to dd at this point, by testing with product number "3760314500074", and the response is the following:

[
  "code" => "3760314500074"
  "status" => 0
  "status_verbose" => "product not found"
]

So the fix for me (if you think it make sense), would be to update the code like this

class Api
{
  // ...

  private function fetch(string $url, bool $isJsonFile = true): array
  {
    // ...

    $jsonResult = json_decode($response->getBody(), true);

-   if (!empty($this->cache) && !empty($jsonResult)) {
+   if (!empty($this->cache) && !empty($jsonResult) && (($jsonResult["status"] ?? 0) !== 0) {
      $this->cache->set($cacheKey, $jsonResult);
    }
  }
}

Tasks

  • Task 1
  • Task 1
  • Task 1

Installation problem

Hi, there is one issue when installed

laravel 8.0
guzzle 7.2

./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Can only install one of: guzzlehttp/guzzle[6.5.x-dev, 7.2.0].
    - Can only install one of: guzzlehttp/guzzle[7.2.0, 6.5.x-dev].
    - Can only install one of: guzzlehttp/guzzle[6.5.x-dev, 7.2.0].
    - openfoodfacts/openfoodfacts-php 0.2.3 requires guzzlehttp/guzzle ^6.3 -> satisfiable by guzzlehttp/guzzle[6.5.x-dev].
    - openfoodfacts/openfoodfacts-laravel v0.2 requires openfoodfacts/openfoodfacts-php ^0.2.3 -> satisfiable by openfoodfacts/openfoodfacts-php[0.2.3].
    - Installation request for openfoodfacts/openfoodfacts-laravel ^0.2.0 -> satisfiable by openfoodfacts/openfoodfacts-laravel[v0.2].
    - Installation request for guzzlehttp/guzzle (locked at 7.2.0, required as ^7.0.1) -> satisfiable by guzzlehttp/guzzle[7.2.0].

composer fail : laravel 9 compatibility

Hello, when i tried to install it in my project i had this error !

 Problem 1
    - openfoodfacts/openfoodfacts-laravel[dev-dependabot/github_actions/actions/cache-3.0.7, dev-explicit_geography, dev-master, v0.3.0] require openfoodfacts/openfoodfacts-php ^0.2.4 -> satisfiable by openfoodfacts/openfoodfacts-php[0.2.4].
    - openfoodfacts/openfoodfacts-laravel v0.1 requires illuminate/support 5.7.x|5.8.x|^6.0|^7.0 -> found illuminate/support[v5.7.0, ..., 5.8.x-dev, v6.0.0, ..., 6.x-dev, v7.0.0, ..., 7.x-dev] but these were not loaded, likely because it conflicts with another require.
    - openfoodfacts/openfoodfacts-laravel[v0.2, ..., v0.2.2] require illuminate/support 5.7.x|5.8.x|^6.0|^7.0|^8.0 -> found illuminate/support[v5.7.0, ..., 5.8.x-dev, v6.0.0, ..., 6.x-dev, v7.0.0, ..., 7.x-dev, v8.0.0, ..., 8.x-dev] but these were not loaded, likely because it conflicts with another require.
    - openfoodfacts/openfoodfacts-php 0.2.4 requires psr/log ^1.0 -> found psr/log[1.0.0, ..., 1.1.4] but the package is fixed to 3.0.0 (lock file version) by a partial update and that version does not match. Make sure you list it as an argument for the update command.
    - Root composer.json requires openfoodfacts/openfoodfacts-laravel * -> satisfiable by openfoodfacts/openfoodfacts-laravel[dev-dependabot/github_actions/actions/cache-3.0.7, dev-explicit_geography, dev-master, v0.1, ..., v0.3.0, 9999999-dev].

Use the option --with-all-dependencies (-W) to allow upgrades, downgrades and removals for packages currently locked to specific versions.

Can anyone help me in this ! Im junior developper so im not very experimented thanks !!

Assign geographic variable when OpenFoodFacts class is instantiated

The geographic subdomain is assigned via the config:

parent::__construct([
  'geography' =>  $app['config']->get('openfoodfacts.geography'),
  'app' =>  $app['config']->get('app.name'),
], $app['cache.store']);

This doesn't allow for the API subdomain to be conditionally changed if the user is in another country. It would be great if this value could be assigned when the class instantiated.

Require a custom user agent to use the SDK

What

  • Require a custom user agent to use the SDK
  • Using the SDK and the API in general will remain as of today: free and accessible for everyone.
    IN the same way, we don't want to implement an API key mechanism and all that kind of stuff.
  • However, due to misuse of the API through various SDKs, we will now force all language clients to have a valid User Agent so that we can reach out in case of problems.
  • For now, We will only require an app name for the user agent.

Cannot install with laravel 11.7

What

The installation via composer doesn't work on Laravel 11.7.0

Steps to reproduce the behavior:

  1. composer require openfoodfacts/openfoodfacts-laravel on a Laravel project with version 11.7.0

Expected behavior

Installing the package with composer :)

Screenshots

Problem 1
    - openfoodfacts/openfoodfacts-laravel[v0.4.0, ..., v0.4.1] require openfoodfacts/openfoodfacts-php ^0.3.0 -> satisfiable by openfoodfacts/openfoodfacts-php[v0.3.0].
    - openfoodfacts/openfoodfacts-laravel v0.1 requires illuminate/support 5.7.x|5.8.x|^6.0|^7.0 -> found illuminate/support[v5.7.0, ..., v5.8.36, v6.0.0, ..., v6.20.44, v7.0.0, ..., v7.30.6] but these were not loaded, likely because it conflicts with another require.
    - openfoodfacts/openfoodfacts-laravel[v0.2, ..., v0.2.2] require illuminate/support 5.7.x|5.8.x|^6.0|^7.0|^8.0 -> found illuminate/support[v5.7.0, ..., v5.8.36, v6.0.0, ..., v6.20.44, v7.0.0, ..., v7.30.6, v8.0.0, ..., v8.83.27] but these were not loaded, likely because it conflicts with another require.
    - openfoodfacts/openfoodfacts-laravel v0.3.0 requires illuminate/support ^9.0 -> found illuminate/support[v9.0.0, ..., v9.52.16] but these were not loaded, likely because it conflicts with another require.
    - openfoodfacts/openfoodfacts-php v0.3.0 requires ext-gd * -> it is missing from your system. Install or enable PHP's gd extension.
    - Root composer.json requires openfoodfacts/openfoodfacts-laravel * -> satisfiable by openfoodfacts/openfoodfacts-laravel[v0.1, ..., v0.4.1].

Platform (Desktop, Mobile, Hunger Games)

  • OS: Debian in Docker container
  • Platform: WSL2 (Debian) on Windows 11, PHP 8.3.8

Thank you!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.