|
|
|
@ -1,6 +1,11 @@
|
|
|
|
|
package org.wntr.mdeditor
|
|
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.util.Log
|
|
|
|
|
import com.franmontiel.persistentcookiejar.ClearableCookieJar
|
|
|
|
|
import com.franmontiel.persistentcookiejar.PersistentCookieJar
|
|
|
|
|
import com.franmontiel.persistentcookiejar.cache.SetCookieCache
|
|
|
|
|
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor
|
|
|
|
|
import okhttp3.MultipartBody
|
|
|
|
|
import okhttp3.OkHttpClient
|
|
|
|
|
import okhttp3.ResponseBody
|
|
|
|
@ -15,111 +20,109 @@ import retrofit2.http.Multipart
|
|
|
|
|
import retrofit2.http.POST
|
|
|
|
|
import retrofit2.http.Part
|
|
|
|
|
|
|
|
|
|
class ghostAPI() {
|
|
|
|
|
companion object {
|
|
|
|
|
object RetrofitClient {
|
|
|
|
|
private val BASE_URL = credentials.instance + "/ghost/api/admin/"
|
|
|
|
|
fun getClient(applicationContext: Context): Retrofit {
|
|
|
|
|
/* Detailed logging
|
|
|
|
|
val loggingInterceptor = HttpLoggingInterceptor().apply {
|
|
|
|
|
this.setLevel(HttpLoggingInterceptor.Level.BASIC)
|
|
|
|
|
}*/
|
|
|
|
|
Log.i(javaClass.simpleName, "Retrofitclient baseurl: $BASE_URL")
|
|
|
|
|
val cookieJar: ClearableCookieJar =
|
|
|
|
|
PersistentCookieJar(SetCookieCache(), SharedPrefsCookiePersistor(applicationContext))
|
|
|
|
|
val okHttpClient = OkHttpClient()
|
|
|
|
|
.newBuilder()
|
|
|
|
|
/*
|
|
|
|
|
.addInterceptor(RequestInterceptor)
|
|
|
|
|
*/
|
|
|
|
|
/*
|
|
|
|
|
.addInterceptor(loggingInterceptor)
|
|
|
|
|
*/
|
|
|
|
|
.cookieJar(cookieJar)
|
|
|
|
|
.build()
|
|
|
|
|
|
|
|
|
|
return Retrofit.Builder()
|
|
|
|
|
.client(okHttpClient)
|
|
|
|
|
.baseUrl(BASE_URL)
|
|
|
|
|
.addConverterFactory(JacksonConverterFactory.create())
|
|
|
|
|
.build()
|
|
|
|
|
|
|
|
|
|
private val retrofit = RetrofitClient.getClient()
|
|
|
|
|
val postApi = retrofit.create(PostApi::class.java)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun register(credentials: Credentials): String {
|
|
|
|
|
return try {
|
|
|
|
|
val response = postApi.getCookie(credentials).execute()
|
|
|
|
|
if (!response.isSuccessful) {
|
|
|
|
|
Log.d(javaClass.simpleName, "Response code ${response.code()}")
|
|
|
|
|
when (response.code()) {
|
|
|
|
|
404 -> {
|
|
|
|
|
return "404"
|
|
|
|
|
}
|
|
|
|
|
class ghostAPI(applicationContext: Context) {
|
|
|
|
|
val retrofit = RetrofitClient.getClient(applicationContext)
|
|
|
|
|
val postApi = retrofit.create(PostApi::class.java)
|
|
|
|
|
|
|
|
|
|
fun register(credentials: Credentials): String {
|
|
|
|
|
return try {
|
|
|
|
|
val response = postApi.getCookie(credentials).execute()
|
|
|
|
|
if (!response.isSuccessful) {
|
|
|
|
|
Log.d(javaClass.simpleName, "Response code ${response.code()}")
|
|
|
|
|
when (response.code()) {
|
|
|
|
|
404 -> {
|
|
|
|
|
return "404"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
429 -> {
|
|
|
|
|
Log.d(javaClass.simpleName, "Too many failures")
|
|
|
|
|
return "TOO_MANY_FAILURES"
|
|
|
|
|
}
|
|
|
|
|
429 -> {
|
|
|
|
|
Log.d(javaClass.simpleName, "Too many failures")
|
|
|
|
|
return "TOO_MANY_FAILURES"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else -> {
|
|
|
|
|
val errors = JSONObject(response.errorBody()!!.string())
|
|
|
|
|
val code =
|
|
|
|
|
errors.getJSONArray("errors").getJSONObject(0).getString("code")
|
|
|
|
|
Log.d(javaClass.simpleName, "Error code $code")
|
|
|
|
|
if (code.equals("PASSWORD_INCORRECT")) {
|
|
|
|
|
Log.d(javaClass.simpleName, "Password incorrect")
|
|
|
|
|
}
|
|
|
|
|
if (code !== null) return code
|
|
|
|
|
else return response.code().toString()
|
|
|
|
|
else -> {
|
|
|
|
|
val errors = JSONObject(response.errorBody()!!.string())
|
|
|
|
|
val code =
|
|
|
|
|
errors.getJSONArray("errors").getJSONObject(0).getString("code")
|
|
|
|
|
Log.d(javaClass.simpleName, "Error code $code")
|
|
|
|
|
if (code.equals("PASSWORD_INCORRECT")) {
|
|
|
|
|
Log.d(javaClass.simpleName, "Password incorrect")
|
|
|
|
|
}
|
|
|
|
|
if (code !== null) return code
|
|
|
|
|
else return response.code().toString()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return "SUCCESS"
|
|
|
|
|
} catch (ex: Exception) {
|
|
|
|
|
Log.d(javaClass.simpleName, "Couldn't log in. Exception: ${ex}")
|
|
|
|
|
return "EXCEPTION"
|
|
|
|
|
}
|
|
|
|
|
return "SUCCESS"
|
|
|
|
|
} catch (ex: Exception) {
|
|
|
|
|
Log.d(javaClass.simpleName, "Couldn't log in. Exception: ${ex}")
|
|
|
|
|
return "EXCEPTION"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Detailed logging
|
|
|
|
|
|
|
|
|
|
/* object RequestInterceptor : Interceptor {
|
|
|
|
|
override fun intercept(chain: Interceptor.Chain): Response {
|
|
|
|
|
val request = chain.request()
|
|
|
|
|
val newRequest = request.newBuilder()
|
|
|
|
|
*//*.header("Authorization", "Ghost "+ API_KEY)*//*
|
|
|
|
|
object RequestInterceptor : Interceptor {
|
|
|
|
|
override fun intercept(chain: Interceptor.Chain): Response {
|
|
|
|
|
val request = chain.request()
|
|
|
|
|
val newRequest = request.newBuilder()
|
|
|
|
|
*//*.header("Authorization", "Ghost "+ API_KEY)*//*
|
|
|
|
|
.header("Origin", "https://mary.joefix.it")
|
|
|
|
|
|
|
|
|
|
.build()
|
|
|
|
|
|
|
|
|
|
Log.i(javaClass.simpleName, "Outgoing request to ${newRequest.url}")
|
|
|
|
|
//Log.i(javaClass.simpleName, newRequest.body.toString())
|
|
|
|
|
//Log.i(javaClass.simpleName, newRequest.body.toString())
|
|
|
|
|
return chain.proceed(newRequest)
|
|
|
|
|
}
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
object RetrofitClient {
|
|
|
|
|
private val BASE_URL = credentials.instance + "/ghost/api/admin/"
|
|
|
|
|
//"https://mary.joefix.it/ghost/api/admin/"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun getClient(): Retrofit {
|
|
|
|
|
/*val loggingInterceptor = HttpLoggingInterceptor().apply {
|
|
|
|
|
this.setLevel(HttpLoggingInterceptor.Level.BASIC)
|
|
|
|
|
}*/
|
|
|
|
|
Log.i(javaClass.simpleName, "Retrofitclient baseurl: $BASE_URL")
|
|
|
|
|
val okHttpClient = OkHttpClient()
|
|
|
|
|
.newBuilder()
|
|
|
|
|
/*
|
|
|
|
|
.addInterceptor(RequestInterceptor)
|
|
|
|
|
*/
|
|
|
|
|
/*
|
|
|
|
|
.addInterceptor(loggingInterceptor)
|
|
|
|
|
*/
|
|
|
|
|
.cookieJar(MyCookieJar())
|
|
|
|
|
.build()
|
|
|
|
|
|
|
|
|
|
return Retrofit.Builder()
|
|
|
|
|
.client(okHttpClient)
|
|
|
|
|
.baseUrl(BASE_URL)
|
|
|
|
|
.addConverterFactory(JacksonConverterFactory.create())
|
|
|
|
|
.build()
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface PostApi {
|
|
|
|
|
@GET("posts")
|
|
|
|
|
fun getPosts(): Call<posts>
|
|
|
|
|
/* fun getPosts(): Call<ResponseBody> <- for json string*/
|
|
|
|
|
interface PostApi {
|
|
|
|
|
@GET("posts")
|
|
|
|
|
fun getPosts(): Call<posts>
|
|
|
|
|
/* fun getPosts(): Call<ResponseBody> <- for json string*/
|
|
|
|
|
|
|
|
|
|
@POST("session")
|
|
|
|
|
fun getCookie(@Body credentials: Credentials): Call<ResponseBody>
|
|
|
|
|
@POST("session")
|
|
|
|
|
fun getCookie(@Body credentials: Credentials): Call<ResponseBody>
|
|
|
|
|
|
|
|
|
|
@POST("posts/?source=html")
|
|
|
|
|
fun pushPost(@Body postings: sendPostList): Call<posts>
|
|
|
|
|
@POST("posts/?source=html")
|
|
|
|
|
fun pushPost(@Body postings: sendPostList): Call<posts>
|
|
|
|
|
|
|
|
|
|
@Multipart
|
|
|
|
|
@POST("images/upload")
|
|
|
|
|
fun pushImage(@Part file: MultipartBody.Part): Call<imagesObj>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@Multipart
|
|
|
|
|
@POST("images/upload")
|
|
|
|
|
fun pushMyImage(@Part file: MultipartBody.Part): Call<imagesObj>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|