/
scratchpad.py
197 lines (177 loc) · 7.87 KB
/
scratchpad.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import cgrateshttpapi
import pprint
import sys
import uuid
#Create an Object to send the Requests to (You'll need to update this IP to point to your CGrateS instance)
CGRateS_Obj = cgrateshttpapi.CGRateS('localhost', 2080)
#Define Destinations
CGRateS_Obj.SendData({'method':'ApierV2.SetTPDestination','params':[{"TPid":"cgrates.org","ID":"Dest_AU_Fixed","Prefixes":["612", "613", "617", "618"]}]})
CGRateS_Obj.SendData({'method':'ApierV2.SetTPDestination','params':[{"TPid":"cgrates.org","ID":"Dest_AU_Mobile","Prefixes":["614"]}]})
CGRateS_Obj.SendData({'method':'ApierV2.SetTPDestination','params':[{"TPid":"cgrates.org","ID":"Dest_AU_TollFree","Prefixes":["6113", "6118"]}]})
#Get the Destinations
destinations = CGRateS_Obj.SendData({'method':'ApierV1.GetTPDestinationIDs','params':[{"TPid":"cgrates.org"}]})['result']
print("Destinations: ")
for destination in destinations:
#Iterate through all the Destinations and print the specific data for each and pretty print them
destination = CGRateS_Obj.SendData({'method':'ApierV1.GetTPDestination','params':[{"TPid":"cgrates.org", "ID" : str(destination)}]})['result']
pprint.pprint(destination)
print("\n\n\n")
#Define Rates
CGRateS_Obj.SendData({"method":"ApierV1.SetTPRate","params":[{"ID":"Rate_Data_1024_1","TPid":"cgrates.org","RateSlots":[{"ConnectFee":0,"Rate":1,"RateUnit":"1024","RateIncrement":"1024","GroupIntervalStart":"0s"}]}],"id":1})
CGRateS_Obj.SendData({"method":"ApierV1.SetTPRate","params":[{"ID":"Rate_AU_Mobile_Rate_1","TPid":"cgrates.org","RateSlots":[{"ConnectFee":0,"Rate":22,"RateUnit":"60s","RateIncrement":"60s","GroupIntervalStart":"0s"}]}],"id":1})
CGRateS_Obj.SendData({"method":"ApierV1.SetTPRate","params":[{"ID":"Rate_AU_Fixed_Rate_1","TPid":"cgrates.org","RateSlots":[{"ConnectFee":0,"Rate":14,"RateUnit":"60s","RateIncrement":"60s","GroupIntervalStart":"0s"}]}],"id":1})
CGRateS_Obj.SendData({"method":"ApierV1.SetTPRate","params":[{"ID":"Rate_AU_Toll_Free_Rate_1","TPid":"cgrates.org","RateSlots":[{"ConnectFee":25,"Rate":0,"RateUnit":"60s","RateIncrement":"60s","GroupIntervalStart":"0s"}]}],"id":1})
#Get the Rates
TPRateIds = CGRateS_Obj.SendData({"method":"ApierV1.GetTPRateIds","params":[{"TPid":"cgrates.org"}]})['result']
print("Rates: ")
for TPRateId in TPRateIds:
#Iterate through all the rates and pretty print them
print(TPRateId)
print("\n\n\n")
#Define DestinationRate for Voice
CGRateS_Obj.SendData({"method": "ApierV1.SetTPDestinationRate", "params": [
{"ID": "DestinationRate_AU", "TPid": "cgrates.org", "DestinationRates": [ \
{"DestinationId": "Dest_AU_Fixed", "RateId": "Rate_AU_Fixed_Rate_1", "Rate": None, "RoundingMethod": "*up", "RoundingDecimals": 4, "MaxCost": 0, "MaxCostStrategy": ""},\
{"DestinationId": "Dest_AU_Mobile", "RateId": "Rate_AU_Mobile_Rate_1", "Rate": None, "RoundingMethod": "*up", "RoundingDecimals": 4, "MaxCost": 0, "MaxCostStrategy": ""}, \
{"DestinationId": "Dest_AU_TollFree", "RateId": "Rate_AU_Toll_Free_Rate_1", "Rate": None, "RoundingMethod": "*up", "RoundingDecimals": 4, "MaxCost": 0, "MaxCostStrategy": ""}\
]},
]})
#Define DestinationRate for Data
CGRateS_Obj.SendData({"method": "ApierV1.SetTPDestinationRate", "params": [
{"ID": "DestinationRate_DataServices", "TPid": "cgrates.org", "DestinationRates": [ \
{"DestinationId": "*any", "RateId": "Rate_Data_1024_1", "Rate": None, "RoundingMethod": "*up", "RoundingDecimals": 4, "MaxCost": 0, "MaxCostStrategy": ""}\
]},
]})
#Get the DestinationRate we just created
print("DestinationRate: ")
TPDestinationRate = CGRateS_Obj.SendData({"jsonrpc":"2.0","method":"ApierV1.GetTPDestinationRate","params":[{"ID":"DestinationRate_AU","TPid":"cgrates.org"}],"id":1})
pprint.pprint(TPDestinationRate)
print("\n\n\n")
#Define RatingPlan
TPRatingPlans = CGRateS_Obj.SendData({
"id": 3,
"method": "APIerSv1.SetTPRatingPlan",
"params": [
{
"TPid": "cgrates.org",
"ID": "RatingPlan_VoiceCalls",
"RatingPlanBindings": [
{
"DestinationRatesId": "DestinationRate_AU",
"TimingId": "*any",
"Weight": 10
}
]
},
{
"TPid": "cgrates.org",
"ID": "RatingPlan_DataCalls",
"RatingPlanBindings": [
{
"DestinationRatesId": "DestinationRate_DataServices",
"TimingId": "*any",
"Weight": 10
}
]
}
]
})
#Get the RatingPlans
RatingPlan_VoiceCalls = CGRateS_Obj.SendData(
{"jsonrpc": "2.0", "method": "ApierV1.GetTPRatingPlanIds", "params": [{"TPid": "cgrates.org"}]})
print("RatingPlan_VoiceCalls: ")
pprint.pprint(RatingPlan_VoiceCalls)
print("\n\n\n")
#Load TariffPlan we just defiend from StorDB to DataDB
print(CGRateS_Obj.SendData({"method":"APIerSv1.LoadTariffPlanFromStorDb","params":[{"TPid":"cgrates.org","DryRun":False,"Validate":True,"APIOpts":None,"Caching":None}],"id":0}))
#Create RatingProfile
print(CGRateS_Obj.SendData({
"method": "APIerSv1.SetRatingProfile",
"params": [
{
"TPid": "RatingProfile_VoiceCalls",
"Overwrite": True,
"LoadId" : "APItest",
"Tenant": "cgrates.org",
"Category": "call",
"Subject": "*any",
"RatingPlanActivations": [
{
"ActivationTime": "2014-01-14T00:00:00Z",
"RatingPlanId": "RatingPlan_VoiceCalls",
"FallbackSubjects": ""
}
]
},
{
"TPid": "RatingProfile_Data",
"Overwrite": True,
"LoadId" : "APItest",
"Tenant": "cgrates.org",
"Category": "data",
"Subject": "*any",
"RatingPlanActivations": [
{
"ActivationTime": "2014-01-14T00:00:00Z",
"RatingPlanId": "RatingPlan_DataCalls",
"FallbackSubjects": ""
}
]
}
]
}))
#Get the Rating Profiles
print("GetTPRatingProfileIds: ")
TPRatingProfileIds = CGRateS_Obj.SendData({"jsonrpc": "2.0", "method": "ApierV1.GetRatingProfileIDs", "params": [{"TPid": "cgrates.org"}]})
print("TPRatingProfileIds: ")
pprint.pprint(TPRatingProfileIds)
# Define Charger
print(CGRateS_Obj.SendData({
"method": "APIerSv1.SetChargerProfile",
"params": [
{
"Tenant": "cgrates.org",
"ID": "DEFAULT",
'FilterIDs': [],
'AttributeIDs': ['*none'],
"RunID": "DEFAULT",
'Weight': 0,
}
]}))
# Set Charger
print("GetChargerProfile: ")
GetChargerProfile = CGRateS_Obj.SendData(
{"jsonrpc": "2.0", "method": "ApierV1.GetChargerProfile", "params": [{"Tenant": "cgrates.org", "ID": "DEFAULT"}]})
print("GetChargerProfile: ")
pprint.pprint(GetChargerProfile)
#Clear the Cache
pprint.pprint(CGRateS_Obj.SendData({"method":"CacheSv1.Clear","params":[]}))
#Rate a test call
print("Testing call..")
cdr = CGRateS_Obj.SendData({"method": "APIerSv1.GetCost", "params": [ { \
"Tenant": "cgrates.org", \
"Category": "call", \
"Subject": "1001", \
"AnswerTime": "2025-08-04T13:00:00Z", \
"Destination": "6140000", \
"Usage": "123s", \
"APIOpts": {}
}], "id": 0})
pprint.pprint(cdr)
# #Add a CDR
# print("Testing call..")
# cdr = CGRateS_Obj.SendData({"method": "CDRsV1.ProcessExternalCDR", "params": [ { \
# "Direction": "*out",
# "Category": "call",
# #"RequestType": "*raw",
# #"ToR": "*monetary",
# "Tenant": "cgrates.org",
# "Account": "6140000",
# "Subject": "6141111124211",
# "Destination": "6141111124211",
# "AnswerTime": "2022-02-15 13:07:39",
# "SetupTime": "2022-02-15 13:07:30",
# "Usage": "181s",
# "OriginID": "API Function Example"
# }], "id": 0})
# pprint.pprint(cdr)