Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API - Basic API calls #24

Open
wants to merge 84 commits into
base: gsoc2014
Choose a base branch
from
Open

API - Basic API calls #24

wants to merge 84 commits into from

Conversation

sdaityari
Copy link
Owner

No description provided.

}

function api_module_status(){
$enabled = queryDB("SELECT * FROM %smodules WHERE dir_name = '%s' and status = %d",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

queryDB always returns array of arrays. Rows of columns. What is returning value of this function? It makes more sense to return true/false rather than multidimensional array.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do the following-

return $enabled[0]?true:false;

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is slightly better. $enabled[0] is the array which represents the first row though. If you want to find if there are rows then you need to check count($result) where $result is returning result from queryDB. OR you need to change queryDB to return only 1 row and also return count(*) and then you can always access $result[0]

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer the using PHP's "count" to count the results. I will push the updated code soon.

}

function log_request($log) {
queryDB("INSERT INTO %sapi_logs(ip_address, request_uri, http_method, token, response) VALUES('%s', '%s', '%s', '%s', '%s')",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do some variable guarding because if $log is null then you will get a PHP error that $log["ip_address"] is not accessible

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I purposely misspelled ip_address and noticed that an empty string is inserted in the database and no error is raised.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always think how you would possibly write a test case whenever you write a logic for your functions. if $log is null or empty you will raise php error when trying to get a property "ip_address"

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could provide a default value. function log_request($log = array()) {
That way, it would not raise any error but enter an empty string into the database.

* "access_level" => ADMIN_ACCESS_LEVEL,
* "query" => $query,
* "query_array" => $array,
* "one_row" => true
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this option : "one_row" what is it for ?

$array = array();
}

api_backbone(array(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so if query is "" then what api_backbone will execute here ?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, that case happens when I am making a PUT request without any parameters. No query is executed (as queryDB returns -1), but other things like logging takes place.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't it make sense to change code then to be (using the concept of return more often):

if (!$clause) {
   return;
}

$query = "UPDATE %scourses ".$clause. "WHERE course_id = %d ". $clause;
$array = array(TABLE_PREFIX, $course_id);

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh actually never mind since query_id_existence will return nothing and user will see 404. Nevermind

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants