Skip to content

Commit 432fced

Browse files
authored
Merge pull request #23 from FarshidRoohi/master
change request
2 parents a1d1b26 + c46c214 commit 432fced

File tree

11 files changed

+202
-206
lines changed

11 files changed

+202
-206
lines changed

build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
buildscript {
4-
4+
ext.kotlin_version = '1.2.61'
5+
56
repositories {
67
google()
78
jcenter()
@@ -10,6 +11,7 @@ buildscript {
1011
classpath 'com.android.tools.build:gradle:3.1.4'
1112
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.2'
1213
classpath "com.github.dcendents:android-maven-gradle-plugin:2.0"
14+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1315
}
1416
}
1517

qlroid/build.gradle

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
apply plugin: 'com.android.library'
2+
apply plugin: 'kotlin-android'
23
apply plugin: 'com.jfrog.bintray'
34
apply plugin: 'com.github.dcendents.android-maven'
45

56
android {
6-
compileSdkVersion 26
7+
compileSdkVersion 28
78

89
defaultConfig {
910
minSdkVersion 14
10-
targetSdkVersion 26
11+
targetSdkVersion 28
1112

1213
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1314

@@ -20,9 +21,13 @@ android {
2021
}
2122
}
2223

24+
sourceSets {
25+
main.java.srcDirs += 'src/main/kotlin'
26+
}
27+
2328
}
2429

25-
def versionLib = '0.0.17'
30+
def versionLib = '0.0.18'
2631

2732
group = 'com.zarinpal'
2833
version = versionLib
@@ -76,8 +81,15 @@ bintray {
7681
dependencies {
7782
implementation fileTree(include: ['*.jar'], dir: 'libs')
7883
testImplementation 'junit:junit:4.12'
79-
implementation 'com.zarinpal:httprequest:1.0.1'
8084
implementation 'com.android.support:support-annotations:27.1.1'
81-
implementation 'com.google.code.gson:gson:2.8.5'
85+
implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0'
86+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
87+
8288

8389
}
90+
tasks.withType(Javadoc).all {
91+
enabled = false
92+
}
93+
repositories {
94+
mavenCentral()
95+
}

qlroid/src/main/java/com/github/imanx/QLroid/GraphCore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import com.github.imanx.QLroid.annonations.SerializedField;
77
import com.github.imanx.QLroid.annonations.UnInject;
8-
import com.github.imanx.QLroid.request.Argument;
8+
import com.github.imanx.QLroid.http.Argument;
99

1010
import org.json.JSONObject;
1111

qlroid/src/main/java/com/github/imanx/QLroid/GraphModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import com.github.imanx.QLroid.annonations.SerializedField;
66
import com.github.imanx.QLroid.annonations.UnInject;
7-
import com.github.imanx.QLroid.request.Argument;
7+
import com.github.imanx.QLroid.http.Argument;
88

99
import java.lang.reflect.Field;
1010

qlroid/src/main/java/com/github/imanx/QLroid/Mutation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import android.util.Log;
55

6-
import com.github.imanx.QLroid.request.Argument;
6+
import com.github.imanx.QLroid.http.Argument;
77

88
import org.json.JSONObject;
99

qlroid/src/main/java/com/github/imanx/QLroid/request/Argument.java renamed to qlroid/src/main/java/com/github/imanx/QLroid/http/Argument.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.github.imanx.QLroid.request;
1+
package com.github.imanx.QLroid.http;
22

33
import com.github.imanx.QLroid.argument.Arg;
44

qlroid/src/main/java/com/github/imanx/QLroid/request/Header.java renamed to qlroid/src/main/java/com/github/imanx/QLroid/http/Header.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
package com.github.imanx.QLroid.request;
1+
package com.github.imanx.QLroid.http;
22

3-
import java.util.ArrayList;
43
import java.util.HashMap;
5-
import java.util.List;
64
import java.util.Map;
75

86
/**
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
package com.github.imanx.QLroid.http
2+
3+
4+
import android.content.Context
5+
import android.net.Uri
6+
import android.os.Looper
7+
import android.util.Log
8+
import com.github.imanx.QLroid.GraphCore
9+
import com.github.imanx.QLroid.Mutation
10+
import com.github.imanx.QLroid.Query
11+
import com.github.imanx.QLroid.callback.Callback
12+
import com.github.imanx.QLroid.utility.Utility
13+
import okhttp3.MediaType
14+
import okhttp3.OkHttpClient
15+
import okhttp3.RequestBody
16+
import org.json.JSONException
17+
import org.json.JSONObject
18+
import java.io.IOException
19+
20+
21+
/**
22+
* Created by Farshid Roohi.
23+
* QLroid | Copyrights 2018 ZarinPal Crop.
24+
*/
25+
26+
class Request private constructor(private val builder: Builder) {
27+
private var callback: Callback? = null
28+
29+
30+
fun enqueue(callback: Callback) {
31+
this.callback = callback
32+
enqueue()
33+
}
34+
35+
private fun enqueue() {
36+
37+
if (this.builder.graphCore == null) {
38+
return
39+
}
40+
41+
val jsonObject = JSONObject()
42+
43+
try {
44+
45+
jsonObject.put("operationName", null)
46+
jsonObject.put("query", builder.graphCore!!.query)
47+
48+
builder.graphCore.let {
49+
jsonObject.put("variables", builder.graphCore!!.variables)
50+
}
51+
52+
} catch (e: JSONException) {
53+
e.printStackTrace()
54+
}
55+
56+
val okHttpClient = OkHttpClient()
57+
val request = okhttp3.Request.Builder()
58+
for ((key, value) in this.builder.getHeader()!!.map) {
59+
request.addHeader(key, value)
60+
}
61+
request.url(this.builder.uri!!.toString())
62+
63+
val type = MediaType.parse("application/json; charset=utf-8")
64+
val requestBody = RequestBody.create(type, jsonObject.toString())
65+
request.post(requestBody)
66+
67+
68+
okHttpClient.newCall(request.build()).enqueue(object : okhttp3.Callback {
69+
override fun onResponse(call: okhttp3.Call, response: okhttp3.Response) {
70+
71+
val responseRequest = response.body()!!.string()
72+
73+
if (!response.isSuccessful) {
74+
if (callback != null) callback!!.onFailure(response.code(), response.message())
75+
return
76+
}
77+
78+
val model = builder.graphCore!!.model
79+
val masterKey = if (model == null) builder.graphCore!!.operationName else model.responseModelName
80+
val wrappedJson = Utility.getWrappedJson(JSONObject(responseRequest), masterKey)
81+
82+
if (callback == null) {
83+
return
84+
}
85+
86+
val handler = android.os.Handler(Looper.getMainLooper())
87+
handler.post {
88+
when {
89+
wrappedJson == null -> callback!!.onResponse(responseRequest)
90+
model == null -> callback!!.onResponse(wrappedJson)
91+
else -> {
92+
val result = Utility.refactor(model.javaClass, wrappedJson)
93+
callback!!.onResponse(result)
94+
}
95+
}
96+
97+
}
98+
99+
100+
}
101+
102+
override fun onFailure(call: okhttp3.Call, e: IOException) {
103+
Log.i("LOG:::", "ERROR : ${e.message}")
104+
val handler = android.os.Handler(Looper.getMainLooper())
105+
handler.post {
106+
if (callback != null) callback!!.onFailure(e.hashCode(), e.message)
107+
}
108+
}
109+
})
110+
111+
}
112+
113+
// Builder segment
114+
115+
class Builder {
116+
117+
var context: Context? = null
118+
private set
119+
var uri: Uri? = null
120+
private set
121+
var graphCore: GraphCore? = null
122+
private set
123+
private var header: Header? = null
124+
private var timeout: Int = 0
125+
126+
constructor(context: Context, uri: Uri, query: Query) {
127+
this.uri = uri
128+
this.graphCore = query
129+
this.context = context
130+
}
131+
132+
constructor(context: Context, uri: Uri, mutation: Mutation) {
133+
this.uri = uri
134+
this.graphCore = mutation
135+
this.context = context
136+
}
137+
138+
constructor(context: Context, uri: Uri) {
139+
this.uri = uri
140+
this.context = context
141+
}
142+
143+
fun setGraph(graph: GraphCore): Builder {
144+
this.graphCore = graph
145+
return this
146+
}
147+
148+
fun setHeader(header: Header): Builder {
149+
this.header = header
150+
return this
151+
}
152+
153+
fun setTimeout(sec: Int): Builder {
154+
this.timeout = sec
155+
return this
156+
}
157+
158+
fun getHeader(): Header? {
159+
return this.header
160+
}
161+
162+
fun build(): Request {
163+
return Request(this)
164+
}
165+
166+
fun getTimeout(): Int {
167+
return this.timeout
168+
}
169+
}
170+
}

0 commit comments

Comments
 (0)