Guest User

Untitled

a guest
Mar 24th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. /**
  2. * A helper class for building REST API endpoints.
  3. */
  4. class Endpoint(
  5. val path: String,
  6. val query: String,
  7. val value: String) {
  8.  
  9. // Returns a list of objects containing a subset of object properties
  10. class Filter {
  11. companion object {
  12. fun someThing(value: String): Endpoint = Endpoint("filter.php", "a", value)
  13. fun someOtherThing(value: String): Endpoint = Endpoint("filter.php", "b", value)
  14. }
  15. }
  16.  
  17. // Returns a list of objects containing complete objects
  18. class Search {
  19. companion object {
  20. fun someThing(value: String): Endpoint = Endpoint("search.php", "a", value)
  21. fun someOtherThing(value: String): Endpoint = Endpoint("search.php", "b", value)
  22. }
  23. }
  24.  
  25. // Returns a list containing a single object
  26. class Lookup {
  27. companion object {
  28. fun getById(value: String): Endpoint = Endpoint("lookup.php", "a", value)
  29. fun getRandom(): Endpoint = Endpoint("random.php", "", "")
  30. }
  31. }
  32.  
  33. // Returns a list of strings
  34. class List {
  35. companion object {
  36. fun someCategory(): Endpoint = Endpoint("list.php", "a", "list")
  37. fun someOtherCategory(): Endpoint = Endpoint("list.php", "b", "list")
  38. }
  39. }
  40.  
  41. // There are some cases where we want to know if this is a filter
  42. fun isFilter(): Boolean {
  43. return path == "filter"
  44. }
  45.  
  46. // This is used with Retrofit @QueryMap
  47. fun options(): Map<String, String> {
  48. if (query.isEmpty() && value.isEmpty())
  49. return emptyMap()
  50.  
  51. val map = HashMap<String, String>()
  52. map.put(query, value)
  53. return map
  54. }
  55.  
  56. override fun toString(): String {
  57. return "Endpoint(path='$path', query='$query', value='$value')"
  58. }
  59.  
  60. }
Add Comment
Please, Sign In to add comment