Class Organisation of Api WrappersHow can I verify a Google authentication API access token?What is a wrapper class?A definitive guide to API-breaking changes in .NETHow do I get a YouTube video thumbnail from the YouTube API?How to deal with authentication for a Ruby API wrapper?REST API Authorization & Authentication (web + mobile)SOAP with HTTP basic auth: put username+password in soapAction urls?HTML response support for Restler 3 APIsHow get params to Zend Framework 2 SoapHow to add extra logic on login condition in Laravel 5.2
Fizzy, soft, pop and still drinks
How much cash can I safely carry into the USA and avoid civil forfeiture?
Don’t seats that recline flat defeat the purpose of having seatbelts?
How to pronounce 'C++' in Spanish
Rivers without rain
Who is the Umpire in this picture?
Noun clause (singular all the time?)
Unexpected email from Yorkshire Bank
Can someone publish a story that happened to you?
Why was Germany not as successful as other Europeans in establishing overseas colonies?
What does KSP mean?
How do I deal with a coworker that keeps asking to make small superficial changes to a report, and it is seriously triggering my anxiety?
Will a top journal at least read my introduction?
With a Canadian student visa, can I spend a night at Vancouver before continuing to Toronto?
Examples of non trivial equivalence relations , I mean equivalence relations without the expression " same ... as" in their definition?
Why isn't the definition of absolute value applied when squaring a radical containing a variable?
Why was the Spitfire's elliptical wing almost uncopied by other aircraft of World War 2?
Controversial area of mathematics
What happened to Captain America in Endgame?
Critique of timeline aesthetic
What was the first Intel x86 processor with "Base + Index * Scale + Displacement" addressing mode?
What does the "ep" capability mean?
Why is it that the natural deduction method can't test for invalidity?
Is the 5 MB static resource size limit 5,242,880 bytes or 5,000,000 bytes?
Class Organisation of Api Wrappers
How can I verify a Google authentication API access token?What is a wrapper class?A definitive guide to API-breaking changes in .NETHow do I get a YouTube video thumbnail from the YouTube API?How to deal with authentication for a Ruby API wrapper?REST API Authorization & Authentication (web + mobile)SOAP with HTTP basic auth: put username+password in soapAction urls?HTML response support for Restler 3 APIsHow get params to Zend Framework 2 SoapHow to add extra logic on login condition in Laravel 5.2
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am trying to learn creating php wrappers for 3rd party apis. However I am confused on implementing multiple classes extended/implemented each other.
class Api
protected $username;
protected $password;
protected $wsdl = "http://example.com"
protected $client;
protected $account;
public function __construct($username, $password)
$this->client = new SoapClient($this->wsdl);
$authData = [
"Credentials" => [
"Username" => $username,
"Password" => $password
]
];
$this->makeCall('AuthenticateUser', $authData);
$this->account = $this->makeCall('GetAccountInfo', ["Authenticator" => $this->authenticator]);
protected function makeCall($method, $data)
$result = $this->client->$method($data);
$this->authenticator = $result->Authenticator;
return $result;
Until here, it makes sense. However, at this point I don't want to add all methods in this class. Thus, I decided to create a separate class for each method. And there, here problem begins.
class AddressValidator extends Api
public function validateAddress($data)
$response = $this->makeCall('validateAddress', $data);
dd($response);
Logically, how I need to call the wrapper (in my controller) is like this below, right?
$api = new Api($username, $password);
$api->validateAddress($params); // but I can't call this line with this setup
Instead, this works:
$api = new ValidateAddress($username, $password);
$api->validateAddress($params);
Makes sense, but is this the good way of organising it?
What is the beautiful way of setting up api wrapper? By the way, maybe I am wrong with this approach completely. I'd be happy to hear what you think
php laravel api wrapper
add a comment |
I am trying to learn creating php wrappers for 3rd party apis. However I am confused on implementing multiple classes extended/implemented each other.
class Api
protected $username;
protected $password;
protected $wsdl = "http://example.com"
protected $client;
protected $account;
public function __construct($username, $password)
$this->client = new SoapClient($this->wsdl);
$authData = [
"Credentials" => [
"Username" => $username,
"Password" => $password
]
];
$this->makeCall('AuthenticateUser', $authData);
$this->account = $this->makeCall('GetAccountInfo', ["Authenticator" => $this->authenticator]);
protected function makeCall($method, $data)
$result = $this->client->$method($data);
$this->authenticator = $result->Authenticator;
return $result;
Until here, it makes sense. However, at this point I don't want to add all methods in this class. Thus, I decided to create a separate class for each method. And there, here problem begins.
class AddressValidator extends Api
public function validateAddress($data)
$response = $this->makeCall('validateAddress', $data);
dd($response);
Logically, how I need to call the wrapper (in my controller) is like this below, right?
$api = new Api($username, $password);
$api->validateAddress($params); // but I can't call this line with this setup
Instead, this works:
$api = new ValidateAddress($username, $password);
$api->validateAddress($params);
Makes sense, but is this the good way of organising it?
What is the beautiful way of setting up api wrapper? By the way, maybe I am wrong with this approach completely. I'd be happy to hear what you think
php laravel api wrapper
Maybe something like a 3rd argument in theApi
constructor that defines what class you want to use (AddressValidator in this case) ?
– Scamtex
Mar 22 at 18:09
Do you mean in my Api class,public function __call($name, $arguments)
? And then$serviceClass = 'Service\' . ucfirst($name); $request = new $serviceClass(...$arguments);
– senty
Mar 22 at 18:16
add a comment |
I am trying to learn creating php wrappers for 3rd party apis. However I am confused on implementing multiple classes extended/implemented each other.
class Api
protected $username;
protected $password;
protected $wsdl = "http://example.com"
protected $client;
protected $account;
public function __construct($username, $password)
$this->client = new SoapClient($this->wsdl);
$authData = [
"Credentials" => [
"Username" => $username,
"Password" => $password
]
];
$this->makeCall('AuthenticateUser', $authData);
$this->account = $this->makeCall('GetAccountInfo', ["Authenticator" => $this->authenticator]);
protected function makeCall($method, $data)
$result = $this->client->$method($data);
$this->authenticator = $result->Authenticator;
return $result;
Until here, it makes sense. However, at this point I don't want to add all methods in this class. Thus, I decided to create a separate class for each method. And there, here problem begins.
class AddressValidator extends Api
public function validateAddress($data)
$response = $this->makeCall('validateAddress', $data);
dd($response);
Logically, how I need to call the wrapper (in my controller) is like this below, right?
$api = new Api($username, $password);
$api->validateAddress($params); // but I can't call this line with this setup
Instead, this works:
$api = new ValidateAddress($username, $password);
$api->validateAddress($params);
Makes sense, but is this the good way of organising it?
What is the beautiful way of setting up api wrapper? By the way, maybe I am wrong with this approach completely. I'd be happy to hear what you think
php laravel api wrapper
I am trying to learn creating php wrappers for 3rd party apis. However I am confused on implementing multiple classes extended/implemented each other.
class Api
protected $username;
protected $password;
protected $wsdl = "http://example.com"
protected $client;
protected $account;
public function __construct($username, $password)
$this->client = new SoapClient($this->wsdl);
$authData = [
"Credentials" => [
"Username" => $username,
"Password" => $password
]
];
$this->makeCall('AuthenticateUser', $authData);
$this->account = $this->makeCall('GetAccountInfo', ["Authenticator" => $this->authenticator]);
protected function makeCall($method, $data)
$result = $this->client->$method($data);
$this->authenticator = $result->Authenticator;
return $result;
Until here, it makes sense. However, at this point I don't want to add all methods in this class. Thus, I decided to create a separate class for each method. And there, here problem begins.
class AddressValidator extends Api
public function validateAddress($data)
$response = $this->makeCall('validateAddress', $data);
dd($response);
Logically, how I need to call the wrapper (in my controller) is like this below, right?
$api = new Api($username, $password);
$api->validateAddress($params); // but I can't call this line with this setup
Instead, this works:
$api = new ValidateAddress($username, $password);
$api->validateAddress($params);
Makes sense, but is this the good way of organising it?
What is the beautiful way of setting up api wrapper? By the way, maybe I am wrong with this approach completely. I'd be happy to hear what you think
php laravel api wrapper
php laravel api wrapper
asked Mar 22 at 18:04
sentysenty
3,969657139
3,969657139
Maybe something like a 3rd argument in theApi
constructor that defines what class you want to use (AddressValidator in this case) ?
– Scamtex
Mar 22 at 18:09
Do you mean in my Api class,public function __call($name, $arguments)
? And then$serviceClass = 'Service\' . ucfirst($name); $request = new $serviceClass(...$arguments);
– senty
Mar 22 at 18:16
add a comment |
Maybe something like a 3rd argument in theApi
constructor that defines what class you want to use (AddressValidator in this case) ?
– Scamtex
Mar 22 at 18:09
Do you mean in my Api class,public function __call($name, $arguments)
? And then$serviceClass = 'Service\' . ucfirst($name); $request = new $serviceClass(...$arguments);
– senty
Mar 22 at 18:16
Maybe something like a 3rd argument in the
Api
constructor that defines what class you want to use (AddressValidator in this case) ?– Scamtex
Mar 22 at 18:09
Maybe something like a 3rd argument in the
Api
constructor that defines what class you want to use (AddressValidator in this case) ?– Scamtex
Mar 22 at 18:09
Do you mean in my Api class,
public function __call($name, $arguments)
? And then $serviceClass = 'Service\' . ucfirst($name); $request = new $serviceClass(...$arguments);
– senty
Mar 22 at 18:16
Do you mean in my Api class,
public function __call($name, $arguments)
? And then $serviceClass = 'Service\' . ucfirst($name); $request = new $serviceClass(...$arguments);
– senty
Mar 22 at 18:16
add a comment |
2 Answers
2
active
oldest
votes
Instead of extending your API class, you could use a trait to organize your methods.
class Api
use ValidateAddressTrait;
...
Trait:
trait ValidateAddressTrait
public function validateAddress($data)
$response = $this->makeCall('validateAddress', $data);
dd($response);
Use:
$api = new Api($username, $password);
$api->validateAddress($params);
This isn't exactly the intent of a trait but I think it can give you the result you are looking for.
Thanks for the approach, makes sense - I actually want to know what's the best practice for api wrappers
– senty
Mar 22 at 18:40
add a comment |
Maybe something like
class Api
private $class;
.
.
public function __construct($username, $password, $class_name)
.
.
$this->class = new $class_name();
public function ApiCall($func, ...$arguments)
$this->class->$func($arguments);
I'm not sure if this makes things easier for you, but it works.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55305473%2fclass-organisation-of-api-wrappers%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Instead of extending your API class, you could use a trait to organize your methods.
class Api
use ValidateAddressTrait;
...
Trait:
trait ValidateAddressTrait
public function validateAddress($data)
$response = $this->makeCall('validateAddress', $data);
dd($response);
Use:
$api = new Api($username, $password);
$api->validateAddress($params);
This isn't exactly the intent of a trait but I think it can give you the result you are looking for.
Thanks for the approach, makes sense - I actually want to know what's the best practice for api wrappers
– senty
Mar 22 at 18:40
add a comment |
Instead of extending your API class, you could use a trait to organize your methods.
class Api
use ValidateAddressTrait;
...
Trait:
trait ValidateAddressTrait
public function validateAddress($data)
$response = $this->makeCall('validateAddress', $data);
dd($response);
Use:
$api = new Api($username, $password);
$api->validateAddress($params);
This isn't exactly the intent of a trait but I think it can give you the result you are looking for.
Thanks for the approach, makes sense - I actually want to know what's the best practice for api wrappers
– senty
Mar 22 at 18:40
add a comment |
Instead of extending your API class, you could use a trait to organize your methods.
class Api
use ValidateAddressTrait;
...
Trait:
trait ValidateAddressTrait
public function validateAddress($data)
$response = $this->makeCall('validateAddress', $data);
dd($response);
Use:
$api = new Api($username, $password);
$api->validateAddress($params);
This isn't exactly the intent of a trait but I think it can give you the result you are looking for.
Instead of extending your API class, you could use a trait to organize your methods.
class Api
use ValidateAddressTrait;
...
Trait:
trait ValidateAddressTrait
public function validateAddress($data)
$response = $this->makeCall('validateAddress', $data);
dd($response);
Use:
$api = new Api($username, $password);
$api->validateAddress($params);
This isn't exactly the intent of a trait but I think it can give you the result you are looking for.
answered Mar 22 at 18:29
Sean BerceSean Berce
913
913
Thanks for the approach, makes sense - I actually want to know what's the best practice for api wrappers
– senty
Mar 22 at 18:40
add a comment |
Thanks for the approach, makes sense - I actually want to know what's the best practice for api wrappers
– senty
Mar 22 at 18:40
Thanks for the approach, makes sense - I actually want to know what's the best practice for api wrappers
– senty
Mar 22 at 18:40
Thanks for the approach, makes sense - I actually want to know what's the best practice for api wrappers
– senty
Mar 22 at 18:40
add a comment |
Maybe something like
class Api
private $class;
.
.
public function __construct($username, $password, $class_name)
.
.
$this->class = new $class_name();
public function ApiCall($func, ...$arguments)
$this->class->$func($arguments);
I'm not sure if this makes things easier for you, but it works.
add a comment |
Maybe something like
class Api
private $class;
.
.
public function __construct($username, $password, $class_name)
.
.
$this->class = new $class_name();
public function ApiCall($func, ...$arguments)
$this->class->$func($arguments);
I'm not sure if this makes things easier for you, but it works.
add a comment |
Maybe something like
class Api
private $class;
.
.
public function __construct($username, $password, $class_name)
.
.
$this->class = new $class_name();
public function ApiCall($func, ...$arguments)
$this->class->$func($arguments);
I'm not sure if this makes things easier for you, but it works.
Maybe something like
class Api
private $class;
.
.
public function __construct($username, $password, $class_name)
.
.
$this->class = new $class_name();
public function ApiCall($func, ...$arguments)
$this->class->$func($arguments);
I'm not sure if this makes things easier for you, but it works.
answered Mar 22 at 21:34
ScamtexScamtex
1766
1766
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55305473%2fclass-organisation-of-api-wrappers%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Maybe something like a 3rd argument in the
Api
constructor that defines what class you want to use (AddressValidator in this case) ?– Scamtex
Mar 22 at 18:09
Do you mean in my Api class,
public function __call($name, $arguments)
? And then$serviceClass = 'Service\' . ucfirst($name); $request = new $serviceClass(...$arguments);
– senty
Mar 22 at 18:16