Skip to content

Commit e41952c

Browse files
authored
Merge pull request #13 from weex/api-spec-create
WIP: transfer api sketch into yaml spec, test /trust
2 parents 48eac41 + 39ec07b commit e41952c

File tree

3 files changed

+99
-40
lines changed

3 files changed

+99
-40
lines changed

README.md

+1-28
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,4 @@ python -m test
3939

4040
## REST API
4141

42-
* All requests via HTTP GET except where noted.
43-
* Data returned as JSON
44-
* All responses include `status` (an HTTP standard RFC 2616 response code) and `message` populated on error.
45-
46-
/ - returns basic info
47-
48-
/status - returns uptime and free space
49-
50-
### /trust (POST) - set or update single trust value
51-
Either value or delta must be set.
52-
53-
Parameters
54-
source - string
55-
dest - string
56-
value - set trust value from source to dest, float in range (-100.0, 100.0)
57-
delta - amount to increment or decrement trust value, float in range (-200.0, 200)
58-
59-
### /score - return score from source to dest
60-
Parameters
61-
source - string
62-
dest - string
63-
64-
Returns
65-
trusted - boolean
66-
67-
### /rescore - recalculate scores
68-
Parameters
69-
(none)
42+
The API is defined in openapi-spec.yaml

openapi-spec.yaml

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
openapi: 3.0.0
2+
info:
3+
version: 1.0.0
4+
title: Web of Trust API
5+
description: Providing API access to update trust scores, retrieve trusted state and recalculate scoring.
6+
7+
servers:
8+
- url: http://localhost:20202
9+
10+
paths:
11+
/trust:
12+
post:
13+
description: Set or update single trust value
14+
requestBody:
15+
required: true
16+
content:
17+
application/json:
18+
schema:
19+
type: object
20+
required:
21+
- source
22+
- target
23+
properties:
24+
source:
25+
description: Giver of trust
26+
type: string
27+
target:
28+
description: Recipient of trust
29+
type: string
30+
value:
31+
type: number
32+
delta:
33+
type: number
34+
responses:
35+
'200':
36+
description: Success set or updated a trust value
37+
/score:
38+
get:
39+
description: Set or update single trust value
40+
requestBody:
41+
required: true
42+
content:
43+
application/json:
44+
schema:
45+
type: object
46+
required:
47+
- source
48+
- target
49+
properties:
50+
source:
51+
description: Giver of trust
52+
type: string
53+
target:
54+
description: Recipient of trust
55+
type: string
56+
responses:
57+
'200':
58+
description: Successfully retrieved
59+
content:
60+
application/json:
61+
schema:
62+
type: object
63+
properties:
64+
trusted:
65+
type: boolean
66+
'400':
67+
description: Invalid request
68+
content:
69+
application/json:
70+
schema:
71+
type: object
72+
properties:
73+
message:
74+
type: string
75+
/rescore:
76+
get:
77+
description: Recalculate scores
78+
responses:
79+
'200':
80+
description: Successfully retrieved
81+
'400':
82+
description: Invalid request
83+
content:
84+
application/json:
85+
schema:
86+
type: object
87+
properties:
88+
message:
89+
type: string

server.py

+9-12
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,13 @@ def put():
9393
try:
9494
body = request.data.decode('utf-8')
9595
in_obj = json.loads(body)
96+
s = in_obj['source'] # source user's id
97+
t = in_obj['target'] # target user's id
98+
v = in_obj['value'] # integer value for rating
9699
except:
97-
return ("JSON Decode failed", 400, {'Content-Type':'text/plain'})
98-
99-
s = in_obj['source'] # source user's id
100-
t = in_obj['target'] # target user's id
101-
v = in_obj['value'] # integer value for rating
100+
body = '{"message": "JSON Decode failed"}'
101+
return (body, 400, {'Content-length': 0,
102+
'Content-Type':'text/plain'})
102103

103104
trust = db.session.query(Trust).filter(and_(Trust.user_id == s, Trust.user_id2 == t)).first()
104105
if trust is None:
@@ -109,13 +110,9 @@ def put():
109110
trust.value = v
110111
db.session.commit()
111112

112-
body = json.dumps({'result': 'success'})
113-
code = 201
114-
115-
return (body, code, {'Content-length': len(body),
116-
'Content-type': 'application/json',
117-
}
118-
)
113+
return ('', 200, {'Content-length': 0,
114+
'Content-type': 'application/json',
115+
})
119116

120117
@app.route('/delete', methods=['POST'])
121118
def delete():

0 commit comments

Comments
 (0)