/**
  * Respond to the client with the given response object.
  *
  * @param Response $response
  *
  * @return string
  */
 public function respond(Response $response)
 {
     $content_type = 'application/json';
     $this->send_header('Content-Type', $content_type . '; charset=' . get_option('blog_charset'));
     $this->send_headers($response->get_headers());
     $this->set_status($response->get_status());
     $result = $this->response_to_data($response);
     $result = json_encode($result);
     $json_error_message = $this->get_json_last_error();
     if ($json_error_message) {
         $json_error_obj = new Response(array('success' => false, 'error' => array('code' => 500, 'message' => $json_error_message), 500));
         $result = $this->response_to_data($json_error_obj);
         $result = json_encode($result);
     }
     return $result;
 }
 /**
  * Convert a response to an array.
  *
  * @param Response $response
  *
  * @return array
  */
 protected function response_to_data(Response $response)
 {
     return (array) $this->prepare_response($response->get_data());
 }
 /**
  * Retrieve the response object for when authentication is missing.
  *
  * @since 1.0
  *
  * @param Authenticatable $endpoint
  *
  * @return Response
  */
 protected function generate_auth_missing(Authenticatable $endpoint)
 {
     $response = new Response(array('success' => false, 'error' => array('code' => $endpoint->get_auth_error_code(), 'message' => $endpoint->get_auth_error_message())), 401);
     switch ($endpoint->get_auth_mode()) {
         case Authenticatable::MODE_VALID_ACTIVATION:
             $realm = __("An active license key is required to access this resource, passed as the username, and the activation record ID as the password.", Plugin::SLUG);
             break;
         case Authenticatable::MODE_ACTIVE:
             $realm = __("An active license key is required to access this resource, passed as the username. Leave password blank.", Plugin::SLUG);
             break;
         case Authenticatable::MODE_EXISTS:
         default:
             $realm = __("A license key is required to access this resource, passed as the username. Leave password blank.", Plugin::SLUG);
             break;
     }
     $response->header('WWW-Authenticate', "Basic realm=\"{$realm}\"");
     return $response;
 }