| @@ 10-74 (lines=65) @@ | ||
| 7 | use Illuminate\Http\Request; |
|
| 8 | use App\Http\Controllers\Controller; |
|
| 9 | ||
| 10 | class NytController extends Controller |
|
| 11 | { |
|
| 12 | ||
| 13 | /** |
|
| 14 | * Instance of Guzzle Client |
|
| 15 | * @var object |
|
| 16 | */ |
|
| 17 | protected $client; |
|
| 18 | ||
| 19 | /** |
|
| 20 | * BaseUrl |
|
| 21 | * @var string |
|
| 22 | */ |
|
| 23 | protected $baseUrl; |
|
| 24 | ||
| 25 | /** |
|
| 26 | * Initialize the Controller with necessary arguments |
|
| 27 | */ |
|
| 28 | public function __construct() |
|
| 29 | { |
|
| 30 | $this->baseUrl = 'http://api.nytimes.com/svc'; |
|
| 31 | $this->client = new Client(['base_uri' => $this->baseUrl]); |
|
| 32 | ||
| 33 | $relativeUrl = '/books/v3/lists/overview.json?api-key=' . env('NYT_BOOKS_API_KEY'); |
|
| 34 | $this->setGetResponse($relativeUrl); |
|
| 35 | } |
|
| 36 | ||
| 37 | /** |
|
| 38 | * Get the response from New York times API |
|
| 39 | * @param string $relativeUrl |
|
| 40 | */ |
|
| 41 | private function setGetResponse($relativeUrl) |
|
| 42 | { |
|
| 43 | $this->response = $this->client->get($this->baseUrl . $relativeUrl, []); |
|
| 44 | } |
|
| 45 | ||
| 46 | /** |
|
| 47 | * Get the whole response from a get operation |
|
| 48 | * @return array |
|
| 49 | */ |
|
| 50 | private function getResponse() |
|
| 51 | { |
|
| 52 | return json_decode($this->response->getBody(), true); |
|
| 53 | } |
|
| 54 | ||
| 55 | /** |
|
| 56 | * Get the data response from a get operation |
|
| 57 | * @return array |
|
| 58 | */ |
|
| 59 | private function getData() |
|
| 60 | { |
|
| 61 | return $this->getResponse()['results']['lists'][0]['books']; |
|
| 62 | } |
|
| 63 | ||
| 64 | /** |
|
| 65 | * Return all the data to the New York times API dashboard |
|
| 66 | * @return array |
|
| 67 | */ |
|
| 68 | public function getPage() |
|
| 69 | { |
|
| 70 | $data = $this->getData(); |
|
| 71 | ||
| 72 | return view('api.nyt')->withData($data); |
|
| 73 | } |
|
| 74 | } |
|
| 75 | ||
| @@ 11-78 (lines=68) @@ | ||
| 8 | use App\Http\Requests; |
|
| 9 | use App\Http\Controllers\Controller; |
|
| 10 | ||
| 11 | class YahooController extends Controller |
|
| 12 | { |
|
| 13 | /** |
|
| 14 | * Instance of Guzzle Client |
|
| 15 | * @var object |
|
| 16 | */ |
|
| 17 | protected $client; |
|
| 18 | ||
| 19 | /** |
|
| 20 | * BaseUrl |
|
| 21 | * @var string |
|
| 22 | */ |
|
| 23 | protected $baseUrl; |
|
| 24 | ||
| 25 | /** |
|
| 26 | * Initialize the Controller with necessary arguments |
|
| 27 | */ |
|
| 28 | public function __construct() |
|
| 29 | { |
|
| 30 | $this->baseUrl = 'https://query.yahooapis.com/v1/public/yql'; |
|
| 31 | $this->client = new Client(['base_uri' => $this->baseUrl]); |
|
| 32 | ||
| 33 | $query = "SELECT * FROM weather.forecast WHERE (location = 10007)"; |
|
| 34 | ||
| 35 | $relativeUrl = '?q=' . $query . '&format=json'; |
|
| 36 | $this->setGetResponse($relativeUrl); |
|
| 37 | } |
|
| 38 | ||
| 39 | /** |
|
| 40 | * Get the response from Yahoo API |
|
| 41 | * @param string $relativeUrl |
|
| 42 | */ |
|
| 43 | private function setGetResponse($relativeUrl) |
|
| 44 | { |
|
| 45 | $this->response = $this->client->get($this->baseUrl . $relativeUrl, []); |
|
| 46 | } |
|
| 47 | ||
| 48 | /** |
|
| 49 | * Get the whole response from a get operation |
|
| 50 | * @return array |
|
| 51 | */ |
|
| 52 | private function getResponse() |
|
| 53 | { |
|
| 54 | return json_decode($this->response->getBody(), true); |
|
| 55 | } |
|
| 56 | ||
| 57 | /** |
|
| 58 | * Get the data response from a get operation |
|
| 59 | * @return array |
|
| 60 | */ |
|
| 61 | private function getData() |
|
| 62 | { |
|
| 63 | return $this->getResponse()['query']['results']['channel']; |
|
| 64 | } |
|
| 65 | ||
| 66 | ||
| 67 | /** |
|
| 68 | * Return all data to the Yahoo API dashboard |
|
| 69 | * @return mixed |
|
| 70 | */ |
|
| 71 | public function getPage() |
|
| 72 | { |
|
| 73 | $data = $this->getData(); |
|
| 74 | ||
| 75 | return view('api.yahoo')->withData($data); |
|
| 76 | } |
|
| 77 | ||
| 78 | } |
|
| 79 | ||