|
| 1 | +package com.spotify.api.controller.controller; |
| 2 | + |
| 3 | +import com.spotify.api.controller.helper.SearchModel; |
| 4 | +import org.apache.http.HttpResponse; |
| 5 | +import org.apache.http.client.HttpClient; |
| 6 | +import org.apache.http.client.methods.HttpGet; |
| 7 | +import org.apache.http.impl.client.HttpClientBuilder; |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
| 10 | +import org.springframework.beans.factory.annotation.Value; |
| 11 | +import org.springframework.web.bind.annotation.*; |
| 12 | + |
| 13 | +import java.io.BufferedReader; |
| 14 | +import java.io.IOException; |
| 15 | +import java.io.InputStreamReader; |
| 16 | + |
| 17 | +import static org.apache.http.HttpHeaders.USER_AGENT; |
| 18 | + |
| 19 | +/** |
| 20 | + * Created by Fatih Totrakanli on 31/05/2017. |
| 21 | + */ |
| 22 | + |
| 23 | +@RestController |
| 24 | +@RequestMapping(value = "search") |
| 25 | +public class SearchController { |
| 26 | + |
| 27 | + private static Logger logger = LoggerFactory.getLogger(SearchController.class.getName()); |
| 28 | + |
| 29 | + @Value("${token}") |
| 30 | + private String token; |
| 31 | + |
| 32 | + @PostMapping(value = "/api") |
| 33 | + public StringBuffer searchQ(@RequestBody SearchModel search) throws IOException { |
| 34 | + |
| 35 | + String query = search.getQuery().equals("") ? "%2B" : search.getQuery().replaceAll("\\s+", "%2B"); |
| 36 | + logger.info(query + " search started..."); |
| 37 | + |
| 38 | + String url = "https://api.spotify.com/v1/search?q=" + query + "&type=track&limit=" + search.getLimit() + "&offset=" + search.getOffset(); |
| 39 | + |
| 40 | + HttpClient client = HttpClientBuilder.create().build(); |
| 41 | + HttpGet request = new HttpGet(url); |
| 42 | + |
| 43 | + request.addHeader("User-Agent", USER_AGENT); |
| 44 | + request.addHeader("Authorization", "Bearer " + token); |
| 45 | + request.addHeader("Accept", "application/json"); |
| 46 | + HttpResponse response = client.execute(request); |
| 47 | + |
| 48 | + logger.info("Response Code : " + response.getStatusLine().getStatusCode()); |
| 49 | + |
| 50 | + if (response.getStatusLine().getStatusCode() == 401) { |
| 51 | + String error = "The server did not respond properly. Please check spotify authorization token from application.yml or get new token from (https://developer.spotify.com/web-api/console/get-search-item/)."; |
| 52 | + logger.error(error); |
| 53 | + throw new RuntimeException(error); |
| 54 | + } else { |
| 55 | + if (response.getStatusLine().getStatusCode() != 200) |
| 56 | + throw new RuntimeException("Something went wrong."); |
| 57 | + } |
| 58 | + |
| 59 | + BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); |
| 60 | + |
| 61 | + StringBuffer result = new StringBuffer(); |
| 62 | + String line = ""; |
| 63 | + logger.info("Reading response..."); |
| 64 | + while ((line = rd.readLine()) != null) { |
| 65 | + result.append(line); |
| 66 | + } |
| 67 | + |
| 68 | + logger.info("Reading response completed successfully."); |
| 69 | + |
| 70 | + return result; |
| 71 | + } |
| 72 | +} |
0 commit comments