Browse Source

update packages, add jvm example, use optional string serialization

main
James Fenn 1 month ago
parent
commit
f2bd8b9a88
35 changed files with 177 additions and 79 deletions
  1. +1
    -2
      build.gradle
  2. +1
    -0
      example-jvm/.gitignore
  3. +25
    -0
      example-jvm/build.gradle
  4. +33
    -0
      example-jvm/src/main/java/me/jfenn/gitrest/example/jvm/Main.kt
  5. +4
    -4
      example-kotlinbrowser/src/main/kotlin/Main.kt
  6. +7
    -0
      gitrest/build.gradle
  7. +5
    -4
      gitrest/src/commonMain/kotlin/me/jfenn/gitrest/RequestProviderDelegate.kt
  8. +4
    -4
      gitrest/src/commonMain/kotlin/me/jfenn/gitrest/base/RequestProvider.kt
  9. +1
    -1
      gitrest/src/commonMain/kotlin/me/jfenn/gitrest/base/ServiceBuilder.kt
  10. +7
    -7
      gitrest/src/commonMain/kotlin/me/jfenn/gitrest/impl/gitea/GiteaProvider.kt
  11. +1
    -1
      gitrest/src/commonMain/kotlin/me/jfenn/gitrest/impl/gitea/model/GiteaLicense.kt
  12. +7
    -2
      gitrest/src/commonMain/kotlin/me/jfenn/gitrest/impl/gitea/model/GiteaRepo.kt
  13. +4
    -2
      gitrest/src/commonMain/kotlin/me/jfenn/gitrest/impl/gitea/model/GiteaUser.kt
  14. +6
    -6
      gitrest/src/commonMain/kotlin/me/jfenn/gitrest/impl/github/GithubProvider.kt
  15. +2
    -2
      gitrest/src/commonMain/kotlin/me/jfenn/gitrest/impl/github/model/GithubLicense.kt
  16. +5
    -2
      gitrest/src/commonMain/kotlin/me/jfenn/gitrest/impl/github/model/GithubRepo.kt
  17. +2
    -2
      gitrest/src/commonMain/kotlin/me/jfenn/gitrest/impl/github/model/GithubUser.kt
  18. +6
    -6
      gitrest/src/commonMain/kotlin/me/jfenn/gitrest/impl/gitlab/GitlabProvider.kt
  19. +2
    -2
      gitrest/src/commonMain/kotlin/me/jfenn/gitrest/impl/gitlab/model/GitlabLicense.kt
  20. +2
    -2
      gitrest/src/commonMain/kotlin/me/jfenn/gitrest/impl/gitlab/model/GitlabRepo.kt
  21. +2
    -2
      gitrest/src/commonMain/kotlin/me/jfenn/gitrest/impl/gitlab/model/GitlabUser.kt
  22. +1
    -1
      gitrest/src/commonMain/kotlin/me/jfenn/gitrest/model/DelegateResource.kt
  23. +1
    -1
      gitrest/src/commonMain/kotlin/me/jfenn/gitrest/model/License.kt
  24. +1
    -1
      gitrest/src/commonMain/kotlin/me/jfenn/gitrest/model/ProviderString.kt
  25. +8
    -6
      gitrest/src/commonMain/kotlin/me/jfenn/gitrest/model/Repo.kt
  26. +1
    -1
      gitrest/src/commonMain/kotlin/me/jfenn/gitrest/model/User.kt
  27. +23
    -0
      gitrest/src/commonMain/kotlin/me/jfenn/gitrest/util/Serialization.kt
  28. +0
    -2
      gitrest/src/commonTest/kotlin/BasicTest.kt
  29. +1
    -3
      gitrest/src/commonTest/kotlin/ProviderStringTest.kt
  30. +5
    -5
      gitrest/src/jsMain/kotlin/RequestProvider.kt
  31. +2
    -2
      gitrest/src/jsMain/kotlin/gitrest/js/License.kt
  32. +2
    -2
      gitrest/src/jsMain/kotlin/gitrest/js/Repo.kt
  33. +2
    -2
      gitrest/src/jsMain/kotlin/gitrest/js/User.kt
  34. +2
    -2
      gradle/wrapper/gradle-wrapper.properties
  35. +1
    -0
      settings.gradle

