Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // Package configure 使用示例
  2. //
  3. // 以下示例演示如何使用配置中心客户端SDK
  4. package test
  5. import (
  6. "context"
  7. "fmt"
  8. "log"
  9. "time"
  10. "git.x2erp.com/qdy/go-base/model/request/queryreq"
  11. "git.x2erp.com/qdy/go-base/sdk/configure"
  12. )
  13. // ExampleUsage 演示SDK的基本用法
  14. func ExampleUsage() {
  15. // 示例1:创建客户端
  16. exampleCreateClient()
  17. // 示例2:查询列表
  18. exampleListTables()
  19. // 示例3:创建表
  20. exampleSaveTable()
  21. // 示例4:查询详情
  22. exampleGetTable()
  23. // 示例5:删除表
  24. exampleDeleteTable()
  25. }
  26. func exampleCreateClient() {
  27. fmt.Println("=== 示例1:创建客户端 ===")
  28. // 方法1:使用默认配置
  29. client1, err := configure.NewClient()
  30. if err != nil {
  31. log.Printf("Failed to create default client: %v", err)
  32. } else {
  33. fmt.Printf("Default client created: URL=%s\n", client1.GetConfig().BaseURL)
  34. }
  35. // 方法2:使用Basic认证
  36. if _, err := configure.NewBasicAuthClient("http://localhost:8080", "admin", "123"); err != nil {
  37. log.Printf("Failed to create basic auth client: %v", err)
  38. } else {
  39. fmt.Println("Basic auth client created")
  40. }
  41. // 方法3:使用Token认证
  42. if _, err := configure.NewTokenAuthClient("http://localhost:8080", "your-token-here"); err != nil {
  43. log.Printf("Failed to create token auth client: %v", err)
  44. } else {
  45. fmt.Println("Token auth client created")
  46. }
  47. fmt.Println()
  48. }
  49. func exampleListTables() {
  50. fmt.Println("=== 示例2:查询数据库表字典列表 ===")
  51. // 创建客户端
  52. client, err := configure.NewBasicAuthClient("http://localhost:8080", "admin", "123")
  53. if err != nil {
  54. log.Printf("Failed to create client: %v", err)
  55. return
  56. }
  57. // 创建查询请求
  58. query := &configure.DicTableQueryRequest{
  59. QueryRequest: queryreq.QueryRequest{
  60. Page: 0,
  61. PageSize: 10,
  62. },
  63. }
  64. // 执行查询
  65. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  66. defer cancel()
  67. result, err := client.ListTables(ctx, query)
  68. if err != nil {
  69. log.Printf("Error listing tables: %v", err)
  70. return
  71. }
  72. fmt.Printf("Total tables: %d\n", result.TotalCount)
  73. fmt.Printf("Last page: %d\n", result.LastPage)
  74. fmt.Println()
  75. }
  76. func exampleSaveTable() {
  77. fmt.Println("=== 示例3:创建数据库表字典 ===")
  78. // 创建客户端
  79. client, err := configure.NewBasicAuthClient("http://localhost:8080", "admin", "123")
  80. if err != nil {
  81. log.Printf("Failed to create client: %v", err)
  82. return
  83. }
  84. // 创建请求
  85. req := &configure.DicTableRequest{
  86. TableID: "example_table_001",
  87. TableType: "实体表",
  88. Name: "示例表001",
  89. Description: "这是一个示例表",
  90. Creator: "admin",
  91. CreatedAt: time.Now(),
  92. UpdatedAt: time.Now(),
  93. Fields: []configure.DicTableFieldRequest{
  94. {
  95. FieldID: "example_table_001.id",
  96. TableID: "example_table_001",
  97. FiledType: "实际字段",
  98. DataType: "数值型",
  99. FieldName: "id",
  100. FieldNameCN: "主键ID",
  101. Description: "主键字段",
  102. Creator: "admin",
  103. CreatedAt: time.Now(),
  104. UpdatedAt: time.Now(),
  105. },
  106. {
  107. FieldID: "example_table_001.name",
  108. TableID: "example_table_001",
  109. FiledType: "实际字段",
  110. DataType: "字符型",
  111. FieldName: "name",
  112. FieldNameCN: "名称",
  113. Description: "名称字段",
  114. Creator: "admin",
  115. CreatedAt: time.Now(),
  116. UpdatedAt: time.Now(),
  117. },
  118. },
  119. }
  120. // 执行保存
  121. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  122. defer cancel()
  123. detail, err := client.SaveTable(ctx, req)
  124. if err != nil {
  125. log.Printf("Error saving table: %v", err)
  126. return
  127. }
  128. fmt.Printf("Table saved successfully: %s\n", detail.Table.TableID)
  129. fmt.Printf("Table name: %s\n", detail.Table.Name)
  130. fmt.Printf("Number of fields: %d\n", len(detail.Fields))
  131. fmt.Println()
  132. }
  133. func exampleGetTable() {
  134. fmt.Println("=== 示例4:查询数据库表字典详情 ===")
  135. // 创建客户端
  136. client, err := configure.NewBasicAuthClient("http://localhost:8080", "admin", "123")
  137. if err != nil {
  138. log.Printf("Failed to create client: %v", err)
  139. return
  140. }
  141. // 查询表详情
  142. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  143. defer cancel()
  144. tableID := "example_table_001"
  145. detail, err := client.GetTable(ctx, tableID)
  146. if err != nil {
  147. if err == configure.ErrNotFound {
  148. fmt.Printf("Table %s not found\n", tableID)
  149. } else {
  150. log.Printf("Error getting table: %v", err)
  151. }
  152. return
  153. }
  154. fmt.Printf("Table ID: %s\n", detail.Table.TableID)
  155. fmt.Printf("Table Type: %s\n", detail.Table.TableType)
  156. fmt.Printf("Name: %s\n", detail.Table.Name)
  157. fmt.Printf("Description: %s\n", detail.Table.Description)
  158. fmt.Printf("Creator: %s\n", detail.Table.Creator)
  159. fmt.Printf("Fields count: %d\n", len(detail.Fields))
  160. fmt.Println()
  161. }
  162. func exampleDeleteTable() {
  163. fmt.Println("=== 示例5:删除数据库表字典 ===")
  164. // 创建客户端
  165. client, err := configure.NewBasicAuthClient("http://localhost:8080", "admin", "123")
  166. if err != nil {
  167. log.Printf("Failed to create client: %v", err)
  168. return
  169. }
  170. // 删除表
  171. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  172. defer cancel()
  173. tableID := "example_table_001"
  174. if err := client.DeleteTable(ctx, tableID); err != nil {
  175. if err == configure.ErrNotFound {
  176. fmt.Printf("Table %s not found\n", tableID)
  177. } else {
  178. log.Printf("Error deleting table: %v", err)
  179. }
  180. return
  181. }
  182. fmt.Printf("Table %s deleted successfully\n", tableID)
  183. fmt.Println()
  184. }