Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: API Version support #360

Merged
merged 27 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
ab737ee
fix: remove dependency on kiit.common.auth
kishorereddy Nov 26, 2023
aa8b13e
wip: api route loading changes
kishorereddy Nov 27, 2023
e775c7b
wip: router updated with new version support
kishorereddy Nov 27, 2023
c50d632
wip: server update for version support
kishorereddy Nov 27, 2023
f86b707
wip
kishorereddy Nov 27, 2023
1be9c86
wip
kishorereddy Nov 28, 2023
8f1acc2
wip
kishorereddy Dec 3, 2023
f78bb3b
wip: new router with version
kishorereddy Dec 3, 2023
8c9f9bf
wip: fix set up
kishorereddy Dec 3, 2023
165a32a
wip: router working
kishorereddy Dec 3, 2023
2aeea49
Update Api_001_Loader_Tests.kt
kishorereddy Dec 3, 2023
75eea0e
fix : security for loading only public, annotated methods
kishorereddy Dec 3, 2023
c8bb6fb
fix: action execution
kishorereddy Dec 3, 2023
520dd55
wip: protocol tests
kishorereddy Dec 4, 2023
559d06a
fix: additional tests
kishorereddy Dec 4, 2023
f7b516c
fix : security tests
kishorereddy Dec 4, 2023
0ac024a
fix : tests for queuing working
kishorereddy Dec 4, 2023
43ab2af
fix: db tests
kishorereddy Dec 4, 2023
3d46fdd
wip: config loader
kishorereddy Dec 4, 2023
1b6164d
fix : loading api set up from config file
kishorereddy Dec 4, 2023
b1236b3
wip: redirect support
kishorereddy Dec 4, 2023
a15e7ee
fix: use constants and support for redirect
kishorereddy Dec 4, 2023
ffc13f3
wip: cleanup to middle ware
kishorereddy Dec 4, 2023
060db82
fix : naming tests
kishorereddy Dec 9, 2023
2cd0b1a
fix: api input validation tests
kishorereddy Dec 9, 2023
b7827ea
setup: for parameter fixes
kishorereddy Dec 9, 2023
f808ab7
wip: custom decoders for api
kishorereddy Dec 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added conf/.DS_Store
Binary file not shown.
5 changes: 4 additions & 1 deletion src/common/common/src/main/kotlin/kiit/common/Source.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package kiit.common

import java.util.*

