/**
  * Get the details for a username.
  *
  * @param string $apiVersion The API version of the request
  * @param string $username   The MyAnimeList username of the user.
  *
  * @return View
  */
 public function getProfileAction($apiVersion, $username)
 {
     // http://myanimelist.net/profile/#{username}
     $downloader = $this->get('atarashii_api.communicator');
     try {
         $profilecontent = $downloader->fetch('/profile/' . $username);
     } catch (Exception\CurlException $e) {
         return $this->view(array('error' => 'network-error'), 500);
     } catch (Exception\ClientErrorResponseException $e) {
         $profilecontent = $e->getResponse();
     }
     $response = new Response();
     $serializationContext = SerializationContext::create();
     $serializationContext->setVersion($apiVersion);
     //For compatibility, API 1.0 explicitly passes null parameters.
     if ($apiVersion == '1.0') {
         $serializationContext->setSerializeNull(true);
     }
     $response->setPublic();
     $response->setMaxAge(900);
     //15 minutes
     $response->headers->addCacheControlDirective('must-revalidate', true);
     $response->setEtag('profile/' . $username);
     //Also, set "expires" header for caches that don't understand Cache-Control
     $date = new \DateTime();
     $date->modify('+900 seconds');
     //15 minutes
     $response->setExpires($date);
     if (strpos($profilecontent, 'Failed to find') !== false || strpos($profilecontent, 'This page doesn\'t exist') !== false) {
         $view = $this->view(array('error' => 'not-found'));
         $view->setResponse($response);
         $view->setStatusCode(404);
         return $view;
     } else {
         $userprofile = User::parse($profilecontent, $apiVersion);
         $view = $this->view($userprofile);
         $view->setSerializationContext($serializationContext);
         $view->setResponse($response);
         $view->setStatusCode(200);
         return $view;
     }
 }
Example #2
0
 protected function setUp()
 {
     $profileContents = file_get_contents(__DIR__ . '/../InputSamples/profile-motokochan.html');
     $profile2Contents = file_get_contents(__DIR__ . '/../InputSamples/profile-xinil.html');
     $this->profile = User::parse($profileContents, '2.1');
     $this->profile2 = User::parse($profile2Contents, '2.1');
 }