|
| 1 | +## Want to add your own request? and serialize them into objects? |
| 2 | + |
| 3 | +1. Simply create a new class with any name you want |
| 4 | +2. Make `AbstractRequest` its super class |
| 5 | + |
| 6 | +```java |
| 7 | +public class MyClass extends AbstractRequest { |
| 8 | +} |
| 9 | +``` |
| 10 | + |
| 11 | +3. Annotated it with |
| 12 | + |
| 13 | +```Java |
| 14 | +@SpotifyRequest("<request>") |
| 15 | +``` |
| 16 | + |
| 17 | +replacing `<request>` with the request you are wanting to call E.g. It will be evaluated |
| 18 | +as `"https://api.spotify.com/v1/<request>"` |
| 19 | + |
| 20 | +So for `<request> = albums`, |
| 21 | + |
| 22 | +```java |
| 23 | + |
| 24 | +@SpotifyRequest("albums") |
| 25 | +public class MyClass extends AbstractRequest { |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +the url will be `"https://api.spotify.com/v1/albums"` |
| 30 | + |
| 31 | +4. If there are variables within the URL like `"https://api.spotify.com/v1/albums/{id}"` |
| 32 | + |
| 33 | +Add `@SpotifySubRequest` to the required field |
| 34 | + |
| 35 | +```java |
| 36 | +@SpotifyRequest("albums") |
| 37 | +public class MyClass extends AbstractRequest { |
| 38 | + |
| 39 | + @SpotifySubRequest |
| 40 | + private final String id; |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +(For SubRequest annotated fields, they will be evaluated in order of declaration in the class, therefore **order |
| 45 | +matters**) |
| 46 | + |
| 47 | +5. For parameters within the url like `https://api.spotify.com/v1/albums/{id}?parameter=value` |
| 48 | + |
| 49 | +Add `@SpotifyRequestField` to the required fields |
| 50 | + |
| 51 | +```java |
| 52 | +@SpotifyRequest("albums") |
| 53 | +public class MyClass extends AbstractRequest { |
| 54 | + |
| 55 | + @SpotifySubRequest |
| 56 | + private final String id; |
| 57 | + |
| 58 | + @SpotifyRequestField |
| 59 | + private Market market; |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +Where `parameter = market`, the field name. The `value` is the value within the specific field |
| 64 | + |
| 65 | +So for |
| 66 | + |
| 67 | +```java |
| 68 | +@SpotifyRequest("albums") |
| 69 | +public class MyClass extends AbstractRequest { |
| 70 | + |
| 71 | + @SpotifySubRequest |
| 72 | + private final String id = "382ObEPsp2rxGrnsizN5TX"; |
| 73 | + |
| 74 | + @SpotifyRequestField |
| 75 | + private Market market = Market.ES; |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +the url will be evaluated as `https://api.spotify.com/v1/albums/382ObEPsp2rxGrnsizN5TX?market=ES` |
| 80 | + |
| 81 | +Fields with types other than String will call `toString` |
| 82 | + |
| 83 | +Fields with the type of array, the array contents will be put into a comma seperated list of their respected string |
| 84 | +contents. |
| 85 | + |
| 86 | +(RequestField will again be evaluated in order of declaration within the class however order doesn't matter with url |
| 87 | +parameters) |
| 88 | + |
| 89 | +and finally to execute the request, after creating a SpotifyClient instance, |
| 90 | + |
| 91 | +```java |
| 92 | +MyClass myClass=new MyClass(); |
| 93 | + SpotifyRepsonse sr=spotifyClient.executeRequest(myClass); |
| 94 | + JSONObject json=sr.getJsonObject(); |
| 95 | +``` |
0 commit comments