+ 1
- 2
build.gradle View File

@@ -15,7 +15,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.6.3'
classpath 'com.android.tools.build:gradle:4.0.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
classpath "gradle.plugin.com.github.salomonbrys.gradle.kotlin.js:kotlin-js-npm-bundle:1.0.0"
@@ -29,6 +29,5 @@ allprojects {
repositories {
google()
jcenter()
}
}

+ 1
- 0
example-jvm/.gitignore View File

@@ -0,0 +1 @@
/build

+ 25
- 0
example-jvm/build.gradle View File

@@ -0,0 +1,25 @@
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'application'

compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}

compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}

mainClassName = "me.jfenn.gitrest.example.jvm.MainKt"

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7"

implementation project(':gitrest')
}

+ 33
- 0
example-jvm/src/main/java/me/jfenn/gitrest/example/jvm/Main.kt View File

@@ -0,0 +1,33 @@
package me.jfenn.gitrest.example.jvm

import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import me.jfenn.gitrest.RequestProviderDelegate
import me.jfenn.gitrest.impl.gitea.GiteaProvider
import me.jfenn.gitrest.impl.github.GithubProvider
import me.jfenn.gitrest.impl.gitlab.GitlabProvider

val delegate = RequestProviderDelegate(arrayOf(
GithubProvider,
GitlabProvider,
GiteaProvider
))

suspend fun fetchRepository(repoId: String) {
delegate.getRepo(repoId)?.let {
println("""
${it.slug}:
${it.description}
${it.url}
${it.websiteUrl}
""".trimIndent())
} ?: run {
println("ERROR: failed to fetch repo $repoId")
}
}

suspend fun main() {
fetchRepository("gitea@code.horrific.dev:james/git-rest-wrapper")
fetchRepository("github:fennifith/Attribouter")
fetchRepository("gitlab@salsa.debian.org:reproducible-builds/reproducible-website")
}

+ 4
- 4
example-kotlinbrowser/src/main/kotlin/Main.kt View File

@@ -1,7 +1,7 @@
import gitrest.RequestProviderDelegate
import gitrest.impl.gitea.GiteaProvider
import gitrest.impl.github.GithubProvider
import gitrest.impl.gitlab.GitlabProvider
import me.jfenn.gitrest.RequestProviderDelegate
import me.jfenn.gitrest.impl.gitea.GiteaProvider
import me.jfenn.gitrest.impl.github.GithubProvider
import me.jfenn.gitrest.impl.gitlab.GitlabProvider
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.html.InputType


+ 7
- 0
gitrest/build.gradle View File

@@ -5,6 +5,8 @@ apply plugin: 'maven-publish'
group 'gitrest'
version '0.0.1'

project.plugins.apply(org.jetbrains.kotlin.gradle.targets.js.npm.NpmResolverPlugin)

