Skip to content

Commit

Permalink
[Feat]Add Article Api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Marvel999 committed Mar 14, 2021
1 parent d8b1980 commit 4c3a61a
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,13 @@ dependencies {
//kapt codegen make coding faster and add some auto gentated code
kapt("com.squareup.moshi:moshi-kotlin-codegen:1.11.0")

//Test
testImplementation 'junit:junit:4.13.2'

//Coroutines in kotlin

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1'



}
15 changes: 15 additions & 0 deletions api/src/main/java/com/example/api/ConduitClient.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.api

import com.example.api.service.ConduitApi
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory

class ConduitClient {
val retrofit=Retrofit.Builder()
.baseUrl("https://conduit.productionready.io/api/")
.addConverterFactory(MoshiConverterFactory.create())
.build()

val api=retrofit.create(ConduitApi::class.java)

}
2 changes: 1 addition & 1 deletion api/src/main/java/com/example/api/model/Profile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
data class Profile(
@Json(name = "bio")
val bio: String,
val bio: String?,
@Json(name = "following")
val following: Boolean,
@Json(name = "image")
Expand Down
12 changes: 12 additions & 0 deletions api/src/main/java/com/example/api/service/ConduitApi.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
package com.example.api.service

import com.example.api.model.ArticlesResponse
import retrofit2.Call
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Query

interface ConduitApi {
@GET("articles")
suspend fun getArticles(
@Query("author")author:String?=null,
@Query("tag")tag:List<String>?=null,
@Query("favorited")favorited:String?=null
):Response<ArticlesResponse>
}
44 changes: 44 additions & 0 deletions api/src/test/java/ConduitApiTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import com.example.api.ConduitClient
import com.example.api.service.ConduitApi
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertNotNull
import org.junit.Test
import java.util.*

class ConduitApiTest {

val conduitClient=ConduitClient()
@Test
fun getArticle(){
runBlocking {
val articles=conduitClient.api.getArticles()
assertNotNull(articles.body()?.articles)
}

}
@Test
fun `get Article by author`(){
runBlocking {
val articles=conduitClient.api.getArticles(author = "444")
assertNotNull(articles.body()?.articles)
}

}
@Test
fun `get Article by tag`(){
runBlocking {
val articles=conduitClient.api.getArticles(tag = Arrays.asList("js","dragon"))
assertNotNull(articles.body()?.articles)
}

}
@Test
fun `get Article by favorite`(){
runBlocking {
val articles=conduitClient.api.getArticles(favorited = "444")
assertNotNull(articles.body()?.articles)
}

}

}
18 changes: 18 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
}

android {
Expand Down Expand Up @@ -43,4 +44,21 @@ dependencies {
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
//Retrofit
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
//It is connecter between moshi and retrofit
implementation "com.squareup.retrofit2:converter-moshi:$retrofit_version"
//Moshi json converter
implementation "com.squareup.moshi:moshi-kotlin:$moshi_version"

//kapt codegen make coding faster and add some auto generated code
kapt("com.squareup.moshi:moshi-kotlin-codegen:1.11.0")

// Coroutines in kotlin
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1'

//Timber
implementation 'com.jakewharton.timber:timber:4.7.1'


}
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.blogoapp">

<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/com/example/blogoapp/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@ package com.example.blogoapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.example.api.ConduitClient
import kotlinx.coroutines.runBlocking

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val conduitClient=ConduitClient()
runBlocking {
val articels=conduitClient.api.getArticles()
Log.e("Article",""+articels.body()?.articles.toString())
}


}
}
8 changes: 6 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
#org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
Expand All @@ -16,4 +16,8 @@ org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
kotlin.code.style=official

#org.gradle.jvmargs=-XX\:MaxHeapSize\=256m -Xmx256m

org.gradle.jvmargs=-Xmx1024m

0 comments on commit 4c3a61a

Please sign in to comment.