Added support for coupons
Restructuring api service calls
This commit is contained in:
parent
2746f910f1
commit
d90fa02cd1
@ -41,9 +41,10 @@ android {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
||||||
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
|
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
|
||||||
implementation 'com.android.support:appcompat-v7:28.0.0'
|
implementation 'com.android.support:appcompat-v7:28.0.0'
|
||||||
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
|
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
|
||||||
|
implementation 'com.android.support:design:28.0.0'
|
||||||
testImplementation 'junit:junit:4.12'
|
testImplementation 'junit:junit:4.12'
|
||||||
androidTestImplementation 'com.android.support.test:runner:1.0.2'
|
androidTestImplementation 'com.android.support.test:runner:1.0.2'
|
||||||
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
|
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
|
||||||
|
|||||||
@ -11,7 +11,11 @@
|
|||||||
android:roundIcon="@mipmap/ic_launcher_round"
|
android:roundIcon="@mipmap/ic_launcher_round"
|
||||||
android:supportsRtl="true"
|
android:supportsRtl="true"
|
||||||
android:theme="@style/AppTheme">
|
android:theme="@style/AppTheme">
|
||||||
|
<activity
|
||||||
|
android:name=".ui.ShopActivity"
|
||||||
|
android:label="@string/title_activity_shop"
|
||||||
|
android:theme="@style/AppTheme.NoActionBar">
|
||||||
|
</activity>
|
||||||
<activity android:name=".MainActivity">
|
<activity android:name=".MainActivity">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN"/>
|
<action android:name="android.intent.action.MAIN"/>
|
||||||
|
|||||||
@ -8,6 +8,7 @@ import kotlinx.android.synthetic.main.activity_main.*
|
|||||||
import me.gilo.woodroid.Woocommerce
|
import me.gilo.woodroid.Woocommerce
|
||||||
import me.gilo.woodroid.Woocommerce.Builder
|
import me.gilo.woodroid.Woocommerce.Builder
|
||||||
import me.gilo.woodroid.callback.Status
|
import me.gilo.woodroid.callback.Status
|
||||||
|
import me.gilo.woodroid.models.Coupon
|
||||||
import me.gilo.woodroid.models.Product
|
import me.gilo.woodroid.models.Product
|
||||||
import java.util.ArrayList
|
import java.util.ArrayList
|
||||||
import retrofit2.Call
|
import retrofit2.Call
|
||||||
@ -29,16 +30,40 @@ class MainActivity : AppCompatActivity() {
|
|||||||
.setConsumerSecret("cs_062e8e3a7ae0ce08fdebc0c39f8f834d5e87598e")
|
.setConsumerSecret("cs_062e8e3a7ae0ce08fdebc0c39f8f834d5e87598e")
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
|
// tvText.append("\n")
|
||||||
|
// tvText.append("\n")
|
||||||
|
// tvText.append("Products")
|
||||||
|
// tvText.append("\n")
|
||||||
|
// tvText.append("\n")
|
||||||
|
//
|
||||||
|
// woocommerce.products.enqueue(object : Callback<ArrayList<Product>> {
|
||||||
|
// override fun onResponse(call: Call<ArrayList<Product>>, response: Response<ArrayList<Product>>) {
|
||||||
|
// val products = response.body()
|
||||||
|
// for (product in products!!) {
|
||||||
|
// tvText.append(product.title + "\n")
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// override fun onFailure(call: Call<ArrayList<Product>>, t: Throwable) {
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
// })
|
||||||
|
//
|
||||||
|
// tvText.append("\n")
|
||||||
|
// tvText.append("\n")
|
||||||
|
// tvText.append("Coupons")
|
||||||
|
// tvText.append("\n")
|
||||||
|
// tvText.append("\n")
|
||||||
|
|
||||||
woocommerce.products.enqueue(object : Callback<ArrayList<Product>> {
|
woocommerce.Coupon().coupons().enqueue(object : Callback<List<Coupon>> {
|
||||||
override fun onResponse(call: Call<ArrayList<Product>>, response: Response<ArrayList<Product>>) {
|
override fun onResponse(call: Call<List<Coupon>>, response: Response<List<Coupon>>) {
|
||||||
val products = response.body()
|
val coupons = response.body()
|
||||||
for (product in products!!) {
|
for (coupon in coupons!!) {
|
||||||
tvText.append(product.title)
|
tvText.append(coupon.description + "\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onFailure(call: Call<ArrayList<Product>>, t: Throwable) {
|
override fun onFailure(call: Call<List<Coupon>>, t: Throwable) {
|
||||||
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
23
app/src/main/java/me/gilo/wc_app/ui/ShopActivity.kt
Normal file
23
app/src/main/java/me/gilo/wc_app/ui/ShopActivity.kt
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package me.gilo.wc_app.ui
|
||||||
|
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.support.design.widget.Snackbar
|
||||||
|
import android.support.v7.app.AppCompatActivity;
|
||||||
|
import me.gilo.wc_app.R
|
||||||
|
|
||||||
|
import kotlinx.android.synthetic.main.activity_shop.*
|
||||||
|
|
||||||
|
class ShopActivity : AppCompatActivity() {
|
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
setContentView(R.layout.activity_shop)
|
||||||
|
setSupportActionBar(toolbar)
|
||||||
|
|
||||||
|
fab.setOnClickListener { view ->
|
||||||
|
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
|
||||||
|
.setAction("Action", null).show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,20 +1,18 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<android.support.constraint.ConstraintLayout
|
<android.support.v4.widget.NestedScrollView
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:tools="http://schemas.android.com/tools"
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
tools:context=".MainActivity">
|
tools:context=".MainActivity"
|
||||||
|
android:layout_margin="16dp"
|
||||||
|
>
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/tvText"
|
android:id="@+id/tvText"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="match_parent"
|
||||||
android:text="Hello World!"
|
/>
|
||||||
app:layout_constraintBottom_toBottomOf="parent"
|
|
||||||
app:layout_constraintLeft_toLeftOf="parent"
|
|
||||||
app:layout_constraintRight_toRightOf="parent"
|
|
||||||
app:layout_constraintTop_toTopOf="parent"/>
|
|
||||||
|
|
||||||
</android.support.constraint.ConstraintLayout>
|
</android.support.v4.widget.NestedScrollView>
|
||||||
34
app/src/main/res/layout/activity_shop.xml
Normal file
34
app/src/main/res/layout/activity_shop.xml
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<android.support.design.widget.CoordinatorLayout
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
tools:context=".ui.ShopActivity">
|
||||||
|
|
||||||
|
<android.support.design.widget.AppBarLayout
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:theme="@style/AppTheme.AppBarOverlay">
|
||||||
|
|
||||||
|
<android.support.v7.widget.Toolbar
|
||||||
|
android:id="@+id/toolbar"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="?attr/actionBarSize"
|
||||||
|
android:background="?attr/colorPrimary"
|
||||||
|
app:popupTheme="@style/AppTheme.PopupOverlay"/>
|
||||||
|
|
||||||
|
</android.support.design.widget.AppBarLayout>
|
||||||
|
|
||||||
|
<include layout="@layout/content_shop"/>
|
||||||
|
|
||||||
|
<android.support.design.widget.FloatingActionButton
|
||||||
|
android:id="@+id/fab"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="bottom|end"
|
||||||
|
android:layout_margin="@dimen/fab_margin"
|
||||||
|
app:srcCompat="@android:drawable/ic_dialog_email"/>
|
||||||
|
|
||||||
|
</android.support.design.widget.CoordinatorLayout>
|
||||||
17
app/src/main/res/layout/content_shop.xml
Normal file
17
app/src/main/res/layout/content_shop.xml
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<LinearLayout
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
app:layout_behavior="@string/appbar_scrolling_view_behavior"
|
||||||
|
tools:showIn="@layout/activity_shop"
|
||||||
|
tools:context=".ui.ShopActivity">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
/>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
3
app/src/main/res/values/dimens.xml
Normal file
3
app/src/main/res/values/dimens.xml
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
<resources>
|
||||||
|
<dimen name="fab_margin">16dp</dimen>
|
||||||
|
</resources>
|
||||||
@ -1,3 +1,4 @@
|
|||||||
<resources>
|
<resources>
|
||||||
<string name="app_name">wc-app</string>
|
<string name="app_name">wc-app</string>
|
||||||
|
<string name="title_activity_shop">ShopActivity</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
@ -7,5 +7,11 @@
|
|||||||
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
|
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
|
||||||
<item name="colorAccent">@color/colorAccent</item>
|
<item name="colorAccent">@color/colorAccent</item>
|
||||||
</style>
|
</style>
|
||||||
|
<style name="AppTheme.NoActionBar">
|
||||||
|
<item name="windowActionBar">false</item>
|
||||||
|
<item name="windowNoTitle">true</item>
|
||||||
|
</style>
|
||||||
|
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
|
||||||
|
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
@ -1,6 +1,9 @@
|
|||||||
package me.gilo.woodroid;
|
package me.gilo.woodroid;
|
||||||
|
|
||||||
|
import me.gilo.woodroid.dto.CouponData;
|
||||||
|
import me.gilo.woodroid.models.Coupon;
|
||||||
import me.gilo.woodroid.models.Product;
|
import me.gilo.woodroid.models.Product;
|
||||||
|
import me.gilo.woodroid.repo.CouponRepository;
|
||||||
import me.gilo.woodroid.repo.ProductRepository;
|
import me.gilo.woodroid.repo.ProductRepository;
|
||||||
import retrofit2.Call;
|
import retrofit2.Call;
|
||||||
import retrofit2.Callback;
|
import retrofit2.Callback;
|
||||||
@ -17,33 +20,7 @@ public class Woocommerce {
|
|||||||
private String consumerSecret;
|
private String consumerSecret;
|
||||||
|
|
||||||
final ProductRepository productRepository;
|
final ProductRepository productRepository;
|
||||||
|
final CouponData couponData;
|
||||||
public static void main(String args[]){
|
|
||||||
System.out.println("Hello");
|
|
||||||
|
|
||||||
Woocommerce woocommerce = new Woocommerce.Builder()
|
|
||||||
.setSiteUrl("http://iappsdevelopers.com/codecan/woodroid")
|
|
||||||
.setApiVersion("2")
|
|
||||||
.setConsumerKey("ck_62b755890341ad3d2bd334433d30b5070eea349f")
|
|
||||||
.setConsumerSecret("cs_8f678fedc94cbf520e0240d6eacab4dab2954aaa")
|
|
||||||
.build();
|
|
||||||
|
|
||||||
|
|
||||||
woocommerce.getProducts().enqueue(new Callback<ArrayList<Product>>() {
|
|
||||||
@Override
|
|
||||||
public void onResponse(Call<ArrayList<Product>> call, Response<ArrayList<Product>> response) {
|
|
||||||
ArrayList<Product> products = response.body();
|
|
||||||
for (Product product : products){
|
|
||||||
System.out.println(product.getTitle());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onFailure(Call<ArrayList<Product>> call, Throwable t) {
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public Woocommerce(String siteUrl, String apiVerion, String consumerKey, String consumerSecret) {
|
public Woocommerce(String siteUrl, String apiVerion, String consumerKey, String consumerSecret) {
|
||||||
this.siteUrl = siteUrl;
|
this.siteUrl = siteUrl;
|
||||||
@ -54,6 +31,7 @@ public class Woocommerce {
|
|||||||
this.baseUrl = siteUrl + "/wp-json/wc/v" + apiVerion + "/";
|
this.baseUrl = siteUrl + "/wp-json/wc/v" + apiVerion + "/";
|
||||||
|
|
||||||
productRepository = new ProductRepository(baseUrl, consumerKey, consumerSecret);
|
productRepository = new ProductRepository(baseUrl, consumerKey, consumerSecret);
|
||||||
|
couponData = new CouponData(baseUrl, consumerKey, consumerSecret);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -98,5 +76,7 @@ public class Woocommerce {
|
|||||||
return productRepository.products();
|
return productRepository.products();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CouponData Coupon() {
|
||||||
|
return couponData;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -77,7 +77,15 @@ public interface API {
|
|||||||
Call<Data> loginUser2(@Path("email") String email);
|
Call<Data> loginUser2(@Path("email") String email);
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
//Coupons
|
/** Coupons
|
||||||
|
The coupons API allows you to
|
||||||
|
1. create,
|
||||||
|
2. view,
|
||||||
|
3. update
|
||||||
|
4. delete
|
||||||
|
individual, or a batch, of coupon codes.
|
||||||
|
|
||||||
|
**/
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
@Headers("Content-Type: application/json")
|
@Headers("Content-Type: application/json")
|
||||||
|
|||||||
45
woodroid/src/main/java/me/gilo/woodroid/data/CouponAPI.java
Normal file
45
woodroid/src/main/java/me/gilo/woodroid/data/CouponAPI.java
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
package me.gilo.woodroid.data;
|
||||||
|
|
||||||
|
|
||||||
|
import me.gilo.woodroid.models.Coupon;
|
||||||
|
import retrofit2.Call;
|
||||||
|
import retrofit2.http.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface CouponAPI{
|
||||||
|
|
||||||
|
/** Coupons
|
||||||
|
The coupons API allows you to
|
||||||
|
1. create,
|
||||||
|
2. view,
|
||||||
|
3. update
|
||||||
|
4. delete
|
||||||
|
individual, or a batch, of coupon codes.
|
||||||
|
|
||||||
|
**/
|
||||||
|
|
||||||
|
@Headers("Content-Type: application/json")
|
||||||
|
@POST("coupons")
|
||||||
|
Call<Coupon> create(@Body Coupon body);
|
||||||
|
|
||||||
|
@GET("coupons/{id}")
|
||||||
|
Call<Coupon> view(@Path("id") int id);
|
||||||
|
|
||||||
|
@GET("coupons")
|
||||||
|
Call<List<Coupon>> list();
|
||||||
|
|
||||||
|
@Headers("Content-Type: application/json")
|
||||||
|
@PUT("coupons/{id}")
|
||||||
|
Call<Coupon> update(@Path("id") int id, @Body Coupon body);
|
||||||
|
|
||||||
|
@DELETE("coupons/{id}")
|
||||||
|
Call<Coupon> delete(@Path("id") int id);
|
||||||
|
|
||||||
|
@DELETE("coupons/{id}")
|
||||||
|
Call<Coupon> delete(@Path("id") int id, @Query("force") boolean force);
|
||||||
|
|
||||||
|
@POST("coupons/batch")
|
||||||
|
Call<String> batch(@Body Coupon body);
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,19 +1,16 @@
|
|||||||
package me.gilo.woodroid.data;
|
package me.gilo.woodroid.data;
|
||||||
|
|
||||||
import android.util.Base64;
|
import android.util.Base64;
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import okhttp3.HttpUrl;
|
import okhttp3.HttpUrl;
|
||||||
import okhttp3.Interceptor;
|
import okhttp3.Interceptor;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
|
||||||
|
|
||||||
import org.apache.http.NameValuePair;
|
import org.apache.http.NameValuePair;
|
||||||
import org.apache.http.client.utils.URLEncodedUtils;
|
import org.apache.http.client.utils.URLEncodedUtils;
|
||||||
import org.apache.http.message.BasicNameValuePair;
|
import org.apache.http.message.BasicNameValuePair;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
@ -103,6 +100,81 @@ public class RestAdapter {
|
|||||||
return retrofit.create(API.class);
|
return retrofit.create(API.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public API createAPI(Class<API> service, final String endpoint, Map<String, String> query) {
|
||||||
|
|
||||||
|
if (query != null) {
|
||||||
|
setParams(endpoint, query);
|
||||||
|
}else{
|
||||||
|
setParams(endpoint);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Define the interceptor, add authentication headers
|
||||||
|
Interceptor interceptor = chain -> {
|
||||||
|
|
||||||
|
HttpUrl.Builder builder = chain.request().url().newBuilder();
|
||||||
|
for (NameValuePair entry : params) {
|
||||||
|
builder.addQueryParameter(entry.getName(), entry.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
Request newRequest = chain.request()
|
||||||
|
.newBuilder()
|
||||||
|
.url(builder.build())
|
||||||
|
.header("Accept", "application/json")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return chain.proceed(newRequest);
|
||||||
|
};
|
||||||
|
|
||||||
|
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
|
||||||
|
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
|
||||||
|
|
||||||
|
OkHttpClient client = new OkHttpClient.Builder()
|
||||||
|
.addInterceptor(interceptor)
|
||||||
|
.addInterceptor(loggingInterceptor)
|
||||||
|
.readTimeout(30, TimeUnit.SECONDS)
|
||||||
|
.writeTimeout(30, TimeUnit.SECONDS)
|
||||||
|
.connectTimeout(15, TimeUnit.SECONDS)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Retrofit retrofit = new Retrofit.Builder()
|
||||||
|
.baseUrl(baseUrl)
|
||||||
|
//.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
|
||||||
|
.addConverterFactory(GsonConverterFactory.create())
|
||||||
|
.client(client)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return retrofit.create(API.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Interceptor getInterceptor(final String endpoint, Map<String, String> query) {
|
||||||
|
|
||||||
|
if (query != null) {
|
||||||
|
setParams(endpoint, query);
|
||||||
|
}else{
|
||||||
|
setParams(endpoint);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Define the interceptor, add authentication headers
|
||||||
|
Interceptor interceptor = chain -> {
|
||||||
|
|
||||||
|
HttpUrl.Builder builder = chain.request().url().newBuilder();
|
||||||
|
for (NameValuePair entry : params) {
|
||||||
|
builder.addQueryParameter(entry.getName(), entry.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
Request newRequest = chain.request()
|
||||||
|
.newBuilder()
|
||||||
|
.url(builder.build())
|
||||||
|
.header("Accept", "application/json")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return chain.proceed(newRequest);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
return interceptor;
|
||||||
|
}
|
||||||
|
|
||||||
public ArrayList<NameValuePair> setParams(String endpoint) {
|
public ArrayList<NameValuePair> setParams(String endpoint) {
|
||||||
final String uri = baseUrl + endpoint;
|
final String uri = baseUrl + endpoint;
|
||||||
|
|
||||||
|
|||||||
43
woodroid/src/main/java/me/gilo/woodroid/dto/CouponData.java
Normal file
43
woodroid/src/main/java/me/gilo/woodroid/dto/CouponData.java
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
package me.gilo.woodroid.dto;
|
||||||
|
|
||||||
|
import me.gilo.woodroid.data.RestAdapter;
|
||||||
|
import me.gilo.woodroid.models.Coupon;
|
||||||
|
import me.gilo.woodroid.repo.CouponRepository;
|
||||||
|
import retrofit2.Call;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class CouponData {
|
||||||
|
|
||||||
|
final CouponRepository couponRepository;
|
||||||
|
|
||||||
|
public CouponData(String baseUrl, String consumerKey, String consumerSecret) {
|
||||||
|
couponRepository = new CouponRepository(baseUrl, consumerKey, consumerSecret);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Call<Coupon> create(Coupon coupon) {
|
||||||
|
return couponRepository.create(coupon);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Call<Coupon> coupon(int id) {
|
||||||
|
return couponRepository.coupon(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Call<List<Coupon>> coupons() {
|
||||||
|
return couponRepository.coupons();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Call<Coupon> update(int id, Coupon coupon) {
|
||||||
|
return couponRepository.update(id, coupon);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Call<Coupon> delete(int id) {
|
||||||
|
return couponRepository.delete(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Call<Coupon> delete(int id, boolean force) {
|
||||||
|
return couponRepository.delete(id, force);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,49 @@
|
|||||||
|
package me.gilo.woodroid.repo;
|
||||||
|
|
||||||
|
import me.gilo.woodroid.data.CouponAPI;
|
||||||
|
import me.gilo.woodroid.models.Coupon;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.logging.HttpLoggingInterceptor;
|
||||||
|
import retrofit2.Call;
|
||||||
|
import retrofit2.Retrofit;
|
||||||
|
import retrofit2.converter.gson.GsonConverterFactory;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
public class CouponRepository extends WooRepository{
|
||||||
|
|
||||||
|
private final CouponAPI apiService;
|
||||||
|
|
||||||
|
public CouponRepository(String baseUrl, String consumerKey, String consumerSecret) {
|
||||||
|
super(baseUrl, consumerKey, consumerSecret);
|
||||||
|
apiService = retrofit.create(CouponAPI.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Call<Coupon> create(Coupon coupon) {
|
||||||
|
return apiService.create(coupon);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Call<Coupon> coupon(int id) {
|
||||||
|
return apiService.view(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Call<List<Coupon>> coupons() {
|
||||||
|
return apiService.list();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Call<Coupon> update(int id, Coupon coupon) {
|
||||||
|
return apiService.update(id, coupon);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Call<Coupon> delete(int id) {
|
||||||
|
return apiService.delete(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Call<Coupon> delete(int id, boolean force) {
|
||||||
|
return apiService.delete(id, force);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -9,26 +9,11 @@ import retrofit2.Call;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
public class ProductRepository {
|
public class ProductRepository extends WooRepository{
|
||||||
|
|
||||||
private String baseUrl;
|
|
||||||
private String consumerKey;
|
|
||||||
private String consumerSecret;
|
|
||||||
|
|
||||||
RestAdapter restAdapter;
|
|
||||||
|
|
||||||
public ProductRepository(String baseUrl, String consumerKey, String consumerSecret) {
|
public ProductRepository(String baseUrl, String consumerKey, String consumerSecret) {
|
||||||
this.baseUrl = baseUrl;
|
super(baseUrl, consumerKey, consumerSecret);
|
||||||
this.consumerKey = consumerKey;
|
|
||||||
this.consumerSecret = consumerSecret;
|
|
||||||
|
|
||||||
restAdapter = new RestAdapter(baseUrl, consumerKey, consumerSecret);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Call<ArrayList<Product>> products() {
|
|
||||||
API apiService = restAdapter.createAPI("products");
|
|
||||||
return apiService.getProducts();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO
|
//TODO
|
||||||
@ -46,25 +31,18 @@ public class ProductRepository {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Call<ArrayList<Product>> products() {
|
||||||
|
API apiService = restAdapter.createAPI("products");
|
||||||
|
return apiService.getProducts();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public Call<ArrayList<Product>> products(HashMap<String, String> filters) {
|
public Call<ArrayList<Product>> products(HashMap<String, String> filters) {
|
||||||
API apiService = restAdapter.createAPI("products", filters);
|
API apiService = restAdapter.createAPI("products", filters);
|
||||||
return apiService.filter(filters);
|
return apiService.filter(filters);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Call<ArrayList<Category>> categories() {
|
|
||||||
API apiService = restAdapter.createAPI("products/categories");
|
|
||||||
return apiService.getCategories();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Call<ArrayList<Category>> categories(HashMap<String, String> filters) {
|
|
||||||
API apiService = restAdapter.createAPI("products/categories", filters);
|
|
||||||
return apiService.getCategories();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Call<ArrayList<Category>> categories(int parent_category) {
|
|
||||||
API apiService = restAdapter.createAPI("products/categories");
|
|
||||||
return apiService.getCategories();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,54 @@
|
|||||||
|
package me.gilo.woodroid.repo;
|
||||||
|
|
||||||
|
import me.gilo.woodroid.data.API;
|
||||||
|
import me.gilo.woodroid.data.RestAdapter;
|
||||||
|
import me.gilo.woodroid.models.Category;
|
||||||
|
import me.gilo.woodroid.models.Product;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.logging.HttpLoggingInterceptor;
|
||||||
|
import retrofit2.Call;
|
||||||
|
import retrofit2.Retrofit;
|
||||||
|
import retrofit2.converter.gson.GsonConverterFactory;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
public class WooRepository {
|
||||||
|
|
||||||
|
private String baseUrl;
|
||||||
|
private String consumerKey;
|
||||||
|
private String consumerSecret;
|
||||||
|
|
||||||
|
RestAdapter restAdapter;
|
||||||
|
Retrofit retrofit;
|
||||||
|
|
||||||
|
public WooRepository(String baseUrl, String consumerKey, String consumerSecret) {
|
||||||
|
this.baseUrl = baseUrl;
|
||||||
|
this.consumerKey = consumerKey;
|
||||||
|
this.consumerSecret = consumerSecret;
|
||||||
|
|
||||||
|
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
|
||||||
|
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
|
||||||
|
|
||||||
|
restAdapter = new RestAdapter(baseUrl, consumerKey, consumerSecret);
|
||||||
|
|
||||||
|
OkHttpClient client = new OkHttpClient.Builder()
|
||||||
|
.addInterceptor(restAdapter.getInterceptor("coupons", null))
|
||||||
|
.addInterceptor(loggingInterceptor)
|
||||||
|
.readTimeout(30, TimeUnit.SECONDS)
|
||||||
|
.writeTimeout(30, TimeUnit.SECONDS)
|
||||||
|
.connectTimeout(15, TimeUnit.SECONDS)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
retrofit = new Retrofit.Builder()
|
||||||
|
.baseUrl(baseUrl)
|
||||||
|
.addConverterFactory(GsonConverterFactory.create())
|
||||||
|
.client(client)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user