All Downloads are FREE. Search and download functionalities are using the official Maven repository.

stexceed.WarframeMarKT.1.0.1.source-code.WarframeMarket.kt Maven / Gradle / Ivy

import com.fasterxml.jackson.annotation.JsonInclude
import enums.Platform
import enums.Region
import io.ktor.client.HttpClient
import io.ktor.client.features.cookies.AcceptAllCookiesStorage
import io.ktor.client.features.cookies.HttpCookies
import io.ktor.client.features.cookies.cookies
import io.ktor.client.features.json.JacksonSerializer
import io.ktor.client.features.json.JsonFeature
import io.ktor.client.request.*
import io.ktor.client.statement.HttpResponse
import io.ktor.http.ContentType
import io.ktor.http.contentType
import payload.PayloadContainer
import payload.request.*
import payload.response.*
import payload.response.common.Order
import payload.response.common.User
import java.net.URL
import kotlin.reflect.full.memberProperties

//naming case conventions are intentionally violated because reflection is used to build url strings, which are case sensitive
//this also helps with disambiguation between endpoints and payload types
@Suppress("ClassName", "unused", "FunctionName")
object WarframeMarket : Endpoint(null) {
	override val url = "https://api.warframe.market"
	var language = Region.en
	var platform = Platform.pc

	private val httpClient = HttpClient {
		install(JsonFeature) {
			serializer = JacksonSerializer {
				setSerializationInclusion(JsonInclude.Include.NON_NULL)
			}
			//TODO: don't initialize nullable types with default value unless explicitly specified
		}
		install(HttpCookies) {
			storage = AcceptAllCookiesStorage()
		}
	}

	private var jwt = ""

	private fun createRequestBuilder(url: String, payload: Any? = null) =
		HttpRequestBuilder(URL(url)).apply {
			contentType(ContentType.Application.Json)
			accept(ContentType.Application.Json)
			header("language", language)
			header("platform", platform)
			header("Authorization", "JWT $jwt")
			if (payload != null) {
				body = payload
			}
		}

	object v1 : Endpoint(WarframeMarket) {
		object auctions : Endpoint(v1), Get {
			override suspend fun get() = httpClient.get>(url).payload

			object create : Endpoint(auctions), Create {
				override suspend fun create(payload: AuctionCreate) =
					httpClient.post>(createRequestBuilder(url, payload)).payload
			}

			object entry : Endpoint(auctions) {
				class ENTRY(auction_id: String) : Endpoint(entry), Get, Update {
					override val pathName = auction_id

					override suspend fun get() = httpClient.get>(url).payload

					override suspend fun update(payload: AuctionUpdate) =
						httpClient.put>(createRequestBuilder(url, payload)).payload

					suspend fun close() =
						httpClient.put>(createRequestBuilder("$url/close")).payload

					val bids = Bids(this)

					class Bids internal constructor(parent: Endpoint) : Endpoint(parent), Get {
						override suspend fun get() = httpClient.get>(url).payload
					}
				}
			}

			object participant : Endpoint(auctions), Get {
				override suspend fun get() = httpClient.get>(url).payload
			}

			object popular : Endpoint(auctions), Get {
				override suspend fun get() = httpClient.get>(url).payload
			}

			object search : Endpoint(auctions) {
				suspend fun get(searchParameters: AuctionSearch) = httpClient.get>(url) {
					url {
						searchParameters::class.memberProperties.forEach { property ->
							property.getter.call(searchParameters)?.let { value ->
								parameter(property.name, value)
							}
						}
					}
				}.payload
			}
		}

		object auth : Endpoint(v1) {
			suspend fun signIn(payload: SigninCredentials): ProfilePrivate {
				val profile = httpClient.post>(createRequestBuilder("$url/signin", payload)).payload
				jwt = httpClient.cookies("$url/signin").find { it.name == "JWT" }!!.value
				return profile
			}

			suspend fun signOut(): Unit { //returns a profile with all values null
				httpClient.get("$url/signout")
				jwt = ""
			}

			suspend fun register(): Nothing = TODO()

			suspend fun changePassword(payload: PasswordChange) = httpClient.post>(createRequestBuilder(v1.url + "/settings/account/change_password", payload)).payload
		}

		object im : Endpoint(v1) {
			object chats : Endpoint(im), Get {
				override suspend fun get() = httpClient.get>(createRequestBuilder(url)).payload

				class CHAT(chat_id: String) : Endpoint(chats), Get>, Delete {
					override val pathName = chat_id

					override suspend fun get() = httpClient.get>>(createRequestBuilder(url)).payload

					override suspend fun delete() = httpClient.delete>(createRequestBuilder(url)).payload
				}
			}

			object ignore : Endpoint(im), Get>, Create {
				override suspend fun get() = httpClient.get>>(createRequestBuilder(url)).payload

				override suspend fun create(payload: IgnoreCreate) =
					httpClient.post>(createRequestBuilder(url, payload)).payload

