0

I found this very interesting project on github: https://github.com/brannondorsey/apibuilder

After starting using it, I noticed that the request limitation won't work properly. The value for the requests in the MySQL database counts to "1000" (or the limit set in $hits_per_day), but the API output is still some data from the database.

The expected error message is simply attached to the API response.

But I would like to have an API that doesn't allow any output and only the error message. Below you can find the concerning file.

public function get_json_from_assoc(&$get_array){

    $json_obj = new StdClass();
    $pretty_print = $this->pretty_print;

    if(!$this->find_config_errors()){

        if(isset($get_array['pretty_print'])){
            if(strtolower($get_array['pretty_print']) == "true") $pretty_print = true;
            if(strtolower($get_array['pretty_print']) == "false") $pretty_print = false;  
        }

        //if API is public or if API is private and a correct private key was provided
        if(!$this->private ||
            $this->private &&
            isset($get_array['private_key']) &&
            $this->private_key == $get_array['private_key']){

            $query = $this->form_query($get_array);
            if($this->check_API_key()
                || !$this->API_key_required){
                //if search was included as a parameter in the http request but it isn't allowed in the api's config...
                if(isset($get_array['search']) &&
                   !$this->search_allowed){
                    $json_obj->error = "search parameter not enabled for this API";
                }
                else if(isset($get_array['exclude']) &&
                    !$this->exclude_allowed){
                    $json_obj->error = "exclude parameter not enabled for this API";
                }else{
                    if($results_array = Database::get_all_results($query)){

                        if(is_array($results_array)){
                            // deletes key => value pairs if the value is empty. Only works if array is nested: 
                            // http://stackoverflow.com/questions/5750407/php-array-removing-empty-values   
                            $results_array = array_filter(array_map('array_filter', $results_array));

                            foreach($results_array as $result_array){
                                foreach($result_array as $key => $value){
                                    if($key == "COUNT(*)"){
                                        $count = $value;
                                        break;
                                    }
                                }
                            }
                            if(!isset($count)) $json_obj->data = $results_array;
                            else $json_obj->count = $count; 
                            //COME BACK need to make count only parameter work
                        }
                    }else $json_obj->error = "no results found";
                }

                //only attempt to increment the api hit count if this method is called from a PUBLIC API request
                if($this->API_key_required){
                    $query = "SELECT " . $this->API_hit_date_column_name . " FROM " . Database::$users_table . " WHERE " . $this->API_key_column_name . " = '" . $this->API_key . "' LIMIT 1";
                    $result = Database::get_all_results($query);
                    //increments the hit count and/or hit date OR sets the error message if the key has reached its hit limit for the day
                    if($this->update_API_hits($this->API_key, $result[0][$this->API_hit_date_column_name]) === false){
                     $json_obj->error = "API hit limit reached";
                    }
                 }
            }else $json_obj->error = "API key is invalid or was not provided";
            //if there was a search and it returned no results
            if($this->search != "" &&
                !$this->search_has_been_repeated &&
                isset($json_obj->error) &&
                strstr($json_obj->error, $this->no_results_message) == true){
                    $this->search_in_boolean_mode = true; //set search in boolean mode to true
                    $this->search_has_been_repeated = true; //note that the search will now have been repeated
                    //$this->JSON_string = $this->get_json_from_assoc($get_array, $object_parent_name); //recurse the function (thus re-searching)
                    return $this->get_json_from_assoc($get_array); //recurse the function (thus re-searching)
            }
        }else{ //API is private but private_key is invalid or was not provided
            $json_obj->error = "this API is private and the private key was invalid or not provided";
        }
    }else{ //config errors were present
        $pretty_print = true; //always output errors in pretty print for readability
        $json_obj->config_error = $this->config_errors;
    }
    return ($pretty_print && version_compare(PHP_VERSION, '5.4.0') >= 0) ? json_encode($json_obj, JSON_PRETTY_PRINT) : json_encode($json_obj);
}
3
  • Please strip the code down to the relevant parts. Commented Apr 11, 2017 at 21:05
  • @Marvin just shortened it, thanks Commented Apr 11, 2017 at 21:09
  • Francis, generally you create a new issue at Github and describe the problem there. Commented Apr 15, 2017 at 8:36

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.