22
33package com.x_twitter_scraper.api.models.radar
44
5+ import com.fasterxml.jackson.annotation.JsonCreator
6+ import com.x_twitter_scraper.api.core.Enum
7+ import com.x_twitter_scraper.api.core.JsonField
58import com.x_twitter_scraper.api.core.Params
69import com.x_twitter_scraper.api.core.http.Headers
710import com.x_twitter_scraper.api.core.http.QueryParams
11+ import com.x_twitter_scraper.api.errors.XTwitterScraperInvalidDataException
812import java.util.Objects
913import java.util.Optional
1014import kotlin.jvm.optionals.getOrNull
@@ -16,7 +20,7 @@ private constructor(
1620 private val count: Long? ,
1721 private val hours: Long? ,
1822 private val region: String? ,
19- private val source: String ? ,
23+ private val source: Source ? ,
2024 private val additionalHeaders: Headers ,
2125 private val additionalQueryParams: QueryParams ,
2226) : Params {
@@ -37,7 +41,7 @@ private constructor(
3741 * Source filter. One of: github, google_trends, hacker_news, polymarket, reddit, trustmrr,
3842 * wikipedia
3943 */
40- fun source (): Optional <String > = Optional .ofNullable(source)
44+ fun source (): Optional <Source > = Optional .ofNullable(source)
4145
4246 /* * Additional headers to send with the request. */
4347 fun _additionalHeaders (): Headers = additionalHeaders
@@ -65,7 +69,7 @@ private constructor(
6569 private var count: Long? = null
6670 private var hours: Long? = null
6771 private var region: String? = null
68- private var source: String ? = null
72+ private var source: Source ? = null
6973 private var additionalHeaders: Headers .Builder = Headers .builder()
7074 private var additionalQueryParams: QueryParams .Builder = QueryParams .builder()
7175
@@ -124,10 +128,10 @@ private constructor(
124128 * Source filter. One of: github, google_trends, hacker_news, polymarket, reddit, trustmrr,
125129 * wikipedia
126130 */
127- fun source (source : String ? ) = apply { this .source = source }
131+ fun source (source : Source ? ) = apply { this .source = source }
128132
129133 /* * Alias for calling [Builder.source] with `source.orElse(null)`. */
130- fun source (source : Optional <String >) = source(source.getOrNull())
134+ fun source (source : Optional <Source >) = source(source.getOrNull())
131135
132136 fun additionalHeaders (additionalHeaders : Headers ) = apply {
133137 this .additionalHeaders.clear()
@@ -253,11 +257,172 @@ private constructor(
253257 count?.let { put(" count" , it.toString()) }
254258 hours?.let { put(" hours" , it.toString()) }
255259 region?.let { put(" region" , it) }
256- source?.let { put(" source" , it) }
260+ source?.let { put(" source" , it.toString() ) }
257261 putAll(additionalQueryParams)
258262 }
259263 .build()
260264
265+ /* *
266+ * Source filter. One of: github, google_trends, hacker_news, polymarket, reddit, trustmrr,
267+ * wikipedia
268+ */
269+ class Source @JsonCreator private constructor(private val value : JsonField <String >) : Enum {
270+
271+ /* *
272+ * Returns this class instance's raw value.
273+ *
274+ * This is usually only useful if this instance was deserialized from data that doesn't
275+ * match any known member, and you want to know that value. For example, if the SDK is on an
276+ * older version than the API, then the API may respond with new members that the SDK is
277+ * unaware of.
278+ */
279+ @com.fasterxml.jackson.annotation.JsonValue fun _value (): JsonField <String > = value
280+
281+ companion object {
282+
283+ @JvmField val GITHUB = of(" github" )
284+
285+ @JvmField val GOOGLE_TRENDS = of(" google_trends" )
286+
287+ @JvmField val HACKER_NEWS = of(" hacker_news" )
288+
289+ @JvmField val POLYMARKET = of(" polymarket" )
290+
291+ @JvmField val REDDIT = of(" reddit" )
292+
293+ @JvmField val TRUSTMRR = of(" trustmrr" )
294+
295+ @JvmField val WIKIPEDIA = of(" wikipedia" )
296+
297+ @JvmStatic fun of (value : String ) = Source (JsonField .of(value))
298+ }
299+
300+ /* * An enum containing [Source]'s known values. */
301+ enum class Known {
302+ GITHUB ,
303+ GOOGLE_TRENDS ,
304+ HACKER_NEWS ,
305+ POLYMARKET ,
306+ REDDIT ,
307+ TRUSTMRR ,
308+ WIKIPEDIA ,
309+ }
310+
311+ /* *
312+ * An enum containing [Source]'s known values, as well as an [_UNKNOWN] member.
313+ *
314+ * An instance of [Source] can contain an unknown value in a couple of cases:
315+ * - It was deserialized from data that doesn't match any known member. For example, if the
316+ * SDK is on an older version than the API, then the API may respond with new members that
317+ * the SDK is unaware of.
318+ * - It was constructed with an arbitrary value using the [of] method.
319+ */
320+ enum class Value {
321+ GITHUB ,
322+ GOOGLE_TRENDS ,
323+ HACKER_NEWS ,
324+ POLYMARKET ,
325+ REDDIT ,
326+ TRUSTMRR ,
327+ WIKIPEDIA ,
328+ /* * An enum member indicating that [Source] was instantiated with an unknown value. */
329+ _UNKNOWN ,
330+ }
331+
332+ /* *
333+ * Returns an enum member corresponding to this class instance's value, or [Value._UNKNOWN]
334+ * if the class was instantiated with an unknown value.
335+ *
336+ * Use the [known] method instead if you're certain the value is always known or if you want
337+ * to throw for the unknown case.
338+ */
339+ fun value (): Value =
340+ when (this ) {
341+ GITHUB -> Value .GITHUB
342+ GOOGLE_TRENDS -> Value .GOOGLE_TRENDS
343+ HACKER_NEWS -> Value .HACKER_NEWS
344+ POLYMARKET -> Value .POLYMARKET
345+ REDDIT -> Value .REDDIT
346+ TRUSTMRR -> Value .TRUSTMRR
347+ WIKIPEDIA -> Value .WIKIPEDIA
348+ else -> Value ._UNKNOWN
349+ }
350+
351+ /* *
352+ * Returns an enum member corresponding to this class instance's value.
353+ *
354+ * Use the [value] method instead if you're uncertain the value is always known and don't
355+ * want to throw for the unknown case.
356+ *
357+ * @throws XTwitterScraperInvalidDataException if this class instance's value is a not a
358+ * known member.
359+ */
360+ fun known (): Known =
361+ when (this ) {
362+ GITHUB -> Known .GITHUB
363+ GOOGLE_TRENDS -> Known .GOOGLE_TRENDS
364+ HACKER_NEWS -> Known .HACKER_NEWS
365+ POLYMARKET -> Known .POLYMARKET
366+ REDDIT -> Known .REDDIT
367+ TRUSTMRR -> Known .TRUSTMRR
368+ WIKIPEDIA -> Known .WIKIPEDIA
369+ else -> throw XTwitterScraperInvalidDataException (" Unknown Source: $value " )
370+ }
371+
372+ /* *
373+ * Returns this class instance's primitive wire representation.
374+ *
375+ * This differs from the [toString] method because that method is primarily for debugging
376+ * and generally doesn't throw.
377+ *
378+ * @throws XTwitterScraperInvalidDataException if this class instance's value does not have
379+ * the expected primitive type.
380+ */
381+ fun asString (): String =
382+ _value ().asString().orElseThrow {
383+ XTwitterScraperInvalidDataException (" Value is not a String" )
384+ }
385+
386+ private var validated: Boolean = false
387+
388+ fun validate (): Source = apply {
389+ if (validated) {
390+ return @apply
391+ }
392+
393+ known()
394+ validated = true
395+ }
396+
397+ fun isValid (): Boolean =
398+ try {
399+ validate()
400+ true
401+ } catch (e: XTwitterScraperInvalidDataException ) {
402+ false
403+ }
404+
405+ /* *
406+ * Returns a score indicating how many valid values are contained in this object
407+ * recursively.
408+ *
409+ * Used for best match union deserialization.
410+ */
411+ @JvmSynthetic internal fun validity (): Int = if (value() == Value ._UNKNOWN ) 0 else 1
412+
413+ override fun equals (other : Any? ): Boolean {
414+ if (this == = other) {
415+ return true
416+ }
417+
418+ return other is Source && value == other.value
419+ }
420+
421+ override fun hashCode () = value.hashCode()
422+
423+ override fun toString () = value.toString()
424+ }
425+
261426 override fun equals (other : Any? ): Boolean {
262427 if (this == = other) {
263428 return true
0 commit comments