Bimba.git

ref: a9046e4c5bd51282cf4b666e3bb8620ffcf637f5

app/src/main/java/ml/adamsprogs/bimba/api/Responses.kt


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package ml.adamsprogs.bimba.api

import xyz.apiote.fruchtfleisch.Reader
import java.io.InputStream

interface DeparturesResponse {
	companion object {
		fun unmarshal(stream: InputStream): DeparturesResponse {
			val reader = Reader(stream)
			return when (reader.readUInt().toULong()) {
				0UL -> {
					ErrorResponse.unmarshal(stream)
				}
				1UL -> {
					DeparturesSuccess.unmarshal(stream)
				}
				else -> {
					TODO("[api-freeze] throw unknown tag")
				}
			}
		}
	}
}

data class DeparturesSuccess(
	val alerts: List<Alert>,
	val departures: List<Departure>,
	val stop: Stop
) : DeparturesResponse {
	companion object {
		fun unmarshal(stream: InputStream): DeparturesSuccess {
			val alerts = mutableListOf<Alert>()
			val departures = mutableListOf<Departure>()

			val reader = Reader(stream)
			val alertsNum = reader.readUInt().toULong()
			for (i in 0UL until alertsNum) {
				val alert = Alert.unmarshal(stream)
				alerts.add(alert)
			}
			val departuresNum = reader.readUInt().toULong()
			for (i in 0UL until departuresNum) {
				val departure = Departure.unmarshal(stream)
				departures.add(departure)
			}

			return DeparturesSuccess(alerts, departures, Stop.unmarshal(stream))
		}
	}
}

interface ItemsResponse {
	companion object {
		fun unmarshal(stream: InputStream): ItemsResponse {
			val reader = Reader(stream)
			return when (reader.readUInt().toULong()) {
				0UL -> {
					ErrorResponse.unmarshal(stream)
				}
				1UL -> {
					ItemsSuccess.unmarshal(stream)
				}
				else -> {
					TODO("[api-freeze] throw unknown tag")
				}
			}
		}
	}
}

data class ItemsSuccess(val items: List<Item>) : ItemsResponse {
	companion object {
		fun unmarshal(stream: InputStream): ItemsSuccess {
			val items = mutableListOf<Item>()
			val reader = Reader(stream)
			val itemsNum = reader.readUInt().toULong()
			for (i in 0UL until itemsNum) {
				when (reader.readUInt().toULong()) {
					0UL -> {
						items.add(Stop.unmarshal(stream))
					}
					1UL -> {
						items.add(Line.unmarshal(stream))
					}
					else -> {
						TODO("[api-freeze] throw unknown tag")
					}
				}
			}
			return ItemsSuccess(items)
		}
	}
}

interface FeedsResponse {
	companion object {
		fun unmarshal(stream: InputStream): FeedsResponse {
			val reader = Reader(stream)
			return when (reader.readUInt().toULong()) {
				0UL -> {
					ErrorResponse.unmarshal(stream)
				}
				1UL -> {
					FeedsSuccess.unmarshal(stream)
				}
				else -> {
					TODO("[api-freeze] throw unknown tag")
				}
			}
		}
	}
}

data class FeedsSuccess(
	val adminContact: String,
	val rateLimited: Boolean,
	val private: Boolean,
	val feeds: List<FeedInfo>
) : FeedsResponse {
	companion object {
		fun unmarshal(stream: InputStream): FeedsSuccess {
			val feeds = mutableListOf<FeedInfo>()
			val reader = Reader(stream)
			val adminContact = reader.readString()
			val rateLimited = reader.readBoolean()
			val private = reader.readBoolean()
			val itemsNum = reader.readUInt().toULong()
			for (i in 0UL until itemsNum) {
				feeds.add(FeedInfo.unmarshal(stream))
			}
			return FeedsSuccess(adminContact, rateLimited, private, feeds)
		}
	}
}

interface LocatablesResponse {
	companion object {
		fun unmarshal(stream: InputStream): LocatablesResponse {
			val reader = Reader(stream)
			return when (reader.readUInt().toULong()) {
				0UL -> {
					ErrorResponse.unmarshal(stream)
				}
				1UL -> {
					LocatablesSuccess.unmarshal(stream)
				}
				else -> {
					TODO("[api-freeze] throw unknown tag")
				}
			}
		}
	}
}

data class LocatablesSuccess(val locatables: List<Locatable>) : LocatablesResponse {
	companion object {
		fun unmarshal(stream: InputStream): LocatablesSuccess {
			val locatables = mutableListOf<Locatable>()
			val reader = Reader(stream)
			val itemsNum = reader.readUInt().toULong()
			for (i in 0UL until itemsNum) {
				when (reader.readUInt().toULong()) {
					0UL -> {
						locatables.add(Stop.unmarshal(stream))
					}
					1UL -> {
						locatables.add(Vehicle.unmarshal(stream))
					}
					else -> {
						TODO("[api-freeze] throw unknown tag")
					}
				}
			}
			return LocatablesSuccess(locatables)
		}
	}
}

data class ErrorResponse(val field: String, val message: String) : ItemsResponse,
	DeparturesResponse, FeedsResponse, LocatablesResponse {
	companion object {
		fun unmarshal(stream: InputStream): ErrorResponse {
			val reader = Reader(stream)
			return ErrorResponse(reader.readString(), reader.readString())
		}
	}
}