kotlin {
js {
compilations.main.kotlinOptions.moduleKind = 'umd'
@@ -25,6 +27,8 @@ kotlin {
}
}

jvm()

sourceSets {
commonMain {
dependencies {
@@ -90,3 +94,6 @@ kotlin {
}

task run dependsOn "check"

// this doesn't launch properly on Arch & always fails (unsure why, but it's lengthy & a bit heavy for CI testing regardless)
gradle.startParameter.excludedTaskNames += ["jsBrowserTest"]

gitrest/src/commonMain/kotlin/gitrest/RequestProviderDelegate.kt → gitrest/src/commonMain/kotlin/me/jfenn/gitrest/RequestProviderDelegate.kt View File

@@ -1,8 +1,9 @@
package gitrest
package me.jfenn.gitrest

import gitrest.base.RequestProvider
import gitrest.base.ServiceBuilder
import gitrest.model.*
import me.jfenn.gitrest.base.RequestProvider
import me.jfenn.gitrest.base.ServiceBuilder
import me.jfenn.gitrest.model.*
import me.jfenn.gitrest.model.DelegateResource

class RequestProviderDelegate(private val services: Array<ServiceBuilder<*>>) :
RequestProvider {

gitrest/src/commonMain/kotlin/gitrest/base/RequestProvider.kt → gitrest/src/commonMain/kotlin/me/jfenn/gitrest/base/RequestProvider.kt View File

@@ -1,8 +1,8 @@
package gitrest.base
package me.jfenn.gitrest.base

import gitrest.model.License
import gitrest.model.Repo
import gitrest.model.User
import me.jfenn.gitrest.model.License
import me.jfenn.gitrest.model.Repo
import me.jfenn.gitrest.model.User

interface RequestProvider {


gitrest/src/commonMain/kotlin/gitrest/base/ServiceBuilder.kt → gitrest/src/commonMain/kotlin/me/jfenn/gitrest/base/ServiceBuilder.kt View File

@@ -1,4 +1,4 @@
package gitrest.base
package me.jfenn.gitrest.base

import io.ktor.client.HttpClient
import io.ktor.client.HttpClientConfig

gitrest/src/commonMain/kotlin/gitrest/impl/gitea/GiteaProvider.kt → gitrest/src/commonMain/kotlin/me/jfenn/gitrest/impl/gitea/GiteaProvider.kt View File

@@ -1,10 +1,10 @@
package gitrest.impl.gitea
package me.jfenn.gitrest.impl.gitea

import gitrest.base.RequestProvider
import gitrest.base.ServiceBuilder
import gitrest.impl.gitea.model.GiteaRepo
import gitrest.impl.gitea.model.GiteaUser
import gitrest.model.License
import me.jfenn.gitrest.base.RequestProvider
import me.jfenn.gitrest.base.ServiceBuilder
import me.jfenn.gitrest.impl.gitea.model.GiteaRepo
import me.jfenn.gitrest.impl.gitea.model.GiteaUser
import me.jfenn.gitrest.model.License
import io.ktor.client.HttpClient
import io.ktor.client.features.defaultRequest
import io.ktor.client.request.get
@@ -29,7 +29,7 @@ class GiteaProvider(
val tokens: MutableMap<String, String> = HashMap()

override fun create(context: String?): GiteaProvider {
val client = gitrest.impl.github.GithubProvider.ktor {
val client = ktor {
defaultRequest {
url {
protocol = URLProtocol.HTTPS

gitrest/src/commonMain/kotlin/gitrest/impl/gitea/model/GiteaLicense.kt → gitrest/src/commonMain/kotlin/me/jfenn/gitrest/impl/gitea/model/GiteaLicense.kt View File

@@ -1,3 +1,3 @@
package gitrest.impl.gitea.model
package me.jfenn.gitrest.impl.gitea.model

// TODO: placeholder file - Gitea has no "license" implementation

gitrest/src/commonMain/kotlin/gitrest/impl/gitea/model/GiteaRepo.kt → gitrest/src/commonMain/kotlin/me/jfenn/gitrest/impl/gitea/model/GiteaRepo.kt View File

@@ -1,20 +1,25 @@
package gitrest.impl.gitea.model
package me.jfenn.gitrest.impl.gitea.model

import gitrest.model.Repo
import me.jfenn.gitrest.model.Repo
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import me.jfenn.gitrest.util.OptionalStringSerializer

@Serializable
class GiteaRepo : Repo() {

@SerialName("full_name")
override val slug: String? = null

@SerialName("description")
@Serializable(with = OptionalStringSerializer::class)
override val description: String? = null

@SerialName("html_url")
override val url: String? = null

@SerialName("website")
@Serializable(with = OptionalStringSerializer::class)
override val websiteUrl: String? = null

@SerialName("clone_url")

gitrest/src/commonMain/kotlin/gitrest/impl/gitea/model/GiteaUser.kt → gitrest/src/commonMain/kotlin/me/jfenn/gitrest/impl/gitea/model/GiteaUser.kt View File

@@ -1,8 +1,10 @@
package gitrest.impl.gitea.model
package me.jfenn.gitrest.impl.gitea.model

import gitrest.model.User
import me.jfenn.gitrest.model.User
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
class GiteaUser : User() {

@SerialName("login")

gitrest/src/commonMain/kotlin/gitrest/impl/github/GithubProvider.kt → gitrest/src/commonMain/kotlin/me/jfenn/gitrest/impl/github/GithubProvider.kt View File

@@ -1,10 +1,10 @@
package gitrest.impl.github
package me.jfenn.gitrest.impl.github

import gitrest.base.RequestProvider
import gitrest.base.ServiceBuilder
import gitrest.impl.github.model.GithubLicense
import gitrest.impl.github.model.GithubRepo
import gitrest.impl.github.model.GithubUser
import me.jfenn.gitrest.base.RequestProvider
import me.jfenn.gitrest.base.ServiceBuilder
import me.jfenn.gitrest.impl.github.model.GithubLicense
import me.jfenn.gitrest.impl.github.model.GithubRepo
import me.jfenn.gitrest.impl.github.model.GithubUser
import io.ktor.client.HttpClient
import io.ktor.client.features.defaultRequest
import io.ktor.client.request.get

gitrest/src/commonMain/kotlin/gitrest/impl/github/model/GithubLicense.kt → gitrest/src/commonMain/kotlin/me/jfenn/gitrest/impl/github/model/GithubLicense.kt View File

@@ -1,6 +1,6 @@
package gitrest.impl.github.model
package me.jfenn.gitrest.impl.github.model

import gitrest.model.License
import me.jfenn.gitrest.model.License
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable


gitrest/src/commonMain/kotlin/gitrest/impl/github/model/GithubRepo.kt → gitrest/src/commonMain/kotlin/me/jfenn/gitrest/impl/github/model/GithubRepo.kt View File

@@ -1,8 +1,9 @@
package gitrest.impl.github.model
package me.jfenn.gitrest.impl.github.model

import gitrest.model.Repo
import me.jfenn.gitrest.model.Repo
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import me.jfenn.gitrest.util.OptionalStringSerializer

@Serializable
class GithubRepo : Repo() {
@@ -11,12 +12,14 @@ class GithubRepo : Repo() {
override val slug: String? = null

@SerialName("description")
@Serializable(with = OptionalStringSerializer::class)
override val description: String? = null

@SerialName("html_url")
override val url: String? = null

@SerialName("homepage")
@Serializable(with = OptionalStringSerializer::class)
override val websiteUrl: String? = null

@SerialName("clone_url")

gitrest/src/commonMain/kotlin/gitrest/impl/github/model/GithubUser.kt → gitrest/src/commonMain/kotlin/me/jfenn/gitrest/impl/github/model/GithubUser.kt View File

@@ -1,6 +1,6 @@
package gitrest.impl.github.model
package me.jfenn.gitrest.impl.github.model

import gitrest.model.User
import me.jfenn.gitrest.model.User
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable


gitrest/src/commonMain/kotlin/gitrest/impl/gitlab/GitlabProvider.kt → gitrest/src/commonMain/kotlin/me/jfenn/gitrest/impl/gitlab/GitlabProvider.kt View File

@@ -1,10 +1,10 @@
package gitrest.impl.gitlab
package me.jfenn.gitrest.impl.gitlab

import gitrest.base.RequestProvider
import gitrest.base.ServiceBuilder
import gitrest.impl.gitlab.model.GitlabLicense
import gitrest.impl.gitlab.model.GitlabRepo
import gitrest.impl.gitlab.model.GitlabUser
import me.jfenn.gitrest.base.RequestProvider
import me.jfenn.gitrest.base.ServiceBuilder
import me.jfenn.gitrest.impl.gitlab.model.GitlabLicense
import me.jfenn.gitrest.impl.gitlab.model.GitlabRepo
import me.jfenn.gitrest.impl.gitlab.model.GitlabUser
import io.ktor.client.HttpClient
import io.ktor.client.features.defaultRequest
import io.ktor.client.request.get

gitrest/src/commonMain/kotlin/gitrest/impl/gitlab/model/GitlabLicense.kt → gitrest/src/commonMain/kotlin/me/jfenn/gitrest/impl/gitlab/model/GitlabLicense.kt View File

@@ -1,6 +1,6 @@
package gitrest.impl.gitlab.model
package me.jfenn.gitrest.impl.gitlab.model

import gitrest.model.License
import me.jfenn.gitrest.model.License
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable


gitrest/src/commonMain/kotlin/gitrest/impl/gitlab/model/GitlabRepo.kt → gitrest/src/commonMain/kotlin/me/jfenn/gitrest/impl/gitlab/model/GitlabRepo.kt View File

@@ -1,6 +1,6 @@
package gitrest.impl.gitlab.model
package me.jfenn.gitrest.impl.gitlab.model

import gitrest.model.Repo
import me.jfenn.gitrest.model.Repo
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable


gitrest/src/commonMain/kotlin/gitrest/impl/gitlab/model/GitlabUser.kt → gitrest/src/commonMain/kotlin/me/jfenn/gitrest/impl/gitlab/model/GitlabUser.kt View File

@@ -1,6 +1,6 @@
package gitrest.impl.gitlab.model
package me.jfenn.gitrest.impl.gitlab.model

import gitrest.model.User
import me.jfenn.gitrest.model.User
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable


gitrest/src/commonMain/kotlin/gitrest/model/DelegateResource.kt → gitrest/src/commonMain/kotlin/me/jfenn/gitrest/model/DelegateResource.kt View File

@@ -1,4 +1,4 @@
package gitrest.model
package me.jfenn.gitrest.model

import kotlinx.serialization.Transient


gitrest/src/commonMain/kotlin/gitrest/model/License.kt → gitrest/src/commonMain/kotlin/me/jfenn/gitrest/model/License.kt View File

@@ -1,4 +1,4 @@
package gitrest.model
package me.jfenn.gitrest.model

open class License : DelegateResource() {
open val key: String? = null

gitrest/src/commonMain/kotlin/gitrest/model/ProviderString.kt → gitrest/src/commonMain/kotlin/me/jfenn/gitrest/model/ProviderString.kt View File

@@ -1,4 +1,4 @@
package gitrest.model
package me.jfenn.gitrest.model

class ProviderString {


gitrest/src/commonMain/kotlin/gitrest/model/Repo.kt → gitrest/src/commonMain/kotlin/me/jfenn/gitrest/model/Repo.kt View File

@@ -1,4 +1,4 @@
package gitrest.model
package me.jfenn.gitrest.model

open class Repo : DelegateResource() {
open val slug: String? = null
@@ -13,11 +13,13 @@ open class Repo : DelegateResource() {
super.setId(id)
license?.apply {
key?.let { licenseKey ->
setId(ProviderString(
provider = id.provider,
context = id.context,
id = licenseKey
))
setId(
ProviderString(
provider = id.provider,
context = id.context,
id = licenseKey
)
)
}
}
}

gitrest/src/commonMain/kotlin/gitrest/model/User.kt → gitrest/src/commonMain/kotlin/me/jfenn/gitrest/model/User.kt View File

@@ -1,4 +1,4 @@
package gitrest.model
package me.jfenn.gitrest.model

open class User : DelegateResource() {
open val login: String? = null

+ 23
- 0
gitrest/src/commonMain/kotlin/me/jfenn/gitrest/util/Serialization.kt View File

@@ -0,0 +1,23 @@
package me.jfenn.gitrest.util

import kotlinx.serialization.*
import kotlinx.serialization.builtins.serializer

/**
* Custom kotlin.String serializer that returns empty/blank strings as null.
*/
open class OptionalStringSerializer : KSerializer<String?> {

final override val descriptor: SerialDescriptor = PrimitiveDescriptor("kotlin.OptionalString", PrimitiveKind.STRING)
private val valueSerializer = String.serializer()

final override fun deserialize(decoder: Decoder): String? {
val str = valueSerializer.deserialize(decoder)
return if (str.isNotBlank()) str else null
}

final override fun serialize(encoder: Encoder, value: String?) {
value?.let { valueSerializer.serialize(encoder, it) }
}

}

gitrest/src/jsTest/kotlin/gitrest/BasicTest.kt → gitrest/src/commonTest/kotlin/BasicTest.kt View File

@@ -1,5 +1,3 @@
package gitrest

import kotlin.test.Test

class BasicTest {

gitrest/src/commonTest/kotlin/gitrest/ProviderStringTest.kt → gitrest/src/commonTest/kotlin/ProviderStringTest.kt View File

@@ -1,6 +1,4 @@
package gitrest

import gitrest.model.ProviderString
import me.jfenn.gitrest.model.ProviderString
import kotlin.test.Test
import kotlin.test.assertEquals


+ 5
- 5
gitrest/src/jsMain/kotlin/RequestProvider.kt View File

@@ -1,8 +1,8 @@
import gitrest.RequestProviderDelegate
import gitrest.base.ServiceBuilder
import gitrest.impl.gitea.GiteaProvider
import gitrest.impl.github.GithubProvider
import gitrest.impl.gitlab.GitlabProvider
import me.jfenn.gitrest.RequestProviderDelegate
import me.jfenn.gitrest.base.ServiceBuilder
import me.jfenn.gitrest.impl.gitea.GiteaProvider
import me.jfenn.gitrest.impl.github.GithubProvider
import me.jfenn.gitrest.impl.gitlab.GitlabProvider
import gitrest.js.License
import gitrest.js.Repo
import gitrest.js.User


+ 2
- 2
gitrest/src/jsMain/kotlin/gitrest/js/License.kt View File

@@ -1,8 +1,8 @@
package gitrest.js

import gitrest.model.ProviderString
import me.jfenn.gitrest.model.ProviderString

class License(license: gitrest.model.License) {
class License(license: me.jfenn.gitrest.model.License) {
val id: ProviderString? = license.id
val key: String? = license.key
val name: String? = license.name


+ 2
- 2
gitrest/src/jsMain/kotlin/gitrest/js/Repo.kt View File

@@ -1,8 +1,8 @@
package gitrest.js

import gitrest.model.ProviderString
import me.jfenn.gitrest.model.ProviderString

class Repo(repo: gitrest.model.Repo) {
class Repo(repo: me.jfenn.gitrest.model.Repo) {
val id: ProviderString? = repo.id
val slug: String? = repo.slug
val description: String? = repo.description


+ 2
- 2
gitrest/src/jsMain/kotlin/gitrest/js/User.kt View File

@@ -1,8 +1,8 @@
package gitrest.js

import gitrest.model.ProviderString
import me.jfenn.gitrest.model.ProviderString

class User(user: gitrest.model.User) {
class User(user: me.jfenn.gitrest.model.User) {
val id: ProviderString? = user.id
val login: String? = user.login
val name: String? = user.name


+ 2
- 2
gradle/wrapper/gradle-wrapper.properties View File

@@ -1,6 +1,6 @@
#Sat May 09 17:04:07 EDT 2020
#Tue Jun 23 06:47:08 EDT 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip

+ 1
- 0
settings.gradle View File

@@ -5,3 +5,4 @@ include ':example-android'
include ':example-js'
include ':example-nodejs'
include ':example-kotlinbrowser'
include ':example-jvm'

Loading…
Cancel
Save