Giter VIP home page Giter VIP logo

pingping's Introduction

PingPing

This composer package allows us to easily integrate PingPing APIs in your Laravel project.

What is PingPing ?

PingPing is the simplest uptime monitoring service in the world to get notified, when your website is down or your certificate becomes invalid. No more, no less.

Installation

Step 1: Composer

Firstly require this package using following command.

composer require enlight/pingping

Step 2: Service Provider (Optional)

This package support's auto discovery but for any reason you need to add ServiceProvider into providers array manually then check follow steps below.

Open config/app.php and, within the providers array, append:

Enlight\PingPing\Providers\PingPingServiceProvider::class

This will bootstrap the package into Laravel.

Step 3: Set Up Environment

Check your .env file, and ensure that your PING_PING_API_TOKEN is set with valid token.

You can get the token from below link and make sure it is enabled.

https://pingping.io/account/settings

You are all set to use it.

Step 4: Publish Configuration (Optional)

Optionally, You can publish configuration file, so you can modify defaults values.

To Publish Configuration Run

php artisan vendor:publish --provider="Enlight\PingPing\Providers\PingPingServiceProvider" --tag="config"

Exported config you can find in /config folder as pingping.php.

Usage

All methods and API calls will return Illuminate\Http\Client\Response instance. That mean's, you have access to following methods.

$response->body() : string;
$response->json() : array|mixed;
$response->collect() : Illuminate\Support\Collection;
$response->status() : int;
$response->ok() : bool;
$response->successful() : bool;
$response->failed() : bool;
$response->serverError() : bool;
$response->clientError() : bool;
$response->header($header) : string;
$response->headers() : array;

Retrieve all websites (monitors)

    use Enlight\PingPing\Client;
    
    public function index(Client $client)
    {
        $response = $client->monitors();

        if ($response->failed()) {
            return $response->json();
        }

        return $response->json();
    }

Retrieve a specific website (monitor)

    use Enlight\PingPing\Client;
    
    public function show($id, Client $client)
    {
        $response = $client->monitors((int) $id);

        if ($response->failed()) {
            return $response->json();
        }

        return $response->json();
    }

Retrieve statistics from a specific website (monitor)

    use Enlight\PingPing\Client;
    use Enlight\PingPing\Exceptions\MonitorIDRequiredException;
    
    /**
     * @throws MonitorIDRequiredException
     */
    public function show($id, Client $client)
    {
        $response = $client->statistics((int) $id);

        if ($response->failed()) {
            return $response->json();
        }

        return $response->json();
    }

Create a website (monitor)

    use Enlight\PingPing\Client;
    use Enlight\PingPing\Exceptions\ValidUrlRequiredException;
   
    /**
     * @throws ValidUrlRequiredException
     */
    public function store(Client $client)
    {
        $url = 'https://my-cool-website.test';

        $response = $client->createMonitor($url);

        if ($response->failed()) {
            return $response->json();
        }

        return $response->json();
    }

Update a website (monitor)

    use Enlight\PingPing\Client;
    use Enlight\PingPing\Exceptions\AliasRequiredException;
    use Enlight\PingPing\Exceptions\ValidUrlRequiredException;
    use Enlight\PingPing\Exceptions\MonitorIDRequiredException;
    
    /**
     * @throws AliasRequiredException
     * @throws ValidUrlRequiredException
     * @throws MonitorIDRequiredException
     */
    public function update($id, Client $client)
    {
        $url = 'https://my-cool-website2.test';
        $alias = 'My cool website2';

        $response = $client->updateMonitor((int) $id, $url, $alias);

        if ($response->failed()) {
            return $response->json();
        }

        return $response->json();
    }

Delete a website (monitor)

    use Enlight\PingPing\Client;
    use Enlight\PingPing\Exceptions\MonitorIDRequiredException;
    
    /**
     * @throws MonitorIDRequiredException
     */
    public function destroy($id, Client $client)
    {
        $response = $client->deleteMonitor((int) $id);

        if ($response->failed()) {
            return $response->json();
        }

        return $response->json();
    }

Finally, your updated controllers might look like this now.

<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use Enlight\PingPing\Client;
use Enlight\PingPing\Exceptions\AliasRequiredException;
use Enlight\PingPing\Exceptions\ValidUrlRequiredException;
use Enlight\PingPing\Exceptions\MonitorIDRequiredException;

class MonitorController extends Controller
{
    /** @var Client */
    private $client;

    public function __construct(Client $client)
    {
        $this->client = $client;
    }

    public function index()
    {
        $response = $this->client->monitors();

        if ($response->failed()) {
            return $response->json();
        }

        return $response->json();
    }

    public function show($id)
    {
        $response = $this->client->monitors((int) $id);

        if ($response->failed()) {
            return $response->json();
        }

        return $response->json();
    }

    /**
     * @throws ValidUrlRequiredException
     */
    public function store()
    {
        $url = 'https://my-cool-website.test';

        $response = $this->client->createMonitor($url);

        if ($response->failed()) {
            return $response->json();
        }

        return $response->json();
    }

    /**
     * @throws AliasRequiredException
     * @throws ValidUrlRequiredException
     * @throws MonitorIDRequiredException
     */
    public function update($id)
    {
        $url = 'https://my-cool-website2.test';
        $alias = 'My cool website2';

        $response = $this->client->updateMonitor((int) $id, $url, $alias);

        if ($response->failed()) {
            return $response->json();
        }

        return $response->json();
    }

    /**
     * @throws MonitorIDRequiredException
     */
    public function destroy($id)
    {
        $response = $this->client->deleteMonitor((int) $id);

        if ($response->failed()) {
            return $response->json();
        }

        return $response->json();
    }
}
<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use Enlight\PingPing\Client;
use Enlight\PingPing\Exceptions\MonitorIDRequiredException;

class StatisticsController extends Controller
{
    /**
     * @throws MonitorIDRequiredException
     */
    public function show($id, Client $client)
    {
        $response = $client->statistics((int) $id);

        if ($response->failed()) {
            return $response->json();
        }

        return $response->json();
    }
}

Contributors

This package is inspired by the Steve's blog post on Working with third party services in Laravel . I highly recommend you to read it.

Thank You :)

Happy Coding.

pingping's People

Contributors

bhushan avatar bobbybouwmann avatar

Stargazers

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

Watchers

 avatar

pingping's Issues

Replace HTTP to Guzzle

Currently, We are using HTTP to send API calls which limits us to use Laravel 7 and above.

If we use Guzzle directly then Other people who have a lower version of Laravel can also use this package.

Replace Responses with DTO

It Will be a huge task as

  • Check the Spatie DTO package if it adds value, we will use it, otherwise, go for a simple standard PHP object
  • Create DTOs for a different response from APIs
  • How to handle different statuses from API using DTO?
  • How to give a response back if we get a failure response from PingPing API as DTO?

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.