-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from dedis/http-service
Add the backend connection to register new users
- Loading branch information
Showing
38 changed files
with
495 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
android/app/src/main/java/com/epfl/dedis/hbt/data/document/Document.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.epfl.dedis.hbt.data.document | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
|
||
data class Document( | ||
@JsonProperty("doc_id") val id: String | ||
) |
28 changes: 28 additions & 0 deletions
28
android/app/src/main/java/com/epfl/dedis/hbt/data/document/Portrait.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.epfl.dedis.hbt.data.document | ||
|
||
import java.io.Serializable | ||
|
||
data class Portrait( | ||
val type: String, | ||
val data: ByteArray | ||
) : Serializable { | ||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (javaClass != other?.javaClass) return false | ||
|
||
other as Portrait | ||
|
||
if (type != other.type) return false | ||
return data.contentEquals(other.data) | ||
} | ||
|
||
override fun hashCode(): Int { | ||
var result = type.hashCode() | ||
result = 31 * result + data.contentHashCode() | ||
return result | ||
} | ||
|
||
override fun toString(): String { | ||
return "Portrait(type='$type', data=${data.contentToString()})" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
android/app/src/main/java/com/epfl/dedis/hbt/di/HttpModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.epfl.dedis.hbt.di | ||
|
||
import com.epfl.dedis.hbt.service.document.DocumentEndpoint | ||
import com.epfl.dedis.hbt.service.http.ResultCallAdapterFactory | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.jackson.JacksonConverterFactory | ||
import javax.inject.Qualifier | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object HttpModule { | ||
|
||
@BaseURL | ||
@Provides | ||
@Singleton | ||
fun provideBaseURL() = "http://10.0.2.2:3000" | ||
|
||
@Provides | ||
@Singleton | ||
fun provideRetrofit(@BaseURL baseUrl: String, mapper: ObjectMapper): Retrofit = | ||
Retrofit.Builder() | ||
.baseUrl(baseUrl) | ||
.addConverterFactory(JacksonConverterFactory.create(mapper)) | ||
.addCallAdapterFactory(ResultCallAdapterFactory()) | ||
.build() | ||
|
||
@Provides | ||
@Singleton | ||
fun provideDocumentService(retrofit: Retrofit): DocumentEndpoint = | ||
retrofit.create(DocumentEndpoint::class.java) | ||
|
||
@Qualifier | ||
@Retention(AnnotationRetention.BINARY) | ||
annotation class BaseURL | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
android/app/src/main/java/com/epfl/dedis/hbt/service/document/DocumentEndpoint.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.epfl.dedis.hbt.service.document | ||
|
||
import com.epfl.dedis.hbt.data.Result | ||
import com.epfl.dedis.hbt.data.document.Document | ||
import okhttp3.MultipartBody | ||
import retrofit2.http.Multipart | ||
import retrofit2.http.POST | ||
import retrofit2.http.Part | ||
|
||
interface DocumentEndpoint { | ||
|
||
@Multipart | ||
@POST("document") | ||
suspend fun create( | ||
@Part("name") name: String, | ||
@Part("passport") passport: String, | ||
@Part("role") role: Int, | ||
@Part portrait: MultipartBody.Part, | ||
@Part("registered") registered: Boolean, | ||
): Result<Document> | ||
} |
26 changes: 26 additions & 0 deletions
26
android/app/src/main/java/com/epfl/dedis/hbt/service/document/DocumentService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.epfl.dedis.hbt.service.document | ||
|
||
import com.epfl.dedis.hbt.data.Result | ||
import com.epfl.dedis.hbt.data.document.Document | ||
import com.epfl.dedis.hbt.data.document.Portrait | ||
import com.epfl.dedis.hbt.data.user.User | ||
import okhttp3.MediaType | ||
import okhttp3.MultipartBody | ||
import okhttp3.RequestBody | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class DocumentService @Inject constructor(private val endpoint: DocumentEndpoint) { | ||
|
||
suspend fun create(user: User, portrait: Portrait, registered: Boolean): Result<Document> = | ||
endpoint.create( | ||
user.name, | ||
user.passport, | ||
user.role.ordinal, | ||
MultipartBody.Part.createFormData("portrait", | ||
"portrait.png", | ||
RequestBody.create(MediaType.parse(portrait.type), portrait.data)), | ||
registered | ||
) | ||
} |
80 changes: 80 additions & 0 deletions
80
android/app/src/main/java/com/epfl/dedis/hbt/service/http/ResultCall.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.epfl.dedis.hbt.service.http | ||
|
||
import com.epfl.dedis.hbt.data.Result | ||
import okhttp3.Request | ||
import okio.Timeout | ||
import retrofit2.Call | ||
import retrofit2.Callback | ||
import retrofit2.HttpException | ||
import retrofit2.Response | ||
import java.io.IOException | ||
|
||
class ResultCall<T : Any>(private val delegate: Call<T>) : Call<Result<T>> { | ||
override fun enqueue(callback: Callback<Result<T>>) { | ||
delegate.enqueue( | ||
object : Callback<T> { | ||
override fun onResponse(call: Call<T>, response: Response<T>) { | ||
if (response.isSuccessful) { | ||
callback.onResponse( | ||
this@ResultCall, | ||
Response.success( | ||
response.code(), | ||
Result.Success(response.body()!!) | ||
) | ||
) | ||
} else { | ||
callback.onResponse( | ||
this@ResultCall, | ||
Response.success( | ||
Result.Error( | ||
HttpException(response) | ||
) | ||
) | ||
) | ||
} | ||
} | ||
|
||
override fun onFailure(call: Call<T>, t: Throwable) { | ||
val errorMessage = when (t) { | ||
is IOException -> "No internet connection" | ||
is HttpException -> "Something went wrong!" | ||
else -> t.localizedMessage | ||
} | ||
callback.onResponse( | ||
this@ResultCall, | ||
Response.success(Result.Error(RuntimeException(errorMessage, t))) | ||
) | ||
} | ||
} | ||
) | ||
} | ||
|
||
override fun isExecuted(): Boolean { | ||
return delegate.isExecuted | ||
} | ||
|
||
override fun execute(): Response<Result<T>> { | ||
return Response.success(Result.Success(delegate.execute().body()!!)) | ||
} | ||
|
||
override fun cancel() { | ||
delegate.cancel() | ||
} | ||
|
||
override fun isCanceled(): Boolean { | ||
return delegate.isCanceled | ||
} | ||
|
||
override fun clone(): Call<Result<T>> { | ||
return ResultCall(delegate.clone()) | ||
} | ||
|
||
override fun request(): Request { | ||
return delegate.request() | ||
} | ||
|
||
override fun timeout(): Timeout { | ||
return delegate.timeout() | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
android/app/src/main/java/com/epfl/dedis/hbt/service/http/ResultCallAdapterFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.epfl.dedis.hbt.service.http | ||
|
||
import com.epfl.dedis.hbt.data.Result | ||
import retrofit2.Call | ||
import retrofit2.CallAdapter | ||
import retrofit2.Retrofit | ||
import java.lang.reflect.ParameterizedType | ||
import java.lang.reflect.Type | ||
|
||
class ResultCallAdapterFactory : CallAdapter.Factory() { | ||
override fun get( | ||
returnType: Type, | ||
annotations: Array<out Annotation>, | ||
retrofit: Retrofit | ||
): CallAdapter<*, *>? { | ||
if (getRawType(returnType) != Call::class.java || returnType !is ParameterizedType) { | ||
return null | ||
} | ||
val upperBound = getParameterUpperBound(0, returnType) | ||
|
||
return if (upperBound is ParameterizedType && upperBound.rawType == Result::class.java) { | ||
object : CallAdapter<Any, Call<Result<*>>> { | ||
override fun responseType(): Type = getParameterUpperBound(0, upperBound) | ||
|
||
override fun adapt(call: Call<Any>): Call<Result<*>> = ResultCall(call) | ||
} | ||
} else { | ||
null | ||
} | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
android/app/src/main/java/com/epfl/dedis/hbt/service/passport/Passport.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
package com.epfl.dedis.hbt.service.passport | ||
|
||
import com.epfl.dedis.hbt.data.document.Portrait | ||
import com.epfl.dedis.hbt.service.passport.mrz.MRZInfo | ||
import org.jmrtd.lds.SODFile | ||
import org.jmrtd.lds.icao.DG11File | ||
|
||
data class Passport( | ||
val mrzInfo: MRZInfo, | ||
val sodFile: SODFile, | ||
val portrait: Portrait, | ||
val dg11File: DG11File? | ||
) |
Oops, something went wrong.