Skip to content

Commit

Permalink
余额查询
Browse files Browse the repository at this point in the history
  • Loading branch information
PlexPt committed Apr 3, 2023
1 parent 91f0349 commit 3f5fabc
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/main/java/com/plexpt/chatgpt/ChatGPT.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@
import com.plexpt.chatgpt.api.Api;
import com.plexpt.chatgpt.entity.BaseResponse;
import com.plexpt.chatgpt.entity.billing.CreditGrantsResponse;
import com.plexpt.chatgpt.entity.billing.SubscriptionData;
import com.plexpt.chatgpt.entity.billing.UseageResponse;
import com.plexpt.chatgpt.entity.chat.ChatCompletion;
import com.plexpt.chatgpt.entity.chat.ChatCompletionResponse;
import com.plexpt.chatgpt.entity.chat.Message;
import com.plexpt.chatgpt.exception.ChatException;

import java.math.BigDecimal;
import java.net.Proxy;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.Header;
Expand Down Expand Up @@ -179,10 +186,17 @@ public CreditGrantsResponse creditGrants() {
* @return
*/
public BigDecimal balance() {
Single<CreditGrantsResponse> creditGrants = apiClient.creditGrants();
CreditGrantsResponse response = creditGrants.blockingGet();
Single<SubscriptionData> subscription = apiClient.subscription();
SubscriptionData subscriptionData = subscription.blockingGet();
BigDecimal total = subscriptionData.getHardLimitUsd();
DateTime start = DateUtil.offsetDay(new Date(), -90);
DateTime end = DateUtil.offsetDay(new Date(), 1);

return response.getTotalAvailable();
Single<UseageResponse> usage = apiClient.usage(formatDate(start), formatDate(end));
UseageResponse useageResponse = usage.blockingGet();
BigDecimal used = useageResponse.getTotalUsage().divide(BigDecimal.valueOf(100));

return total.subtract(used);
}

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

public static String formatDate(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
return sdf.format(date);
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/plexpt/chatgpt/api/Api.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.plexpt.chatgpt.api;

import com.plexpt.chatgpt.entity.billing.CreditGrantsResponse;
import com.plexpt.chatgpt.entity.billing.SubscriptionData;
import com.plexpt.chatgpt.entity.billing.UseageResponse;
import com.plexpt.chatgpt.entity.chat.ChatCompletion;
import com.plexpt.chatgpt.entity.chat.ChatCompletionResponse;

import io.reactivex.Single;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query;


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

/**
* 余额查询
*/
@GET("v1/dashboard/billing/subscription")
Single<SubscriptionData> subscription();

/**
* 余额查询
*/
@GET("v1/dashboard/billing/usage")
Single<UseageResponse> usage(@Query("start_date") String startDate,
@Query("end_date") String endDate);


}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ public class Grants {
private String object;
@JsonProperty("data")
private List<Datum> data;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.plexpt.chatgpt.entity.billing;


import com.fasterxml.jackson.annotation.JsonProperty;

import java.math.BigDecimal;

import lombok.Data;

@Data
public class SubscriptionData {

/**
* 赠送金额:美元
*/
@JsonProperty("hard_limit_usd")
private BigDecimal hardLimitUsd;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.plexpt.chatgpt.entity.billing;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.math.BigDecimal;

import lombok.Data;

/**
* 余额查询接口返回值
*
* @author plexpt
*/
@Data
public class UseageResponse {

/**
* 总使用金额:美元
*/
@JsonProperty("total_usage")
private BigDecimal totalUsage;

}

0 comments on commit 3f5fabc

Please sign in to comment.