Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Simple Video Api Laravel Package

cgnetwork/simple-video-api

Lightweight Laravel/PHP wrapper for a simple video API. Provides straightforward methods to upload, fetch, and manage videos, with clean request handling and easy integration into your app or services. Ideal for quick prototypes and small backends.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require cgnetwork/simple-video-api
    

    Publish the config file (if needed):

    php artisan vendor:publish --provider="Cgnetwork\SimpleVideoApi\SimpleVideoApiServiceProvider" --tag="config"
    
  2. Configuration Edit config/simple-video-api.php with your API credentials (e.g., YouTube, Vimeo, or self-hosted endpoints). Example for YouTube:

    'default' => 'youtube',
    'youtube' => [
        'api_key' => env('YOUTUBE_API_KEY'),
        'base_url' => 'https://www.googleapis.com/youtube/v3/',
    ],
    
  3. First Use Case: Fetch a Video

    use Cgnetwork\SimpleVideoApi\Facades\SimpleVideoApi;
    
    $video = SimpleVideoApi::get('youtube')->video('dQw4w9WgXcQ');
    dd($video->title, $video->description);
    

Implementation Patterns

Common Workflows

  1. Fetching Video Metadata

    // Get a single video
    $video = SimpleVideoApi::get('vimeo')->video('123456789');
    
    // Get video stats (views, likes, etc.)
    $stats = $video->stats;
    
  2. Searching Videos

    $results = SimpleVideoApi::get('youtube')->search('laravel tutorials', 5);
    foreach ($results as $video) {
        echo $video->title . "\n";
    }
    
  3. Uploading Videos (Self-Hosted)

    $response = SimpleVideoApi::get('selfhosted')->upload(
        file_path: 'path/to/video.mp4',
        title: 'My Video',
        description: 'A test upload'
    );
    
  4. Integration with Eloquent

    // Store video ID in a database model
    $post->video_id = 'dQw4w9WgXcQ';
    $post->save();
    
    // Retrieve metadata later
    $video = SimpleVideoApi::get('youtube')->video($post->video_id);
    

Integration Tips

  • Caching: Cache API responses to reduce rate limits:
    $video = Cache::remember("video_{$videoId}", now()->addHours(1), function () use ($videoId) {
        return SimpleVideoApi::get('youtube')->video($videoId);
    });
    
  • Error Handling: Wrap API calls in try-catch:
    try {
        $video = SimpleVideoApi::get('youtube')->video('invalid_id');
    } catch (\Cgnetwork\SimpleVideoApi\Exceptions\ApiException $e) {
        Log::error("Video API error: " . $e->getMessage());
        return response()->json(['error' => 'Video not found'], 404);
    }
    
  • Rate Limiting: Respect API quotas (e.g., YouTube’s 10k units/day). Use middleware to track usage:
    // Example: Track API calls in a middleware
    public function handle($request, Closure $next) {
        $response = $next($request);
        if ($request->route()->getName() === 'video.api') {
            $this->trackApiUsage('youtube');
        }
        return $response;
    }
    

Gotchas and Tips

Pitfalls

  1. API Key Leaks

    • Never hardcode API keys in config. Use Laravel’s .env:
      YOUTUBE_API_KEY=your_key_here
      
    • Restrict keys to project-specific scopes in the provider’s dashboard (e.g., YouTube API Console).
  2. Rate Limits

    • YouTube/Vimeo enforce strict quotas. Monitor usage via:
      $quota = SimpleVideoApi::get('youtube')->quota();
      Log::info("Remaining quota: " . $quota->remaining);
      
    • Implement exponential backoff for retries:
      use Symfony\Component\Process\Exception\TimeoutException;
      
      try {
          $video = SimpleVideoApi::get('youtube')->video($id);
      } catch (TimeoutException $e) {
          sleep(2); // Wait before retry
          retry();
      }
      
  3. Deprecated Endpoints

    • The package may not support the latest API versions. Check the GitLab issues for updates.
    • Example: YouTube’s v3 API changes frequently. Test endpoints like /videos and /search before production use.
  4. Self-Hosted Quirks

    • For custom endpoints, ensure the server supports:
      • File uploads (e.g., POST /upload with multipart/form-data).
      • CORS headers if accessing from a frontend.

Debugging Tips

  • Enable Debug Mode Set debug to true in config/simple-video-api.php to log raw API responses:
    'debug' => env('APP_DEBUG', false),
    
  • Log API Responses Override the handleResponse method in a service provider:
    SimpleVideoApi::extend('youtube', function ($app) {
        return new \Cgnetwork\SimpleVideoApi\Adapters\YouTubeAdapter([
            'api_key' => config('services.youtube.key'),
            'debug' => true,
        ]);
    });
    
  • Test with Mock Data Use Laravel’s HTTP tests to mock API responses:
    public function test_video_fetch()
    {
        $mockResponse = ['id' => 'test123', 'title' => 'Mock Video'];
        $this->mock(\Cgnetwork\SimpleVideoApi\Adapters\YouTubeAdapter::class, function ($mock) {
            $mock->shouldReceive('video')->andReturn($mockResponse);
        });
    
        $video = SimpleVideoApi::get('youtube')->video('test123');
        $this->assertEquals('Mock Video', $video->title);
    }
    

Extension Points

  1. Add New Adapters Extend the package by creating a custom adapter:

    namespace App\Providers;
    
    use Cgnetwork\SimpleVideoApi\Adapters\AdapterInterface;
    use Cgnetwork\SimpleVideoApi\Adapters\AbstractAdapter;
    
    class CustomAdapter extends AbstractAdapter implements AdapterInterface
    {
        public function video($id)
        {
            // Custom logic for your video API
            return $this->request("GET", "/videos/{$id}");
        }
    }
    

    Register it in AppServiceProvider:

    SimpleVideoApi::extend('custom', function () {
        return new CustomAdapter(config('services.custom'));
    });
    
  2. Override Default Behavior Bind the facade to a custom manager in AppServiceProvider:

    $this->app->bind(\Cgnetwork\SimpleVideoApi\Contracts\Manager::class, function ($app) {
        return new \App\Services\CustomVideoManager();
    });
    
  3. Customize Response Handling Modify how responses are parsed by overriding the parseResponse method in your adapter:

    protected function parseResponse($response, $method, $endpoint)
    {
        // Custom parsing logic
        return json_decode($response, true);
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
coleus/frame
thtg88/journalism
filagora/filagora-ai-forums
rlacerda83/query-parser
socialdept/atp-orm
cboxdk/laravel-assistant-client
socialdept/atp-resolver
socialdept/tenant-domains
tautvydasr/blade-class-names
socialdept/color-scales
maherelgamil/controller
coleus/widgets
coleus/table
coleus/users
coleus/music
coleus/health
coleus/calendar
coleus/notes
coleus/support
coleus/settings