Weave Code
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

Commons Uptime Robot Bundle Laravel Package

20steps/commons-uptime-robot-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation Run:

    composer require 20steps/commons-uptime-robot-bundle
    

    Ensure your config/packages/twentysteps_commons_uptime_robot.yaml includes:

    twentysteps_commons_uptime_robot:
        api_key: "%env(UPTIMEROBOT_API_KEY)%"
    

    (Use .env for security.)

  2. First Use Case Inject the API service into a controller or command:

    use Twentysteps\Commons\UptimeRobotBundle\API\UptimeRobotAPI;
    
    class MyController {
        public function __construct(private UptimeRobotAPI $uptimeRobotAPI) {}
    }
    

    Verify connectivity with:

    $this->uptimeRobotAPI->getMyInfo();
    

Implementation Patterns

Core Workflows

  1. Monitor Creation Define a monitor via Model\Monitor and create it:

    $monitor = new Model\Monitor();
    $monitor->setFriendlyName('My Service')
            ->setUrl('https://example.com')
            ->setType(Model\Monitor::TYPE_HTTP)
            ->setInterval(5); // Minutes
    
    $this->uptimeRobotAPI->createMonitor($monitor);
    
  2. Alert Contacts Manage contacts (e.g., emails) via Model\AlertContact:

    $contact = new Model\AlertContact();
    $contact->setContact('admin@example.com')
            ->setType(Model\AlertContact::TYPE_EMAIL);
    
    $this->uptimeRobotAPI->createAlertContact($contact);
    
  3. Maintenance Windows Schedule downtime for monitors:

    $window = new Model\MaintenanceWindow();
    $window->setMonitorId($monitorId)
           ->setStartTime('2023-12-25T12:00:00')
           ->setEndTime('2023-12-25T13:00:00');
    
    $this->uptimeRobotAPI->createMaintenanceWindow($window);
    

Integration Tips

  • Event-Driven Deployments Trigger monitor updates during deployments (e.g., via Symfony KernelEvents):
    $event->getResponse()->headers->set('X-UptimeRobot-Monitor', $monitorId);
    
  • Caching Cache API responses (e.g., monitor lists) using Symfony’s cache system:
    $monitors = $this->uptimeRobotAPI->getMonitors();
    $this->cache->set('uptime_robot_monitors', $monitors, '0', new \DateInterval('P1D'));
    
  • Error Handling Wrap API calls in try-catch blocks:
    try {
        $this->uptimeRobotAPI->createMonitor($monitor);
    } catch (\Exception $e) {
        $this->logger->error('UptimeRobot API Error: ' . $e->getMessage());
    }
    

Gotchas and Tips

Pitfalls

  1. API Key Security

    • Never hardcode API keys. Use .env and Symfony’s %env().
    • Restrict UptimeRobot API key permissions to only necessary actions (e.g., "Monitors: Read/Write").
  2. Rate Limits

    • UptimeRobot’s API has rate limits. Handle 429 Too Many Requests gracefully:
      if ($e->getCode() === 429) {
          sleep(60); // Retry after 1 minute
      }
      
  3. Monitor Types

    • Ensure setType() matches UptimeRobot’s valid types (e.g., TYPE_HTTP, TYPE_HTTPS, TYPE_PING).
  4. Time Zones

    • Use UTC for MaintenanceWindow timestamps to avoid timezone-related issues:
      $window->setStartTime((new \DateTime('now', new \DateTimeZone('UTC')))->format(\DateTime::ATOM));
      

Debugging

  • Enable API Logging Configure Monolog in config/packages/monolog.yaml to log API requests/responses:

    handlers:
        uptime_robot:
            type: stream
            path: "%kernel.logs_dir%/uptime_robot.log"
            level: debug
    

    Then inject the logger:

    $this->uptimeRobotAPI->setLogger($this->logger);
    
  • Validate Responses Use getLastResponse() to inspect raw API responses:

    $response = $this->uptimeRobotAPI->getMonitors();
    $this->logger->debug('API Response:', [$response->getBody()]);
    

Extension Points

  1. Custom Models Extend Model\Monitor or Model\AlertContact to add application-specific fields:

    class CustomMonitor extends Model\Monitor {
        private $customField;
    
        public function setCustomField($value) { $this->customField = $value; }
        public function getCustomField() { return $this->customField; }
    }
    
  2. API Client Overrides Override the default UptimeRobotAPI service in config/services.yaml:

    Twentysteps\Commons\UptimeRobotBundle\API\UptimeRobotAPI:
        arguments:
            $apiKey: '%env(UPTIMEROBOT_API_KEY)%'
            $baseUri: 'https://api.uptimerobot.com/v2'
    
  3. Webhook Handling Use Symfony’s HttpClient to process UptimeRobot webhook notifications:

    $client = $this->httpClient->request('POST', 'https://api.uptimerobot.com/v2/getMonitor', [
        'headers' => ['Authorization' => 'Bearer ' . $this->uptimeRobotAPI->getApiKey()],
        'json' => ['api_key' => $this->uptimeRobotAPI->getApiKey(), 'format' => 'json']
    ]);
    
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.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky