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

Fix #8 - Fix Marvel Api Authorization #9

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ The `Kotlin` plugin for Android Studio is also required.
# Code Enhancements:

1. Add Dagger2
2. create a module for layer data and domain



## Sources
Expand Down
4 changes: 3 additions & 1 deletion buildsystem/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ ext {
//data dependencies version
retrofitVersion = '2.3.0'
retrofitConverterGsonVersion = '2.3.0'
okhttpVersion = '3.8.1'

//test dependencies
mockitoVersion = '2.10.0'
Expand Down Expand Up @@ -50,7 +51,8 @@ ext {
dataDependencies = [
retrofit : "com.squareup.retrofit2:retrofit:${retrofitVersion}",
retrofitConverter : "com.squareup.retrofit2:converter-gson:${retrofitConverterGsonVersion}",

okhttp : "com.squareup.okhttp3:okhttp:${okhttpVersion}",
okhttpLoggingInterceptor: "com.squareup.okhttp3:logging-interceptor:${okhttpVersion}",
]


Expand Down
10 changes: 6 additions & 4 deletions data/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ android {
}
buildTypes {
debug {
buildConfigField "String", "PRIVATE_API_KEY_VALUE", '"d6e634e314a819ca1c0f0078344acb70"'
buildConfigField "String", "PUBLIC_API_KEY_VALUE", '"504e22423d24b530dd878fdf808fc001"'
buildConfigField "String", "PRIVATE_API_KEY_VALUE", '"your-private-apikey"'
buildConfigField "String", "PUBLIC_API_KEY_VALUE", '"your-private-apikey"'
buildConfigField "String", "MARVEL_BASE_URL", '"http://gateway.marvel.com/public/"'

}
Expand Down Expand Up @@ -53,8 +53,10 @@ dependencies {
testImplementation generalTestDependencies.junit,
generalTestDependencies.kotlinTest

compile dataDependencies.retrofit,
dataDependencies.retrofitConverter
implementation dataDependencies.retrofit,
dataDependencies.retrofitConverter,
dataDependencies.okhttp,
dataDependencies.okhttpLoggingInterceptor

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.puzzlebench.cmk.domain.service.CharacterServices
import io.reactivex.Observable


class CharacterServicesImpl(private val api: MarvelResquestGenerator = MarvelResquestGenerator(), private val mapper: CharacterMapperService = CharacterMapperService()) : CharacterServices {
class CharacterServicesImpl(private val api: MarvelRequestGenerator = MarvelRequestGenerator(), private val mapper: CharacterMapperService = CharacterMapperService()) : CharacterServices {
override fun getCharacters(): Observable<List<Character>> {
return Observable.create { subscriber ->
val callResponse = api.createService(MarvelApi::class.java).getCharacter()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.puzzlebench.cmk.data.service

import com.puzzlebench.cmk.data.BuildConfig
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.security.NoSuchAlgorithmException


class MarvelRequestGenerator {
private val QUERY_PARAMETER_KEY_HASH = "hash"
private val QUERY_PARAMETER_KEY_API_KEY = "apikey"
private val QUERY_PARAMETER_KEY_TS = "ts"
private val builder = Retrofit.Builder()
.baseUrl(BuildConfig.MARVEL_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())

private fun getHttpClient() : OkHttpClient {
// TODO: add proper environment configuration
return OkHttpClient.Builder()
.addInterceptor { chain ->
val defaultRequest = chain.request()
val timestamp = System.currentTimeMillis().toString()
val defaultHttpUrl = defaultRequest.url()
val httpUrl = defaultHttpUrl.newBuilder()
.addQueryParameter(QUERY_PARAMETER_KEY_API_KEY, BuildConfig.PUBLIC_API_KEY_VALUE)
.addQueryParameter(
QUERY_PARAMETER_KEY_HASH,
md5(timestamp + BuildConfig.PRIVATE_API_KEY_VALUE + BuildConfig.PUBLIC_API_KEY_VALUE)
)
.addQueryParameter(QUERY_PARAMETER_KEY_TS, timestamp)
.build()

val requestBuilder = defaultRequest.newBuilder().url(httpUrl)
chain.proceed(requestBuilder.build())
}
.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.build()
}

fun <S> createService(serviceClass: Class<S>): S {
val retrofit = builder.client(getHttpClient()).build()
return retrofit.create(serviceClass)
}

fun md5(s: String): String {
val MD5 = "MD5"
try {
// Create MD5 Hash
val digest = java.security.MessageDigest
.getInstance(MD5)
digest.update(s.toByteArray())
val messageDigest = digest.digest()

// Create Hex String
val hexString = StringBuilder()
for (aMessageDigest in messageDigest) {
var h = Integer.toHexString(0xFF and aMessageDigest.toInt())
while (h.length < 2)
h = "0$h"
hexString.append(h)
}
return hexString.toString()

} catch (e: NoSuchAlgorithmException) {
e.printStackTrace()
}

return ""
}
}

This file was deleted.