From 842d772c70757e1b0c3f09a020934100b917cb7f Mon Sep 17 00:00:00 2001 From: Fiyinfolu Eludire <17176053+faeludire@users.noreply.github.com> Date: Tue, 5 Jan 2021 21:41:13 +0000 Subject: [PATCH] Add wrapper for List Tariff Charges api --- .../octopusenergy/OctopusEnergyApi.java | 7 ++ .../example/ProductRequestExample.java | 2 +- .../example/TariffChargeRequestExample.java | 18 ++++ .../request/TariffChargesListRequest.java | 82 +++++++++++++++++++ .../response/data/EnergyType.java | 17 ++++ .../response/data/OctopusEnergyProduct.java | 1 - .../octopusenergy/response/data/RateType.java | 18 ++++ .../response/data/TariffCharge.java | 53 ++++++++++++ .../response/data/TariffChargeData.java | 60 ++++++++++++++ .../service/OctopusEnergyApiService.java | 10 +++ 10 files changed, 266 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/wrapper/octopusenergy/example/TariffChargeRequestExample.java create mode 100644 src/main/java/com/wrapper/octopusenergy/request/TariffChargesListRequest.java create mode 100644 src/main/java/com/wrapper/octopusenergy/response/data/EnergyType.java create mode 100644 src/main/java/com/wrapper/octopusenergy/response/data/RateType.java create mode 100644 src/main/java/com/wrapper/octopusenergy/response/data/TariffCharge.java create mode 100644 src/main/java/com/wrapper/octopusenergy/response/data/TariffChargeData.java diff --git a/src/main/java/com/wrapper/octopusenergy/OctopusEnergyApi.java b/src/main/java/com/wrapper/octopusenergy/OctopusEnergyApi.java index 52334b5..ded5a2c 100644 --- a/src/main/java/com/wrapper/octopusenergy/OctopusEnergyApi.java +++ b/src/main/java/com/wrapper/octopusenergy/OctopusEnergyApi.java @@ -4,6 +4,9 @@ import com.wrapper.octopusenergy.request.ProductsRequest; import com.wrapper.octopusenergy.request.RetrieveProductRequest; +import com.wrapper.octopusenergy.request.TariffChargesListRequest; +import com.wrapper.octopusenergy.response.data.EnergyType; +import com.wrapper.octopusenergy.response.data.RateType; import com.wrapper.octopusenergy.service.OctopusEnergyApiService; import okhttp3.OkHttpClient; import retrofit2.Retrofit; @@ -57,4 +60,8 @@ public ProductsRequest.Builder getProductList() { public RetrieveProductRequest.Builder getProduct(String productCode) { return new RetrieveProductRequest.Builder(this, productCode); } + + public TariffChargesListRequest.Builder getTariffCharges(String productCode, String tariffCode, EnergyType energyType, RateType rateType) { + return new TariffChargesListRequest.Builder(this, productCode, tariffCode, energyType, rateType); + } } diff --git a/src/main/java/com/wrapper/octopusenergy/example/ProductRequestExample.java b/src/main/java/com/wrapper/octopusenergy/example/ProductRequestExample.java index b5b4c92..8d133cc 100644 --- a/src/main/java/com/wrapper/octopusenergy/example/ProductRequestExample.java +++ b/src/main/java/com/wrapper/octopusenergy/example/ProductRequestExample.java @@ -11,7 +11,7 @@ public static void main(String[] args) { OctopusEnergyApi api = new OctopusEnergyApi(args[0]); // Create and execute a request - OctopusEnergyProduct product = api.getProduct("M-AND-S-SEG-FIX-12M-20-11-11") + OctopusEnergyProduct product = api.getProduct("PREPAY-VAR-18-09-21") .tariffsActiveAt(LocalDateTime.now()) .execute(); System.out.println("productRequest: " + product); diff --git a/src/main/java/com/wrapper/octopusenergy/example/TariffChargeRequestExample.java b/src/main/java/com/wrapper/octopusenergy/example/TariffChargeRequestExample.java new file mode 100644 index 0000000..cf1ad92 --- /dev/null +++ b/src/main/java/com/wrapper/octopusenergy/example/TariffChargeRequestExample.java @@ -0,0 +1,18 @@ +package com.wrapper.octopusenergy.example; + +import com.wrapper.octopusenergy.OctopusEnergyApi; +import com.wrapper.octopusenergy.response.data.EnergyType; +import com.wrapper.octopusenergy.response.data.RateType; +import com.wrapper.octopusenergy.response.data.TariffChargeData; + +public class TariffChargeRequestExample { + public static void main(String[] args) { + // For all requests an API key is needed + OctopusEnergyApi api = new OctopusEnergyApi(args[0]); + + // Create and execute a request + TariffChargeData product = api.getTariffCharges("PREPAY-VAR-18-09-21", "E-1R-PREPAY-VAR-18-09-21-A", EnergyType.ELECTRICITY_TARIFFS, RateType.STANDING_CHARGES) + .execute(); + System.out.println("productRequest: " + product); + } +} diff --git a/src/main/java/com/wrapper/octopusenergy/request/TariffChargesListRequest.java b/src/main/java/com/wrapper/octopusenergy/request/TariffChargesListRequest.java new file mode 100644 index 0000000..b917e67 --- /dev/null +++ b/src/main/java/com/wrapper/octopusenergy/request/TariffChargesListRequest.java @@ -0,0 +1,82 @@ +package com.wrapper.octopusenergy.request; + +import java.time.LocalDateTime; + +import com.wrapper.octopusenergy.OctopusEnergyApi; +import com.wrapper.octopusenergy.response.data.EnergyType; +import com.wrapper.octopusenergy.response.data.RateType; +import com.wrapper.octopusenergy.response.data.TariffChargeData; +import com.wrapper.octopusenergy.util.ISODateFormatter; + +public class TariffChargesListRequest extends Request { + private final String periodTo; + private final String pageSize; + private final String periodFrom; + private final RateType rateType; + private final String tariffCode; + private final String productCode; + private final EnergyType energyType; + + TariffChargesListRequest(Builder builder) { + super(builder.octopusEnergyApi); + + this.productCode = builder.productCode; + this.tariffCode = builder.tariffCode; + this.periodTo = builder.periodTo; + this.pageSize = builder.pageSize; + this.periodFrom = builder.periodFrom; + this.rateType = builder.rateType; + this.energyType = builder.energyType; + } + + protected TariffChargeData execute() { + return super.execute(octopusEnergyApi.octopusEnergyApiService() + .getTariffCharges( + productCode, + tariffCode, + energyType.getValue(), + rateType.getValue(), + periodFrom, + periodTo, + pageSize), TariffChargeData.class); + } + + public static class Builder extends AbstractBuilder { + + private String periodTo; + private String pageSize; + private String periodFrom; + private final String tariffCode; + private final RateType rateType; + private final String productCode; + private final EnergyType energyType; + + public Builder(OctopusEnergyApi octopusEnergyApi, String productCode, String tariffCode, EnergyType energyType, RateType rateType) { + super(octopusEnergyApi); + this.productCode = productCode; + this.tariffCode = tariffCode; + this.energyType = energyType; + this.rateType = rateType; + } + + public Builder periodFrom(LocalDateTime periodFrom) { + this.periodFrom = ISODateFormatter.getFormattedDateTimeString(periodFrom); + return this; + } + + public Builder periodTo(LocalDateTime periodTo) { + this.periodTo = ISODateFormatter.getFormattedDateTimeString(periodTo); + return this; + } + + public Builder pageSize(String pageSize) { + this.pageSize = pageSize; + return this; + } + + @Override + public TariffChargeData execute() { + return new TariffChargesListRequest(this).execute(); + } + } +} diff --git a/src/main/java/com/wrapper/octopusenergy/response/data/EnergyType.java b/src/main/java/com/wrapper/octopusenergy/response/data/EnergyType.java new file mode 100644 index 0000000..264f9b1 --- /dev/null +++ b/src/main/java/com/wrapper/octopusenergy/response/data/EnergyType.java @@ -0,0 +1,17 @@ +package com.wrapper.octopusenergy.response.data; + +public enum EnergyType { + + GAS_TARIFFS("gas-tariffs"), + ELECTRICITY_TARIFFS("electricity-tariffs"); + + private final String value; + + EnergyType(String value) { + this.value = value; + } + + public String getValue() { + return value; + } +} diff --git a/src/main/java/com/wrapper/octopusenergy/response/data/OctopusEnergyProduct.java b/src/main/java/com/wrapper/octopusenergy/response/data/OctopusEnergyProduct.java index 3b12270..c24717b 100644 --- a/src/main/java/com/wrapper/octopusenergy/response/data/OctopusEnergyProduct.java +++ b/src/main/java/com/wrapper/octopusenergy/response/data/OctopusEnergyProduct.java @@ -8,7 +8,6 @@ import com.google.gson.annotations.SerializedName; import com.sun.istack.internal.Nullable; import com.wrapper.octopusenergy.response.Response; -import netscape.javascript.JSObject; public class OctopusEnergyProduct extends Response { @SerializedName("code") diff --git a/src/main/java/com/wrapper/octopusenergy/response/data/RateType.java b/src/main/java/com/wrapper/octopusenergy/response/data/RateType.java new file mode 100644 index 0000000..098ffe6 --- /dev/null +++ b/src/main/java/com/wrapper/octopusenergy/response/data/RateType.java @@ -0,0 +1,18 @@ +package com.wrapper.octopusenergy.response.data; + +public enum RateType { + STANDING_CHARGES("standing-charges"), + STANDARD_UNIT_RATES("standard-unit-rates"), + DAY_UNIT_RATES("day-unit-rates"), + NIGHT_UNIT_RATES("night-unit-rates"); + + private final String value; + + RateType(String value) { + this.value = value; + } + + public String getValue() { + return value; + } +} diff --git a/src/main/java/com/wrapper/octopusenergy/response/data/TariffCharge.java b/src/main/java/com/wrapper/octopusenergy/response/data/TariffCharge.java new file mode 100644 index 0000000..2d5e3da --- /dev/null +++ b/src/main/java/com/wrapper/octopusenergy/response/data/TariffCharge.java @@ -0,0 +1,53 @@ +package com.wrapper.octopusenergy.response.data; + +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +public class TariffCharge { + + @SerializedName("value_exc_vat") + @Expose + private Double valueExcVat; + @SerializedName("value_inc_vat") + @Expose + private Double valueIncVat; + @SerializedName("valid_from") + @Expose + private String validFrom; + @SerializedName("valid_to") + @Expose + private String validTo; + + public Double getValueExcVat() { + return valueExcVat; + } + + public void setValueExcVat(Double valueExcVat) { + this.valueExcVat = valueExcVat; + } + + public Double getValueIncVat() { + return valueIncVat; + } + + public void setValueIncVat(Double valueIncVat) { + this.valueIncVat = valueIncVat; + } + + public String getValidFrom() { + return validFrom; + } + + public void setValidFrom(String validFrom) { + this.validFrom = validFrom; + } + + public String getValidTo() { + return validTo; + } + + public void setValidTo(String validTo) { + this.validTo = validTo; + } + +} \ No newline at end of file diff --git a/src/main/java/com/wrapper/octopusenergy/response/data/TariffChargeData.java b/src/main/java/com/wrapper/octopusenergy/response/data/TariffChargeData.java new file mode 100644 index 0000000..abd949b --- /dev/null +++ b/src/main/java/com/wrapper/octopusenergy/response/data/TariffChargeData.java @@ -0,0 +1,60 @@ +package com.wrapper.octopusenergy.response.data; + +import java.util.List; + +import com.google.gson.GsonBuilder; +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; +import com.wrapper.octopusenergy.response.Response; + +public class TariffChargeData extends Response { + + @SerializedName("count") + @Expose + private Integer count; + @SerializedName("next") + @Expose + private Object next; + @SerializedName("previous") + @Expose + private Object previous; + @SerializedName("results") + @Expose + private List tariffCharges = null; + + public Integer getCount() { + return count; + } + + public void setCount(Integer count) { + this.count = count; + } + + public Object getNext() { + return next; + } + + public void setNext(Object next) { + this.next = next; + } + + public Object getPrevious() { + return previous; + } + + public void setPrevious(Object previous) { + this.previous = previous; + } + + public List getResults() { + return tariffCharges; + } + + public void setResults(List results) { + this.tariffCharges = results; + } + + public String toString() { + return new GsonBuilder().setPrettyPrinting().create().toJson(this); + } +} \ No newline at end of file diff --git a/src/main/java/com/wrapper/octopusenergy/service/OctopusEnergyApiService.java b/src/main/java/com/wrapper/octopusenergy/service/OctopusEnergyApiService.java index 158a48d..697c431 100644 --- a/src/main/java/com/wrapper/octopusenergy/service/OctopusEnergyApiService.java +++ b/src/main/java/com/wrapper/octopusenergy/service/OctopusEnergyApiService.java @@ -2,6 +2,7 @@ import com.wrapper.octopusenergy.response.data.OctopusEnergyProduct; import com.wrapper.octopusenergy.response.data.ProductListData; +import com.wrapper.octopusenergy.response.data.TariffChargeData; import retrofit2.Call; import retrofit2.http.GET; import retrofit2.http.Path; @@ -21,4 +22,13 @@ Call getProducts( @GET("/v1/products/{product_code}/") Call getProduct(@Path("product_code") String productCode, @Query("tariffs_active_at") String tariffsActiveAt); + + @GET("/v1/products/{product_code}/{energy_type}/{tariff_code}/{rate_type}/") + Call getTariffCharges(@Path("product_code") String productCode, + @Path("tariff_code") String tariffCode, + @Path("energy_type") String energyType, + @Path("rate_type") String rateType, + @Query("period_from") String periodFrom, + @Query("period_to") String periodTo, + @Query("page_size") String pageSize); }