/**
* Used to represent the source / origin of a request/item being processed
*/
Expand Down Expand Up @@ -91,9 +93,10 @@ sealed class Source(val id: String) {
companion object {

fun parse(name:String): Source {
return when(name) {
return when(name.lowercase(Locale.getDefault())) {
Parent.id -> Parent
All.id -> All
"all" -> All
API.id -> API
Auto.id -> Auto
Bot.id -> Bot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ data class CommonRequest(
override val raw: Any? = null,
override val output: String? = "",
override val tag: String = "",
override val version: String = "1.0",
override val version: String = "0",
override val timestamp: DateTime = DateTime.now()
) : Request {

Expand Down Expand Up @@ -101,7 +101,8 @@ data class CommonRequest(
verb: String,
opts: Map<String, Any>,
args: Map<String, Any>,
raw: Any? = null
raw: Any? = null,
version: String = "0"
): Request {
val path = if (area.isNullOrEmpty()) "$api.$action" else "$area.$api.$action"
return CommonRequest(
Expand All @@ -111,7 +112,8 @@ data class CommonRequest(
verb,
InputArgs(args),
meta = InputArgs(opts),
raw = raw
raw = raw,
version = version
)
}

Expand Down Expand Up @@ -160,13 +162,14 @@ data class CommonRequest(
verb: String,
opts: Map<String, Any>,
args: Map<String, Any>,
raw: Any? = null
raw: Any? = null,
version: String = "0"
): Request {
val parts = path.split(".")
val area = parts[0]
val api = parts[1]
val action = parts[2]
return cli(area, api, action, verb, opts, args, raw)
return cli(area, api, action, verb, opts, args, raw, version)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package kiit.connectors.cli
import kiit.apis.ApiServer
import kiit.apis.routes.Api
import kiit.apis.core.Part
import kiit.apis.routes.VersionAreas
import kiit.cli.CLI
import kiit.cli.CliRequest
import kiit.cli.CliResponse
Expand Down Expand Up @@ -54,12 +55,12 @@ open class CliApi(
val ctx: Context,
val auth: kiit.apis.core.Auth,
settings: CliSettings = CliSettings(),
apiItems: List<Api> = listOf(),
routes: List<VersionAreas> = listOf(),
serializer: (Any?, ContentType) -> Content,
val metaTransform: ((Map<String, Any>) -> List<Pair<String, String>>)? = null
) : CLI(settings, ctx.info, ctx.dirs, serializer = serializer) {
// api container holding all the apis.
val apis = ApiServer.of(ctx, apiItems, auth, Source.CLI)
val apis = ApiServer.of(ctx, routes, auth, Source.CLI)

/**
* executes a line of text by handing it off to the executor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ interface JSONTransformer<S> : Transformer<S, JSONObject, String> {
}



interface JSONRestoreWithContext<S> : Transformer<S, JSONObject, String> {

/**
Expand Down
3 changes: 2 additions & 1 deletion src/lib/kotlin/kiit/src/main/kotlin/kiit/Kiit.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kiit

import kiit.apis.setup.routes
import kiit.apis.support.Authenticator
import kiit.app.App
import kiit.app.AppOptions
Expand Down Expand Up @@ -170,7 +171,7 @@ class Kiit(ctx: Context) : App<Context>(ctx, AppOptions(showWelcome = false, sho
ctx = ctx,
auth = auth,
settings = CliSettings(enableLogging = true, enableOutput = true),
apiItems = apis,
routes = routes(apis),
metaTransform = {
listOf("api-key" to keys.first().key)
},
Expand Down
15 changes: 10 additions & 5 deletions src/lib/kotlin/kiit/src/main/kotlin/kiit/KiitServices.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package kiit

import kiit.apis.SetupType
import kiit.apis.routes.Api
import kiit.apis.setup.GlobalVersion
import kiit.apis.setup.api
import kiit.apis.tools.code.CodeGenApi
import kiit.common.conf.Conf
import kiit.context.Context
Expand All @@ -12,15 +14,18 @@ interface KiitServices {

val ctx: Context

fun apis(settings:Conf): List<Api> {
fun apis(settings:Conf): List<GlobalVersion> {
// APIs
val toolSettings = ToolSettings(settings.getString("kiit.version"), settings.getString("kiit.version.beta"), "logs/logback.log")
val buildSettings = BuildSettings(settings.getString("kotlin.version"))
val logger = ctx.logs.getLogger("gen")
val generator = GeneratorApi(ctx, GeneratorService(ctx, settings, Kiit::class.java, GeneratorSettings(toolSettings, buildSettings), logger = logger))
return listOf(
Api(GeneratorApi(ctx, GeneratorService(ctx, settings, Kiit::class.java, GeneratorSettings(toolSettings, buildSettings), logger = logger)), declaredOnly = true, setup = SetupType.Annotated),
Api(DocApi(ctx), declaredOnly = true, setup = SetupType.Annotated),
Api(CodeGenApi(), declaredOnly = true, setup = SetupType.Annotated)
)
GlobalVersion("0", listOf(
api(GeneratorApi::class, generator),
api(DocApi::class, DocApi(ctx)),
api(CodeGenApi::class, CodeGenApi())
)
))
}
}
27 changes: 18 additions & 9 deletions src/lib/kotlin/kiit/src/main/kotlin/kiit/Server.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
import kiit.apis.*
import kiit.apis.core.Auth
import kiit.apis.routes.Api
import kiit.apis.core.Meta
import kiit.apis.setup.GlobalVersion
import kiit.apis.setup.api
import kiit.apis.setup.routes

// Slate Kit - Common Utilities
import kiit.common.*
Expand All @@ -20,7 +23,7 @@ import kiit.results.*
// Slate Kit - App ( provides args, help, life-cycle methods, etc )
import kiit.requests.toResponse
import kiit.context.Context
import kiit.core.common.FileUtils
import kiit.requests.Request

// Slate Kit - Server ( Ktor support )
import kiit.server.ServerSettings
Expand All @@ -39,7 +42,7 @@ class Server(val ctx: Context) {
suspend fun execute(): Try<Any> {

// 1. Settings
val settings = ServerSettings(port = 5000, prefix = "/api/", docs = true, docKey = "abc123", formatJson = true)
val settings = ServerSettings(port = 5200, prefix = "/api/", docs = true, docKey = "abc123", formatJson = true)

// 2. APIs ( these are Slate Kit Universal APIs )
val apis = apis()
Expand All @@ -48,7 +51,8 @@ class Server(val ctx: Context) {
val auth: Auth? = null //SampleAuth()

// 4. API host
val apiHost = ApiServer.of( ctx, apis, auth = null)
val routes = routes(apis)
val apiHost = ApiServer.of( ctx, routes, auth = null)

// Ktor response handler
val responder = KtorResponse(settings)
Expand Down Expand Up @@ -89,10 +93,9 @@ class Server(val ctx: Context) {
}


fun apis(): List<Api> {
return listOf(
Api(klass = SampleFiles3Api::class, singleton = SampleFiles3Api(), setup = SetupType.Annotated)
)
fun apis(): List<GlobalVersion> {
val apis = listOf(GlobalVersion("0", listOf(api(SampleFiles3Api::class, SampleFiles3Api()))))
return apis
}


Expand All @@ -114,9 +117,15 @@ class SampleFiles3Api {
val sampleCSV = "user1,u1@a.com,true,1234\r\nuser2,u2@a.com,true,1234"
val sampleCSVData = sampleCSV.toByteArray()

@Action()
fun getRequest(request: Request) : String {
return "got inputs"
}

@Action(desc = "test getting content as xml")
fun getData(): String = DateTime.now().toStringMySql()
fun getData(request: Request): String {
return DateTime.now().toStringMySql()
}

@Action(desc = "test getting content as xml")
fun getContentCsv(): Content = Contents.csv("user1,u1@a.com,true,1234\r\nuser2,u2@a.com,true,1234")
Expand Down
20 changes: 10 additions & 10 deletions src/lib/kotlin/kiit/src/main/kotlin/kiit/run.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ import kiit.results.Success
* java -jar ${app.name}.jar -env=dev -log.level=info -conf.dir = "file://./conf-sample-shell"
* java -jar ${app.name}.jar -env=dev -log.level=info -conf.dir = "file://./conf-sample-server"
*
* slatekit new app -name="MyApp1" -packageName="company1.apps"
* slatekit new api -name="MyAPI1" -packageName="company1.apis"
* slatekit new cli -name="MyCLI1" -packageName="company1.apps"
* slatekit new env -name="MyApp2" -packageName="company1.apps"
* slatekit new job -name="MyJob1" -packageName="company1.jobs"
* slatekit new lib -name="MyLib1" -packageName="company1.libs"
* slatekit new orm -name="MyApp1" -packageName="company1.apps"
* kiit new app -name="MyApp1" -packageName="company1.apps"
* kiit new api -name="MyAPI1" -packageName="company1.apis"
* kiit new cli -name="MyCLI1" -packageName="company1.apps"
* kiit new env -name="MyApp2" -packageName="company1.apps"
* kiit new job -name="MyJob1" -packageName="company1.jobs"
* kiit new lib -name="MyLib1" -packageName="company1.libs"
* kiit new orm -name="MyApp1" -packageName="company1.apps"
*
* -job.name=queued
*
Expand All @@ -63,8 +63,8 @@ fun main(args: Array<String>) {
help.show()
}
else {
run(args)
//api(args)
//run(args)
api(args)
}
}
is Failure -> {
Expand All @@ -84,7 +84,7 @@ fun api(args: Array<String>) {
}


fun run(args:Array<String>){
fun run(args:Array<String>) {
/**
* DOCS : https://www.kiit.dev/arch/app/
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ package test.apis
import kotlinx.coroutines.runBlocking
import org.junit.Assert
import kiit.apis.*
import kiit.apis.routes.Api
import kiit.apis.core.Auth
import kiit.apis.SetupType
import kiit.apis.routes.VersionAreas
import kiit.requests.CommonRequest
import kiit.common.Source
import kiit.common.args.Args
import kiit.common.conf.Config
import kiit.common.crypto.Encryptor
import kiit.common.data.DbConString
import kiit.common.data.Connections
import kiit.common.data.Connections.Companion.of
Expand All @@ -36,9 +36,9 @@ import kiit.entities.Entities
import kiit.policy.hooks.Middleware
import kiit.connectors.entities.AppEntContext
import kiit.results.Try
import kiit.serialization.deserializer.Deserializer
import test.TestApp
import test.setup.MyAuthProvider
import test.setup.UserApi
import test.setup.MyEncryptor

/**
Expand Down Expand Up @@ -87,15 +87,15 @@ open class ApiTestsBase {

fun getApis(source: Source,
auth: Auth? = null,
apis: List<Api> = listOf()): ApiServer {
apis: List<VersionAreas> = listOf()): ApiServer {

// 2. apis
val container = ApiServer.of(ctx, apis, auth, source)
return container
}


fun ensureCall(apis: List<Api> = listOf(),
fun ensureCall(apis: List<VersionAreas> = listOf(),
protocolCt: String,
protocol: String,
authMode: String,
Expand Down Expand Up @@ -124,28 +124,30 @@ open class ApiTestsBase {
}


fun buildUserApiRegSingleton(ctx: AppEntContext): Api {
return Api(UserApi(ctx), setup = SetupType.Annotated)
}
// fun buildUserApiRegSingleton(ctx: AppEntContext): Api {
// return Api(UserApi(ctx), setup = SetupType.Annotated)
// }


fun ensure(
protocol: Source,
middleware: List<Middleware> = listOf(),
apis: List<Api>,
user: Credentials?,
request: Request,
response: Response<*>,
checkFailMsg:Boolean = false) {
fun checkCall(
protocol: Source,
middleware: List<Middleware> = listOf(),
routes: List<VersionAreas>,
user: Credentials?,
request: Request,
response: Response<*>,
checkFailMsg:Boolean = false,
decoder: ((Request, Encryptor?) -> Deserializer)? = null
) {

// Optional auth
val auth = user?.let { u -> MyAuthProvider(u.name, u.roles, buildKeys()) }

// Host
val host = ApiServer(ctx,
apis = apis,
routes = routes,
auth = auth,
settings = Settings(protocol))
settings = Settings(protocol, decoder = decoder))

// Get result
val actual = runBlocking {
Expand Down
Loading