-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #8 - Fix Marvel Api Authorization
- Loading branch information
Showing
4 changed files
with
75 additions
and
38 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
72 changes: 72 additions & 0 deletions
72
data/src/main/java/com/puzzlebench/cmk/data/service/MarvelRequestGenerator.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,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 "" | ||
} | ||
} |
37 changes: 0 additions & 37 deletions
37
data/src/main/java/com/puzzlebench/cmk/data/service/MarvelResquestGenerator.kt
This file was deleted.
Oops, something went wrong.