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;








1















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










share|improve this question






















  • 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


















1















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










share|improve this question






















  • 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














1












1








1








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










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 22 at 18:04









sentysenty

3,969657139




3,969657139












  • 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


















  • 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

















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













2 Answers
2






active

oldest

votes


















1














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.






share|improve this answer























  • 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


















1














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.






share|improve this answer























    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
    );



    );













    draft saved

    draft discarded


















    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









    1














    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.






    share|improve this answer























    • 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















    1














    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.






    share|improve this answer























    • 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













    1












    1








    1







    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.






    share|improve this answer













    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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

















    • 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













    1














    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.






    share|improve this answer



























      1














      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.






      share|improve this answer

























        1












        1








        1







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 22 at 21:34









        ScamtexScamtex

        1766




        1766



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript