Welcome Guest, Not a member yet? Register   Sign In
stuck with models
#1

[eluser]Mutsop[/eluser]
Hi,

I don't know how many of you know Eve-Online (a game), so I will explain you as clearly as possible Smile

So I'm using a php library of someone which calls upon xml files (receive information of your ingame character using the API of Eve-Online) and translates them. I was able to get it to work in the plugin folder... JOY Big Grin

But now, for the long term I though of using CI_models to actually simplify the way I could retrieve information upon my character ingame.

Now, The API of the game has two variations: A LIMITED key and a FULL key. The limited key will provide an error when trying to access information only the FULL key can access.

So how would I code that into my models? fyi there are around 30 functions I should do (45% limited- 45%full -10%nokey needed). The users should register their keys and id when they sign up.

Now this is the model code (simplified) for the information access. The information about a character (limited key) can be seen by anyone. So i thought of using (doctrine -> FindByUsername and call upon the apikey and id)

Code:
<?php
class Api_call extends Model {
    
    var $api = "";
    function Api_call()
    {
        parent::Model();
    }

//Account

//Character List (on account)    /account/Characters.xml.aspx     Limited     Short
    function characters() {
        
    }
//Account Status    /account/AccountStatus.xml.aspx     Full     Short
    function accountStatus() {
        
    }
#2

[eluser]kenny.katzgrau[/eluser]
CodeIgniter is cool in that there often isn't a declared way of how you should do something.

The best way (IMO) is to handle the issue with exceptions. The API will undoubtedly hand you back an error if you try and access information requiring a full key with only a short key. Just throw a specific exception and handle it as needed. Example:

Code:
<?php

class Api_call extends Model {
    
    var $api = "";
    function Api_call()
    {
        parent::Model();
    }

    //Character List (on account)    /account/Characters.xml.aspx     Limited     Short
    function characters() {
        // Make the call.
        if( /* API returned error */ ) {
            throw new APIPermissionsException("Error Message");
        }
        // If we got this far, parse the XML and return objects
    }

    //Account Status    /account/AccountStatus.xml.aspx     Full     Short
    function accountStatus() {
        
    }

}

class APIPermissionsException extends Exception {}

Then your code might look like:

Code:
...
try {
    $characters = $this->api_call->characters();
} catch(APIPermissionsException $ex) {
    // Okay, now what do we do?
}
...
#3

[eluser]Mutsop[/eluser]
I was thinking of using that... But problem is, I have about 30 function I have to add, where around 13 should have the same exception... Should I copy this error check for each of these 13 functions or is there maybe a easier and professionel way of doing so?
#4

[eluser]kenny.katzgrau[/eluser]
Throwing exceptions is definitely the professional way of going about it. While that does mean you'll have to throw an exception wherever that API error occurs (as you should), you don't have to check for the exception individually:

Code:
...
try {
    $account_status = $this->api_call->account_status();
    $characters     = $this->api_call->characters();
    /* other calls */
} catch(APIPermissionsException $ex) {
    // Okay, now what do we do?
}
...

Instead of throwing an exception, you can take the classic PHP route of simply returning a null/false value — although you do lose a lot of information that way.

For example, if a call to characters() fails, you might just want to return an empty array. While that's your discretion, you won't know programmatically whether there were simply no characters returned, or the API call failed.
#5

[eluser]kenny.katzgrau[/eluser]
One other thing you might want to think about is perhaps making a call that requires a full key first. Then, based on the result of that, you will know whether you have full information or short information.

Then your API_call model could have a function like:

Code:
public function getAccountData($key) {
  /* Make a call requiring full access */

  if( /* We have a full key */ )
      return $this->getFullAccountData();
  else
      return $this->getLimitedAccountData();
}

public function getLimitedAccountData($key) {
  /* Build account data object and return it */
  return array (
    'account_status' => $this->accountStatus($key)
    /* Other data requiring a limited key */
  );
}


public function getFullAccountData($key) {
  /* Build account data object and return it */
  return array (
    'account_status' => $this->accountStatus($key)
    'characters'     => $this->characters($key)
    /* Other data requiring a full key */
  );
}

public function accountStatus($key) { ... }

public function characters($key) { ... }

I left out whether to use exceptions or not here, but now determining whether you have a full account is re-factored to your model, and not a controller or library.
#6

[eluser]Mutsop[/eluser]
Thanks alot Kenny Smile

Seriously this helps me really good!




Theme © iAndrew 2016 - Forum software by © MyBB