JSON-RPC 方法
使用参数、结果和错误处理来记录 JSON-RPC 方法
OpenRPC 中的方法是 JSON-RPC API 的核心构建块。每个方法都定义了过程名称、参数、返回值和潜在错误。
openrpc.yml
methods:- name: user.createsummary: Create a new userdescription: Creates a new user account with the provided informationparams:- name: userDataschema:$ref: '#/components/schemas/CreateUserRequest'required: truedescription: User data for account creationresult:name: createdUserschema:$ref: '#/components/schemas/User'description: The newly created user objectexamples:- name: CreateUserExampledescription: Example of creating a userparams:userData:email: "john@example.com"name: "John Doe"age: 30result:id: "user_123"email: "john@example.com"name: "John Doe"age: 30createdAt: "2024-01-15T10:30:00Z"
方法参数
JSON-RPC 中的参数可以是位置参数(按位置)或命名参数(按名称):
命名参数
openrpc.yml
methods:- name: calculate.addsummary: Add two numbersparamStructure: by-nameparams:- name: aschema:type: numberrequired: truedescription: First number- name: bschema:type: numberrequired: truedescription: Second numberresult:name: sumschema:type: number
位置参数
openrpc.yml
methods:- name: calculate.multiplysummary: Multiply two numbersparamStructure: by-positionparams:- schema:type: numberdescription: First number (multiplicand)- schema:type: numberdescription: Second number (multiplier)result:name: productschema:type: number
复杂参数类型
为方法参数使用复杂的模式:
openrpc.yml
methods:- name: order.createsummary: Create a new orderdescription: Creates a new order with items and shipping informationparams:- name: orderDataschema:type: objectproperties:items:type: arrayitems:$ref: '#/components/schemas/OrderItem'minItems: 1shippingAddress:$ref: '#/components/schemas/Address'paymentMethod:type: stringenum: [credit_card, paypal, bank_transfer]notes:type: stringmaxLength: 500required:- items- shippingAddress- paymentMethodrequired: trueresult:name: orderschema:$ref: '#/components/schemas/Order'
方法结果
定义成功方法响应的结构:
openrpc.yml
methods:- name: search.productssummary: Search for productsparams:- name: queryschema:type: stringrequired: trueresult:name: searchResultsschema:type: objectproperties:products:type: arrayitems:$ref: '#/components/schemas/Product'totalCount:type: integerhasMore:type: booleannextCursor:type: stringrequired:- products- totalCount- hasMore
错误处理
为您的方法定义自定义错误:
openrpc.yml
methods:- name: user.loginsummary: Authenticate userparams:- name: emailschema:type: stringformat: emailrequired: trueerrors:- code: -32001message: Invalid credentialsdata:type: objectproperties:error:type: stringconst: "Email or password is incorrect"- code: -32002message: Account lockeddata:type: objectproperties:error:type: stringconst: "Account temporarily locked due to failed login attempts"unlockTime:type: stringformat: date-timeresult:name: authResultschema:$ref: '#/components/schemas/AuthToken'
方法示例
提供全面的示例以获得更好的文档:
openrpc.yml
methods:- name: file.uploadsummary: Upload a fileparams:- name: fileDataschema:type: objectproperties:filename:type: stringcontent:type: stringformat: base64mimeType:type: stringrequired: trueexamples:- name: ImageUploaddescription: Upload a JPEG imageparams:fileData:filename: "photo.jpg"content: "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAABAAEDASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAv/xAAUEAEAAAAAAAAAAAAAAAAAAAAA/8QAFQEBAQAAAAAAAAAAAAAAAAAAAAX/xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwC/gA=="mimeType: "image/jpeg"result:fileId: "file_abc123"url: "https://cdn.example.com/files/file_abc123.jpg"size: 1024000- name: DocumentUploaddescription: Upload a PDF documentparams:fileData:filename: "document.pdf"content: "JVBERi0xLjQKJdPr6eEKMSAwIG9iago8PAovVHlwZSAvQ2F0YWxvZwovUGFnZXMgMiAwIFIKPj4KZW5kb2JqCgoyIDAgb2JqCjw8Ci9UeXBlIC9QYWdlcwo="mimeType: "application/pdf"result:fileId: "file_def456"url: "https://cdn.example.com/files/file_def456.pdf"size: 256000
可选参数
定义带有默认值的可选参数:
openrpc.yml
methods:- name: report.generatesummary: Generate a reportparams:- name: reportTypeschema:type: stringenum: [daily, weekly, monthly, yearly]required: true- name: formatschema:type: stringenum: [pdf, csv, json]default: pdfrequired: falsedescription: Output format (defaults to PDF)- name: includeChartsschema:type: booleandefault: truerequired: falsedescription: Include charts in the reportresult:name: reportschema:$ref: '#/components/schemas/Report'
方法是 JSON-RPC API 的基础,提供了一个清晰的契约来说明哪些操作是可用的、它们期望什么数据以及返回什么。