Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
9fbba5a1
authored
May 22, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: return user's quota with billing api (close #92)
parent
19405d13
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
63 additions
and
4 deletions
+63
-4
controller/billing.go
+41
-0
controller/channel-billing.go
+17
-2
router/dashboard.go
+5
-2
No files found.
controller/billing.go
0 → 100644
View file @
9fbba5a1
package
controller
import
(
"github.com/gin-gonic/gin"
"one-api/model"
)
func
GetSubscription
(
c
*
gin
.
Context
)
{
userId
:=
c
.
GetInt
(
"id"
)
quota
,
err
:=
model
.
GetUserQuota
(
userId
)
if
err
!=
nil
{
openAIError
:=
OpenAIError
{
Message
:
err
.
Error
(),
Type
:
"one_api_error"
,
}
c
.
JSON
(
200
,
gin
.
H
{
"error"
:
openAIError
,
})
return
}
subscription
:=
OpenAISubscriptionResponse
{
Object
:
"billing_subscription"
,
HasPaymentMethod
:
true
,
SoftLimitUSD
:
float64
(
quota
),
HardLimitUSD
:
float64
(
quota
),
SystemHardLimitUSD
:
float64
(
quota
),
}
c
.
JSON
(
200
,
subscription
)
return
}
func
GetUsage
(
c
*
gin
.
Context
)
{
//userId := c.GetInt("id")
// TODO: get usage from database
usage
:=
OpenAIUsageResponse
{
Object
:
"list"
,
TotalUsage
:
0
,
}
c
.
JSON
(
200
,
usage
)
return
}
controller/channel-billing.go
View file @
9fbba5a1
...
@@ -13,12 +13,27 @@ import (
...
@@ -13,12 +13,27 @@ import (
"time"
"time"
)
)
// https://github.com/songquanpeng/one-api/issues/79
type
OpenAISubscriptionResponse
struct
{
type
OpenAISubscriptionResponse
struct
{
HasPaymentMethod
bool
`json:"has_payment_method"`
Object
string
`json:"object"`
HardLimitUSD
float64
`json:"hard_limit_usd"`
HasPaymentMethod
bool
`json:"has_payment_method"`
SoftLimitUSD
float64
`json:"soft_limit_usd"`
HardLimitUSD
float64
`json:"hard_limit_usd"`
SystemHardLimitUSD
float64
`json:"system_hard_limit_usd"`
}
type
OpenAIUsageDailyCost
struct
{
Timestamp
float64
`json:"timestamp"`
LineItems
[]
struct
{
Name
string
`json:"name"`
Cost
float64
`json:"cost"`
}
}
}
type
OpenAIUsageResponse
struct
{
type
OpenAIUsageResponse
struct
{
Object
string
`json:"object"`
//DailyCosts []OpenAIUsageDailyCost `json:"daily_costs"`
TotalUsage
float64
`json:"total_usage"`
// unit: 0.01 dollar
TotalUsage
float64
`json:"total_usage"`
// unit: 0.01 dollar
}
}
...
...
router/dashboard.go
View file @
9fbba5a1
...
@@ -8,11 +8,14 @@ import (
...
@@ -8,11 +8,14 @@ import (
)
)
func
SetDashboardRouter
(
router
*
gin
.
Engine
)
{
func
SetDashboardRouter
(
router
*
gin
.
Engine
)
{
apiRouter
:=
router
.
Group
(
"/
dashboard
"
)
apiRouter
:=
router
.
Group
(
"/"
)
apiRouter
.
Use
(
gzip
.
Gzip
(
gzip
.
DefaultCompression
))
apiRouter
.
Use
(
gzip
.
Gzip
(
gzip
.
DefaultCompression
))
apiRouter
.
Use
(
middleware
.
GlobalAPIRateLimit
())
apiRouter
.
Use
(
middleware
.
GlobalAPIRateLimit
())
apiRouter
.
Use
(
middleware
.
TokenAuth
())
apiRouter
.
Use
(
middleware
.
TokenAuth
())
{
{
apiRouter
.
GET
(
"/billing/credit_grants"
,
controller
.
GetTokenStatus
)
apiRouter
.
GET
(
"/dashboard/billing/subscription"
,
controller
.
GetSubscription
)
apiRouter
.
GET
(
"/v1/dashboard/billing/subscription"
,
controller
.
GetSubscription
)
apiRouter
.
GET
(
"/dashboard/billing/usage"
,
controller
.
GetUsage
)
apiRouter
.
GET
(
"/v1/dashboard/billing/usage"
,
controller
.
GetUsage
)
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment