Skip to content

Commit

Permalink
Endpoints for Tokens (#461)
Browse files Browse the repository at this point in the history
* Endpoints for tokens

* Typo

* Test fixed
  • Loading branch information
javipacheco authored Sep 27, 2023
1 parent e4a4739 commit adb90be
Show file tree
Hide file tree
Showing 11 changed files with 430 additions and 17 deletions.
205 changes: 201 additions & 4 deletions server/docs/postman/xef_postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@
"response": []
},
{
"name": "Get Project",
"name": "Get Projects By Org",
"protocolProfileBehavior": {
"disableBodyPruning": true
},
Expand All @@ -393,14 +393,15 @@
}
},
"url": {
"raw": "{{url}}/v1/settings/projects/1",
"raw": "{{url}}/v1/settings/projects/org/1",
"host": [
"{{url}}"
],
"path": [
"v1",
"settings",
"projects",
"org",
"1"
]
}
Expand All @@ -423,19 +424,215 @@
"method": "DELETE",
"header": [],
"url": {
"raw": "{{url}}/v1/settings/projects/1",
"raw": "{{url}}/v1/settings/projects/4",
"host": [
"{{url}}"
],
"path": [
"v1",
"settings",
"projects",
"4"
]
}
},
"response": []
}
]
},
{
"name": "Xef Tokens",
"item": [
{
"name": "Create Token",
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "{{access_token}}",
"type": "string"
}
]
},
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"name\": \"My Token\",\n \"projectId\": 1\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{url}}/v1/settings/tokens",
"host": [
"{{url}}"
],
"path": [
"v1",
"settings",
"tokens"
]
}
},
"response": []
},
{
"name": "Update Token",
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "{{access_token}}",
"type": "string"
}
]
},
"method": "PUT",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"name\": \"My Token\",\n \"providerConfig\": {\n \"open_ai\": {\n \"token\": \"openai_token\",\n \"url\": null\n },\n \"gcp\": {\n \"token\": \"openai_token\",\n \"project_id\": \"my_project_id\",\n \"location\": \"my_location\"\n }\n }\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{url}}/v1/settings/tokens/1",
"host": [
"{{url}}"
],
"path": [
"v1",
"settings",
"tokens",
"1"
]
}
},
"response": []
},
{
"name": "Get Tokens",
"protocolProfileBehavior": {
"disableBodyPruning": true
},
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "{{access_token}}",
"type": "string"
}
]
},
"method": "GET",
"header": [],
"body": {
"mode": "raw",
"raw": "",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{url}}/v1/settings/tokens",
"host": [
"{{url}}"
],
"path": [
"v1",
"settings",
"tokens"
]
}
},
"response": []
},
{
"name": "Get Tokens by Project",
"protocolProfileBehavior": {
"disableBodyPruning": true
},
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "{{access_token}}",
"type": "string"
}
]
},
"method": "GET",
"header": [],
"body": {
"mode": "raw",
"raw": "",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{url}}/v1/settings/tokens/projects/1",
"host": [
"{{url}}"
],
"path": [
"v1",
"settings",
"tokens",
"projects",
"1"
]
}
},
"response": []
},
{
"name": "Delete tokens",
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "{{access_token}}",
"type": "string"
}
]
},
"method": "DELETE",
"header": [],
"url": {
"raw": "{{url}}/v1/settings/tokens/1",
"host": [
"{{url}}"
],
"path": [
"v1",
"settings",
"tokens",
"1"
]
}
},
"response": []
}
]
},
Expand Down Expand Up @@ -472,7 +669,7 @@
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"name\": \"JC2\",\n \"email\": \"[email protected]\",\n \"password\": \"1234\"\n}",
"raw": "{\n \"name\": \"JC\",\n \"email\": \"[email protected]\",\n \"password\": \"1234\"\n}",
"options": {
"raw": {
"language": "json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ suspend fun ApplicationCall.manageException(cause: XefExceptions) {
is XefExceptions.AuthorizationException -> this.respond(HttpStatusCode.Unauthorized)
is XefExceptions.OrganizationsException -> this.respond(HttpStatusCode.BadRequest, cause.message)
is XefExceptions.ProjectException -> this.respond(HttpStatusCode.BadRequest, cause.message)
is XefExceptions.XefTokenException -> this.respond(HttpStatusCode.BadRequest, cause.message)
is XefExceptions.UserException -> this.respond(HttpStatusCode.BadRequest, cause.message)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.xebia.functional.xef.server.http.routes

import com.xebia.functional.xef.server.models.TokenRequest
import com.xebia.functional.xef.server.models.TokenUpdateRequest
import com.xebia.functional.xef.server.services.TokenRepositoryService
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.auth.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.serialization.json.Json

fun Routing.tokensRoutes(
tokenRepositoryService: TokenRepositoryService
) {
authenticate("auth-bearer") {
get("/v1/settings/tokens") {
val token = call.getToken()
val response = tokenRepositoryService.getTokens(token)
call.respond(response)
}
get("/v1/settings/tokens/projects/{id}") {
val token = call.getToken()
val id = call.getId()
val response = tokenRepositoryService.getTokensByProject(token, id)
call.respond(response)
}
post("/v1/settings/tokens") {

val request = Json.decodeFromString<TokenRequest>(call.receive<String>())
val token = call.getToken()
val response = tokenRepositoryService.createToken(request, token)
call.respond(
status = HttpStatusCode.Created,
response
)
}
put("/v1/settings/tokens/{id}") {
val request = Json.decodeFromString<TokenUpdateRequest>(call.receive<String>())
val token = call.getToken()
val id = call.getId()
val response = tokenRepositoryService.updateToken(token, request, id)
call.respond(
status = HttpStatusCode.NoContent,
response
)
}
delete("/v1/settings/tokens/{id}") {
val token = call.getToken()
val id = call.getId()
val response = tokenRepositoryService.deleteToken(token, id)
call.respond(
status = HttpStatusCode.NoContent,
response
)
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package com.xebia.functional.xef.server.http.routes

import com.xebia.functional.xef.server.services.OrganizationRepositoryService
import com.xebia.functional.xef.server.services.ProjectRepositoryService
import com.xebia.functional.xef.server.services.TokenRepositoryService
import com.xebia.functional.xef.server.services.UserRepositoryService
import io.ktor.client.*
import io.ktor.server.routing.*
import org.slf4j.Logger

Expand All @@ -13,4 +13,5 @@ fun Routing.xefRoutes(
userRoutes(UserRepositoryService(logger))
organizationRoutes(OrganizationRepositoryService(logger))
projectsRoutes(ProjectRepositoryService(logger))
tokensRoutes(TokenRepositoryService(logger))
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
@SerialName("open_ai")
data class OpenAIConf(
val name: String,
val token: String,
val url: String
val url: String?
)

@Serializable
@SerialName("gcp")
data class GCPConf(
val name: String,
val token: String,
@SerialName("project_id")
val projectId: String,
val location: String
)
Expand All @@ -26,4 +23,8 @@ data class ProvidersConfig(
val openAI: OpenAIConf?,
@SerialName("gcp")
val gcp: GCPConf?
)
) {
companion object {
val empty = ProvidersConfig(null, null)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,15 @@ data class ProjectUpdateRequest(
val name: String,
val orgId: Int? = null
)

@Serializable
data class TokenRequest(
val name: String,
val projectId: Int
)

@Serializable
data class TokenUpdateRequest(
val name: String,
val providerConfig: ProvidersConfig
)
Loading

0 comments on commit adb90be

Please sign in to comment.