Page 1 of 1
[Fixed] Set credentials once on startup
Posted: 16 Nov 2011, 20:56
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?

Re: [Suggestion] Set credentials once on startup
Posted: 17 Nov 2011, 19:07
by gouxim
m4rcel wrote:So what about finally fixing this todo?

Wow, someone actually reads my TODOs in the code
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?
Re: [Suggestion] Set credentials once on startup
Posted: 18 Nov 2011, 19:11
by gouxim
Hey,
I implemented that today since it makes us save time on our projects too
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');

Re: [Suggestion] Set credentials once on startup
Posted: 29 Nov 2011, 19:17
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 ^^)
Re: [Suggestion] Set credentials once on startup
Posted: 29 Nov 2011, 19:50
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).
Re: [Suggestion] Set credentials once on startup
Posted: 01 Dec 2011, 18:01
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