The goal of the project is help developers to use the twitter API
This project brings to table set of best practices, tools, and solutions:
- 100% Kotlin
- Testing
In progress
- Dependency Injection
- Static code analyse
In progress
- CI integration by gihub actions
In progress
- Tech-stack
- Kotlin + Coroutines - perform background operations
- Koin - dependency injection
- Retrofit Lib used to make call to Twitter API
- Timber - logging
- security-crypto - deal with encrypt content
- Test - tests
- Architecture
- Abstract Factory
- Tests
- Unit Tests (JUnit)
- core-testing - used to sync background tasks
- kotlinx-coroutines-test - used to sync coroutines jobs
- mockk - mock objects using kotlin's style
- Static code Analyse
- Klint - An anti-bikeshedding Kotlin linter with built-in formatter.
- Gradle
This project cover only the api standard Search Tweets. It is expected in the future to implement other API.
In order to the library works properly you must create a new file named twitter.properties on root folder with the key baseUrl.
// file project/twitter.properties
baseUrl="https://api.twitter.com/"
There are a few ways to open this project.
// latest stable
twitterktx_version = '1.0.0-beta-1'
- Android Studio -> File -> New -> From Version control -> Git
- Enter
https://github.com/thiagoolsilva/twitterKtx
into URL field
- Run
git clone https://github.com/thiagoolsilva/twitterKtx
- With your project opened, go to settings.gradle file and insert the follow code
include ':twitterktx'
project(":twitterktx").projectDir = new File('TWITTERKTX_FOLDER')
- Finally, go to your preference module and insert the follow code to use the library on it
implementation project(path: ':twitterktx')
Init the twitterKtx in an application Android class.
There is two approach to do it on Application class.
- If you Koin as your dependency inject, you must use the code
TwitterKtx.initKoinDependencies()
after callstartKoin{}
.
override fun onCreate() {
startKoin{
...
}
TwitterKtx.initKoinDependencies()
}
You can check it out this code on Application
- If you don't use Koin as your dependency inject, you must use the code
TwitterKtx.init(this@Application)
.
override fun onCreate() {
super.onCreate()
TwitterKtx.init(this@Application)
}
You can check it out this code on Application
- First of all, you must get a valid oauth2bearer token running the follow
curl
code.
curl -X POST \
https://api.twitter.com/oauth2/token \
-H 'authorization: Basic INSERT_YOUR_TOKEN_HERE' \
-H 'cache-control: no-cache'
-H 'content-type: application/x-www-form-urlencoded;charset=UTF-8'
The INSERT_YOUR_TOKEN_HERE is a token in base64 composed by consumer key and secret. For more details go to [twitter](https://developer.twitter.com/en/docs/basics/authentication/oauth-2-0)
It will return something like this:
{
"token_type": "bearer",
"access_token": "your_oauth2_token"
}
- Provide a valid oauth2 bearer token to library calling the function
TwitterKtx.configToken(tokenAuth)
For more details about it you can go to FirstFragment
- As the library uses coroutines, you must create a coroutine context to use it. In order to exemplify you can do it on Fragment as shown below.
viewModelScope.launch {
...
}
Don't forget to get the library implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$2.3.0-alpha02"
- Create a new instance of API library
val standardSearchTweet = SearchTweetFactory().createStandardApi<StandardSearchTweetV1>(SearchTweetFactory.ApiType.V1)
- Call the API providing the required and optional parameters.
val tweets = standardSearchTweet.searchTweet(
query, mutableListOf(
StandardSearchTweetV1Api.COUNT to 20
)
)
- Get the results. Happy coding! :D
This section is related to the features/bug fixes of project.
I'd love if you contribute with the upcoming features or bug fixes. Go ahead and read the CONTRIBUTING file.
You can check it out for new features on github.
Follow me
Copyright (c) 2020 Thiago Lopes da Silva
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
`