Skip to content

Commit 3f5fabc

Browse files
committed
余额查询
1 parent 91f0349 commit 3f5fabc

File tree

5 files changed

+79
-3
lines changed

5 files changed

+79
-3
lines changed

src/main/java/com/plexpt/chatgpt/ChatGPT.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,25 @@
44
import com.plexpt.chatgpt.api.Api;
55
import com.plexpt.chatgpt.entity.BaseResponse;
66
import com.plexpt.chatgpt.entity.billing.CreditGrantsResponse;
7+
import com.plexpt.chatgpt.entity.billing.SubscriptionData;
8+
import com.plexpt.chatgpt.entity.billing.UseageResponse;
79
import com.plexpt.chatgpt.entity.chat.ChatCompletion;
810
import com.plexpt.chatgpt.entity.chat.ChatCompletionResponse;
911
import com.plexpt.chatgpt.entity.chat.Message;
1012
import com.plexpt.chatgpt.exception.ChatException;
1113

1214
import java.math.BigDecimal;
1315
import java.net.Proxy;
16+
import java.text.SimpleDateFormat;
1417
import java.util.Arrays;
18+
import java.util.Date;
1519
import java.util.List;
20+
import java.util.Locale;
1621
import java.util.Objects;
1722
import java.util.concurrent.TimeUnit;
1823

24+
import cn.hutool.core.date.DateTime;
25+
import cn.hutool.core.date.DateUtil;
1926
import cn.hutool.core.util.RandomUtil;
2027
import cn.hutool.http.ContentType;
2128
import cn.hutool.http.Header;
@@ -179,10 +186,17 @@ public CreditGrantsResponse creditGrants() {
179186
* @return
180187
*/
181188
public BigDecimal balance() {
182-
Single<CreditGrantsResponse> creditGrants = apiClient.creditGrants();
183-
CreditGrantsResponse response = creditGrants.blockingGet();
189+
Single<SubscriptionData> subscription = apiClient.subscription();
190+
SubscriptionData subscriptionData = subscription.blockingGet();
191+
BigDecimal total = subscriptionData.getHardLimitUsd();
192+
DateTime start = DateUtil.offsetDay(new Date(), -90);
193+
DateTime end = DateUtil.offsetDay(new Date(), 1);
184194

185-
return response.getTotalAvailable();
195+
Single<UseageResponse> usage = apiClient.usage(formatDate(start), formatDate(end));
196+
UseageResponse useageResponse = usage.blockingGet();
197+
BigDecimal used = useageResponse.getTotalUsage().divide(BigDecimal.valueOf(100));
198+
199+
return total.subtract(used);
186200
}
187201

188202
/**
@@ -199,4 +213,8 @@ public static BigDecimal balance(String key) {
199213
return chatGPT.balance();
200214
}
201215

216+
public static String formatDate(Date date) {
217+
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
218+
return sdf.format(date);
219+
}
202220
}

src/main/java/com/plexpt/chatgpt/api/Api.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package com.plexpt.chatgpt.api;
22

33
import com.plexpt.chatgpt.entity.billing.CreditGrantsResponse;
4+
import com.plexpt.chatgpt.entity.billing.SubscriptionData;
5+
import com.plexpt.chatgpt.entity.billing.UseageResponse;
46
import com.plexpt.chatgpt.entity.chat.ChatCompletion;
57
import com.plexpt.chatgpt.entity.chat.ChatCompletionResponse;
68

79
import io.reactivex.Single;
810
import retrofit2.http.Body;
911
import retrofit2.http.GET;
1012
import retrofit2.http.POST;
13+
import retrofit2.http.Query;
1114

1215

1316
/**
@@ -31,5 +34,18 @@ public interface Api {
3134
@GET("dashboard/billing/credit_grants")
3235
Single<CreditGrantsResponse> creditGrants();
3336

37+
/**
38+
* 余额查询
39+
*/
40+
@GET("v1/dashboard/billing/subscription")
41+
Single<SubscriptionData> subscription();
42+
43+
/**
44+
* 余额查询
45+
*/
46+
@GET("v1/dashboard/billing/usage")
47+
Single<UseageResponse> usage(@Query("start_date") String startDate,
48+
@Query("end_date") String endDate);
49+
3450

3551
}

src/main/java/com/plexpt/chatgpt/entity/billing/Grants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ public class Grants {
1414
private String object;
1515
@JsonProperty("data")
1616
private List<Datum> data;
17+
1718
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.plexpt.chatgpt.entity.billing;
2+
3+
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
6+
import java.math.BigDecimal;
7+
8+
import lombok.Data;
9+
10+
@Data
11+
public class SubscriptionData {
12+
13+
/**
14+
* 赠送金额:美元
15+
*/
16+
@JsonProperty("hard_limit_usd")
17+
private BigDecimal hardLimitUsd;
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.plexpt.chatgpt.entity.billing;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
import java.math.BigDecimal;
6+
7+
import lombok.Data;
8+
9+
/**
10+
* 余额查询接口返回值
11+
*
12+
* @author plexpt
13+
*/
14+
@Data
15+
public class UseageResponse {
16+
17+
/**
18+
* 总使用金额:美元
19+
*/
20+
@JsonProperty("total_usage")
21+
private BigDecimal totalUsage;
22+
23+
}

0 commit comments

Comments
 (0)