Created
November 24, 2022 08:44
-
-
Save ibrahim-mrq/0720d95547ee8a1766a3def2f26b5480 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package com.intertech.e_bike_bethlehem.network.retrofit | |
| import android.content.Context | |
| import com.google.gson.Gson | |
| import com.google.gson.GsonBuilder | |
| import com.intertech.e_bike_bethlehem.R | |
| import okhttp3.OkHttpClient | |
| import okhttp3.logging.HttpLoggingInterceptor | |
| import retrofit2.Retrofit | |
| import retrofit2.converter.gson.GsonConverterFactory | |
| import java.util.concurrent.TimeUnit | |
| object RetrofitClient { | |
| var gson: Gson = GsonBuilder() | |
| .setLenient() | |
| .create() | |
| private var okHttpClient: OkHttpClient = OkHttpClient.Builder() | |
| .readTimeout(60, TimeUnit.SECONDS) | |
| .connectTimeout(60, TimeUnit.SECONDS) | |
| .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)) | |
| .build() | |
| fun makeRetrofitService(): RetrofitService { | |
| return Retrofit.Builder() | |
| .baseUrl(BASE_URL) | |
| .addConverterFactory(GsonConverterFactory.create(gson)) | |
| .client(okHttpClient) | |
| .build().create(RetrofitService::class.java) | |
| } | |
| fun returnErrorServer(context: Context, code: Int): String { | |
| val message = when (code) { | |
| 401, 403 -> { | |
| context.getString(R.string.Logged_from_another_device) + "$code" | |
| } | |
| 404 -> { | |
| context.getString(R.string.not_found) + "$code" | |
| } | |
| 429 -> { | |
| context.getString(R.string.many_request) + "$code" | |
| } | |
| 500 -> { | |
| context.getString(R.string.server_is_broker) + "$code" | |
| } | |
| 503 -> { | |
| context.getString(R.string.server_not_available) + "$code" | |
| } | |
| else -> { | |
| context.getString(R.string.error_unknown) + "$code" | |
| } | |
| } | |
| return message | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment