[Fixed] Set credentials once on startup

Maniaplanet public API, ManiaConnect system and the open source PHP SDK.

Moderator: NADEO

Post Reply
User avatar
m4rcel
Posts: 650
Joined: 15 Jun 2010, 11:12
Contact:

[Fixed] Set credentials once on startup

Post by m4rcel »

As I continue working on my Tetris Manialink, I came across another problem.

I use a config file to hold important data like usernames and passwords. On script startup, I read this file and initialize all classes needed for the project (e.g. create the connection to the Database with username/password from the config) to run. (So I have all sensitive data in a centralized file, and not scattered through the complete project.)

I now would like to see any kind of possibility to "announce" the credentials of the MPWS to the Maniaplanet classes (i.e. to the HTTPClient class), so that I instanciate any Maniaplanet class without worrying about the credentials anymore. (My first idea would be a static method HTTPClient::setCredentials($username, $password), which saves the username and password, and using them in the constructor if no other values are given.)


ADD:
As I looked through the files, I found this comment:
HTTPClient::__construct() wrote:// TODO Try to load the credentials from a ManiaLib config class for easy inclusion in ManiaLib projects
So what about finally fixing this todo? :mrgreen:
ImageImage
Image
User avatar
gouxim
Nadeo
Nadeo
Posts: 1186
Joined: 14 Jun 2010, 17:20

Re: [Suggestion] Set credentials once on startup

Post by gouxim »

m4rcel wrote:So what about finally fixing this todo? :mrgreen:
Wow, someone actually reads my TODOs in the code :shock:

Anyways, I can implement that todo pretty fast. Here's how it'll work:

Somewhere (like ManiaLib\WebServices\Config or something like that) you'll have a class that extends ManiaLib\Utils\Singleton. So, provided you import both the ManiaLib\Utils package and the ManiaLib\WebServices (or whatever this name will be) package, you should be able to write in your config file something like:

Code: Select all

$config = \ManiaLib\WebServices\Config::getInstance();
$config->username = 'xxxxxx';
$config->password = 'xxxxxx';
And then the credentials will be automatically loaded from that class when you instanciate one of the WS class.

But in the end, wouldn't it be more convenient/simple for you to just define two constants in your config file and use them in the constructors of the WS classes?
Please do not PM for support. Instead, create a thread so that everyone can contribute or benefit from the answer! 8-)
User avatar
gouxim
Nadeo
Nadeo
Posts: 1186
Joined: 14 Jun 2010, 17:20

Re: [Suggestion] Set credentials once on startup

Post by gouxim »

Hey,

I implemented that today since it makes us save time on our projects too :thumbsup:
For now it's only available in the trunks of the projects, but it's fairly easy to import what you need.

First you need the latest version of Maniaplanet\WebServices\HTTPClient. You can download it here (click on "view raw file"):
http://code.google.com/p/maniaplanet-ws ... Client.php

Then you need ManiaLib\WebServices\Config - as usual with our unified conventions between projects, you need to put the file in .../libraries/ManiaLib/WebServices/Config.php (case is important!):
http://code.google.com/p/manialib/sourc ... Config.php

And this config file depends on ManiaLib\Utils\Singleton:
http://code.google.com/p/manialib/sourc ... gleton.php

Once you have those, you only need to configure your credentials once by doing:

Code: Select all

$config = \ManiaLib\WebServices\Config::getInstance();
$config->username = 'your_api_username';
$config->password = 'your_api_password';
And that's it, you can then do:

Code: Select all

$players = new \Maniaplanet\WebServices\Players();
$players->get('gouxim');
:pop:
Please do not PM for support. Instead, create a thread so that everyone can contribute or benefit from the answer! 8-)
User avatar
m4rcel
Posts: 650
Joined: 15 Jun 2010, 11:12
Contact:

Re: [Suggestion] Set credentials once on startup

Post by m4rcel »

Thanks for the update so far.

I personally thought about a solution without ManiaLib (as I build my own little framework, where ManiaLib has not been used so far). Anyway, this update will help me with my problem of initialisation ^^

gouxim wrote:But in the end, wouldn't it be more convenient/simple for you to just define two constants in your config file and use them in the constructors of the WS classes?
My framework uses an XML file, from which the components like the DatabaseConnection gets initialised. (And I personally don't really like global constants ^^)
ImageImage
Image
User avatar
gouxim
Nadeo
Nadeo
Posts: 1186
Joined: 14 Jun 2010, 17:20

Re: [Suggestion] Set credentials once on startup

Post by gouxim »

I see. Even if you're building you're own framework, I guess you can still use pieces of ManiaLib to help you save some time (just like you would use the WS SDK, ManiaLib is a set of loosely coupled tools to help you deal with standard stuff).
Please do not PM for support. Instead, create a thread so that everyone can contribute or benefit from the answer! 8-)
User avatar
gouxim
Nadeo
Nadeo
Posts: 1186
Joined: 14 Jun 2010, 17:20

Re: [Suggestion] Set credentials once on startup

Post by gouxim »

Oh I BTW I just added the same feature for loading the "manialink" in Maniaplanet\WebServices\ManiaHome, but that time the manialink is loaded from ManiaLib\Application\Config:

http://code.google.com/p/maniaplanet-ws ... etail?r=83
Please do not PM for support. Instead, create a thread so that everyone can contribute or benefit from the answer! 8-)
Post Reply

Return to “Maniaplanet Web Services”

Who is online

Users browsing this forum: No registered users and 1 guest