				class IGNORE(user_id: String) : Endpoint(im), Delete {
					override val pathName = user_id

					override suspend fun delete() = httpClient.delete>(createRequestBuilder(url)).payload
				}
			}
		}

		object items : Endpoint(v1), Get {
			override suspend fun get() = httpClient.get>(createRequestBuilder(url)).payload

			class ITEM(url_name: String) : Endpoint(items), Get {
				override val pathName = url_name

				override suspend fun get() = httpClient.get>(createRequestBuilder(url)).payload

				val orders = Orders(this)
				val statistics = Statistics(this)

				class Orders internal constructor(parent: Endpoint?) : Endpoint(parent), Get {
					override suspend fun get() =
						httpClient.get>(createRequestBuilder(url)).payload

					val top = Top(this)

					class Top internal constructor(parent: Endpoint?) : Endpoint(parent), Get {
						override suspend fun get() =
							httpClient.get>(createRequestBuilder(url)).payload
					}
				}

				class Statistics internal constructor(parent: Endpoint?) : Endpoint(parent), Get {
					override suspend fun get() =
						httpClient.get>(createRequestBuilder(url)).payload
				}
			}
		}

		object tools : Endpoint(v1) {
			object ducats : Endpoint(tools), Get {
				override suspend fun get() =
					httpClient.get>(createRequestBuilder(url)).payload
			}
		}

		object profile : Endpoint(v1), Get {
			override suspend fun get() =
				httpClient.get>(createRequestBuilder(url)).payload

			object auctions : Endpoint(profile), Get {
				override suspend fun get() = httpClient.get>(url).payload
			}

			object customization : Endpoint(profile) {
				object about : Endpoint(customization), Update {
					override suspend fun update(payload: AboutUpdate) =
						httpClient.put>(createRequestBuilder(url, payload)).payload
				}

				object avatar : Endpoint(customization), Create {
					override suspend fun create(payload: Nothing) = TODO() //body is a binary image
				}
			}

			object orders : Endpoint(profile), Get, Create {
				override suspend fun get() =
					httpClient.get>(createRequestBuilder(url)).payload

				override suspend fun create(payload: OrderCreate) =
					httpClient.post>(createRequestBuilder(url, payload)).payload

				class ORDER(order_id: String) : Endpoint(orders), Update, Delete {
					override val pathName = order_id

					override suspend fun update(payload: OrderUpdate) =
						httpClient.put>(createRequestBuilder(url, payload)).payload

					override suspend fun delete() =
						httpClient.delete>(createRequestBuilder(url)).payload

					suspend fun close() =
						httpClient.put>(createRequestBuilder(orders.url + "/close/" + pathName)).payload
				}
			}

			class USER(username: String) : Endpoint(profile), Get {
				override val pathName = username

				override suspend fun get() =
					httpClient.get>(createRequestBuilder(url)).payload

				val achievements = Achievements(this)
				val orders = Orders(this)
				val review = Review(this)
				val reviews = Reviews(this)
				val statistics = Statistics(this)

				class Achievements internal constructor(parent: Endpoint) : Endpoint(parent), Get {
					override suspend fun get() =
						httpClient.get>(createRequestBuilder(url)).payload
				}

				class Orders internal constructor(parent: Endpoint) : Endpoint(parent), Get {
					override suspend fun get() =
						httpClient.get>(createRequestBuilder(url)).payload
				}

				class Review internal constructor(parent: Endpoint) : Endpoint(parent),
					Create {
					override suspend fun create(payload: ReviewCreate) =
						httpClient.post>(createRequestBuilder(url, payload)).payload

					fun REVIEW(review_id: String) = REVIEW(review_id, this)

					class REVIEW internal constructor(override val pathName: String, parent: Endpoint?) :
						Endpoint(parent),
						Update, Delete {
						override suspend fun update(payload: ReviewUpdate) =
							httpClient.put>(createRequestBuilder(url, payload)).payload

						override suspend fun delete() =
							httpClient.delete>(createRequestBuilder(url)).payload
					}
				}

				class Reviews internal constructor(parent: Endpoint) : Endpoint(parent), Get {
					override suspend fun get() =
						httpClient.get>(createRequestBuilder(url)).payload
				}

				class Statistics internal constructor(parent: Endpoint) : Endpoint(parent), Get {
					override suspend fun get() =
						httpClient.get>(createRequestBuilder(url)).payload
				}
			}
		}

		object settings : Endpoint(v1) {
			object notifications : Endpoint(settings) {
				object push : Endpoint(settings), Create, Delete {
					override suspend fun create(payload: PushNotificationCreate) =
						httpClient.post>(createRequestBuilder(url)).payload

					override suspend fun delete() =
						httpClient.delete>(createRequestBuilder(url)).payload
				}
			}

			object verification : Endpoint(settings) {
				suspend fun patch(payload: VerificationPatch) =
					httpClient.patch>(
						createRequestBuilder(
							url,
							payload
						)
					).payload
			}
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy