Reputation: 9221
I'm working on my first API Wrapper as a Laravel package, it's working for now but all my methods ( getCompanies, getDepartments, ... ) are in the same class
Now I'm wondering if it's difficult to split up methods and group them in subclasses.
For example, now I'm calling the getCompanies as follows.
myApi::getCompanies();
I was wondering if it's not better to have
myApi::companies()->all();
What would be a good way to go about this? Or is it better to leave all methods in one file?
This is my package structure
And this is my main class code
<?php namespace Notflip\Teamleader;
class Teamleader {
private $api_secret;
public function __construct()
{
$this->api_secret = config('teamleader.API_SECRET');
}
public function getDepartments()
{
return $this->call("getDepartments");
}
private function call($endpoint, $params = [])
{
$params['api_group'] = $this->api_group;
$params['api_secret'] = $this->api_secret;
$url = $this->api_base_url . $endpoint . '.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$api_output = curl_exec($ch);
curl_close($ch);
return json_decode($api_output);
}
}
Thank you!
Upvotes: 0
Views: 535
Reputation: 1062
You could be creating a separate client and create separate implementation parts of the API that require the client.
E.g.
Class `Client` {
// implements the actual curl
public function get($path, $params = []);
}
And then create another class that grabs the client and does work with it:
class Departments {
public function all(){
$data = $this->getClient()->get();
}
}
Upvotes: 1