Advertisement
Guest User

Untitled

a guest
May 8th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.42 KB | None | 0 0
  1. here is a detailed walk-through for these functionalitie, with examples:
  2.  
  3.  
  4.  
  5. Here is a quick how-to on items uploading, product search, and bidding with our API. For the purpose of this demo, we assume these two users are registered in the platform (which is the case) :
  6.  
  7. Username : testuser1
  8. Password : 456789
  9. Public key : MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEBKmYzcU5rcQk85vynuwzgrzQjVsX6tIf6dHSGRm9aRIrdYfjANhD3Mmr7WtZOgol/SSInC/c88vs8Ywioxd1Aw==
  10.  
  11. Username : testuser2
  12. Password : 456789
  13. public key : MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAELLR99w0PTlbvTjiorSN/Q7MeR9kAxmRBXqJUiW204qNs8Njzbjlm+9D0//mjKfUnUhNBUMl2CvE726ewpi4cfg==
  14.  
  15.  
  16. 1) Item uploading
  17.  
  18. For a user to upload an item in the marketplace, he need to be admin of a marketplace shop. For him to become so, he can create a shop (and will automatically be admin of this shop), or be granted admin rights by a already existing shop admin. Here we assume he create a shop :
  19.  
  20. For a user to create a shop, he sould call the /ledger/{channel}/{chaincode}/{function} endpoint in POST mode, meaning he will trigger a chaincode function that will write into the ledger.
  21.  
  22. As decribed here : https://api.plastictwist.com/cc_docs/ , the "marketplace" chaincode on channel "ptwist" have a function "new_shop" :
  23.  
  24. Parameters :
  25.  
  26. 1. The shop name
  27. 2. The ERC20 address to transfer money to when a purchase occures
  28.  
  29. Payload :
  30. With a successful shop creation, the function returns the new shop ID.
  31.  
  32. The parameters are passed in the form of an array in the "params" parameter to the API, with pipes (|) as value separator.
  33.  
  34. Knowing all that, here is the curl command to create a shop name "My awesome shop" for testuser1 :
  35.  
  36. curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'X-request-username: testuser1' --header 'X-request-password: 456789' --header 'params: My awesome Shop|MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEBKmYzcU5rcQk85vynuwzgrzQjVsX6tIf6dHSGRm9aRIrdYfjANhD3Mmr7WtZOgol/SSInC/c88vs8Ywioxd1Aw==' 'http://api.plastictwist.com/ledger/ptwist/marketplace/new_shop'
  37.  
  38. This request returns this json :
  39.  
  40. {
  41. "status": "200",
  42. "payload": "2aea532a1b39238024a047da52096ef048bfcca6c06d892c4964c5c7734867aa",
  43. "message": ""
  44. }
  45.  
  46. The status is 200 so everything worked fine, the payload represents a shopID, that will be needed to then post items in this shop.
  47.  
  48. If the item uploading does not follow the shop creation, there is a way to retreive any user shop list with this call to "query_data" function, which allows to perform a mango query on the ledger, here it is, for information:
  49.  
  50. Mango query :
  51.  
  52. {
  53. "selector": {
  54. "DocType": "Shop",
  55. "Users": {
  56. "$elemMatch": {
  57. "$eq": "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEBKmYzcU5rcQk85vynuwzgrzQjVsX6tIf6dHSGRm9aRIrdYfjANhD3Mmr7WtZOgol/SSInC/c88vs8Ywioxd1Aw=="
  58. }
  59. }
  60. }
  61. }
  62.  
  63. Call to query data :
  64.  
  65. curl -X GET --header 'Accept: application/json' --header 'X-request-username: testuser1' --header 'X-request-password: 456789' --header 'params: {"selector":{"DocType":"Shop","Users":{"$elemMatch":{"$eq":"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEBKmYzcU5rcQk85vynuwzgrzQjVsX6tIf6dHSGRm9aRIrdYfjANhD3Mmr7WtZOgol/SSInC/c88vs8Ywioxd1Aw=="}}}}' 'http://api.plastictwist.com/ledger/ptwist/marketplace/query_data'
  66.  
  67. Returns :
  68.  
  69. {
  70. "status": "200",
  71. "response": [
  72. {
  73. "Key": "2aea532a1b39238024a047da52096ef048bfcca6c06d892c4964c5c7734867aa",
  74. "Record": {
  75. "DocType": "Shop",
  76. "ERC20Address": "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEBKmYzcU5rcQk85vynuwzgrzQjVsX6tIf6dHSGRm9aRIrdYfjANhD3Mmr7WtZOgol/SSInC/c88vs8Ywioxd1Aw==",
  77. "Items": null,
  78. "Name": "My awesome Shop",
  79. "Raw": null,
  80. "Users": [
  81. "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEBKmYzcU5rcQk85vynuwzgrzQjVsX6tIf6dHSGRm9aRIrdYfjANhD3Mmr7WtZOgol/SSInC/c88vs8Ywioxd1Aw=="
  82. ]
  83. }
  84. }
  85. ],
  86. "message": ""
  87. }
  88.  
  89. So we can see the "Key" field corresponds to the shop id we created earlier, i will come back to the query_data function later for further explanations.
  90.  
  91.  
  92. So now we have our shop id, which is : 2aea532a1b39238024a047da52096ef048bfcca6c06d892c4964c5c7734867aa
  93.  
  94. What we now need to do is to call the "shop_add_item" function, to create an item.
  95.  
  96. This function require those parameters:
  97. Parameters :
  98.  
  99. 1 . The shop ID
  100. 2 . The item json (ex: { "Bidable": false, "Pictures": [ "picA", "picB" ], "Shipping": [ { "To": "anywhere", "Fee": 12 }, { "To": "fr", "Fee": 3 } ], "Name": "My new item", "Detail": "This is a wonderful item, really !", "Price": 42, "Quantity": 314, "Duration": 604800, "Weight": 45, "Latitude": 48.9070592, "Longitude": 2.3371776 } )
  101.  
  102. Payload :
  103. With a successful operation, the function returns the new item ID.
  104.  
  105. So our first parameter will be our shop id, then a json description of our item, which will be this one :
  106.  
  107. {
  108. "Bidable": true,
  109. "Pictures": [
  110. "https://assets.pinshape.com/uploads/image/file/139445/container_spiral-vase-3d-printing-139445.jpg"
  111. ],
  112. "Shipping": [
  113. {
  114. "To": "anywhere",
  115. "Fee": 12
  116. },
  117. {
  118. "To": "fr",
  119. "Fee": 3
  120. }
  121. ],
  122. "Name": "3d printed vase",
  123. "Detail": "Lorem ipsum dolores sin amet...",
  124. "Price": 2,
  125. "Quantity": 1,
  126. "Duration": 168,
  127. "Weight": 45,
  128. "Latitude": 48.9070592,
  129. "Longitude": 2.3371776
  130. }
  131.  
  132. Meaning the item will be a biddable item, have said characteristics desribed by the json etc, the duration is specified in hours, 168 hours is corresponding to a week (7x24 hours)
  133.  
  134. Here is the call in curl :
  135.  
  136. curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'X-request-username: testuser1' --header 'X-request-password: 456789' --header 'params: 2aea532a1b39238024a047da52096ef048bfcca6c06d892c4964c5c7734867aa|{"Bidable":true,"Pictures":["https://assets.pinshape.com/uploads/image/file/139445/container_spiral-vase-3d-printing-139445.jpg"],"Shipping":[{"To":"anywhere","Fee":12},{"To":"fr","Fee":3}],"Name":"3d printed vase","Detail":"Lorem ipsum dolores sin amet...","Price":2,"Quantity":1,"Duration":168,"Weight":45,"Latitude":48.9070592,"Longitude":2.3371776}' 'http://api.plastictwist.com/ledger/ptwist/marketplace/shop_add_item'
  137.  
  138. Response :
  139.  
  140. {
  141. "status": "200",
  142. "payload": "26b9627a57ab138a8aef5231b044f5e442cb544866468ca93e4160ede213d0f1",
  143. "message": ""
  144. }
  145.  
  146.  
  147. Status is 200, payload is the item id in the marketplace. It's done, our item is online !
  148.  
  149.  
  150.  
  151. 2) Search
  152.  
  153. Now, lets say we are testuser2, and want to search among marketplace items, for a vase.
  154.  
  155. We will make use of the query_data function, that allows us to query the ledger using mango query, for information, here is the data model :
  156.  
  157. Shops:
  158.  
  159. Name string
  160. Users []string
  161. Items []string
  162. Raw []string
  163. DocType string //Will be "Shop"
  164. ERC20Address string
  165.  
  166. Items:
  167.  
  168. Biddable bool
  169. DocType string //Will be "ShopItem"
  170. Pictures []string
  171. Shipping []ShippingFee
  172. Name string
  173. Detail string
  174. Price uint64
  175. Quantity uint64
  176. MinQuantity uint64
  177. CreationDate uint64
  178. ExpireDate uint64
  179. ShopId string
  180. BidList []string
  181. Winner string
  182. Latitude float64
  183. Longitude float64
  184. Weight int
  185.  
  186. Bids:
  187.  
  188. ItemId string
  189. Price uint64
  190. DocType string //Will be "Bid"
  191. Owner string
  192. ShownPrice uint64
  193. Timestamp uint64
  194.  
  195. Sales:
  196.  
  197. User string
  198. ItemId string
  199. ShopId string
  200. Price uint64
  201. Quantity uint64
  202. StateShop uint8
  203. StateUser uint8
  204. DocType string //Will be "Sale"
  205.  
  206.  
  207. And here is more infos about mango query operators and syntax :
  208. https://docs.mongodb.com/manual/reference/operator/query/
  209.  
  210. So, with this information, we can constuct a query that returns the items with a name matching a particular regex, ".*vase.*" for example here.
  211.  
  212. Here is the mango query :
  213.  
  214. {
  215. "selector": {
  216. "DocType": "ShopItem",
  217. "Name": {
  218. "$regex": ".*vase.*"
  219. },
  220. "ExpireDate": {
  221. "$gt": 1554456813
  222. }
  223. }
  224. }
  225.  
  226. Notice that you need to manually input a "ExpireDate" field check, ensuring it is greater than current timestamp, for the query to not return expired items.
  227.  
  228. Here is the curl command that performs our search :
  229.  
  230. curl -X GET --header 'Accept: application/json' --header 'X-request-username: testuser2' --header 'X-request-password: 456789' --header 'params: {"selector":{"DocType":"ShopItem","Name":{"$regex":".*vase.*"},"ExpireDate":{"$gt":1554456813}}}' 'http://api.plastictwist.com/ledger/ptwist/marketplace/query_data'
  231.  
  232. Response :
  233.  
  234.  
  235. {
  236. "status": "200",
  237. "response": [
  238. {
  239. "Key": "26b9627a57ab138a8aef5231b044f5e442cb544866468ca93e4160ede213d0f1",
  240. "Record": {
  241. "BidList": null,
  242. "Biddable": true,
  243. "CreationDate": 1554456251,
  244. "Detail": "Lorem ipsum dolores sin amet...",
  245. "DocType": "ShopItem",
  246. "ExpireDate": 1555061051,
  247. "Latitude": 48.9070592,
  248. "Longitude": 2.3371776,
  249. "MinQuantity": 0,
  250. "Name": "3d printed vase",
  251. "Pictures": [
  252. "https://assets.pinshape.com/uploads/image/file/139445/container_spiral-vase-3d-printing-139445.jpg"
  253. ],
  254. "Price": 2,
  255. "Quantity": 1,
  256. "Shipping": [
  257. {
  258. "Fee": 12,
  259. "To": "anywhere"
  260. },
  261. {
  262. "Fee": 3,
  263. "To": "fr"
  264. }
  265. ],
  266. "ShopId": "2aea532a1b39238024a047da52096ef048bfcca6c06d892c4964c5c7734867aa",
  267. "Weight": 45,
  268. "Winner": ""
  269. }
  270. }
  271. ],
  272. "message": ""
  273. }
  274.  
  275. Here is the list of items corresponding to our search, only one in this case, note the "Key" field is the item id
  276.  
  277. 3 ) Bidding
  278.  
  279. Now for the bidding functionality, let's assume our testuser2 want to bid on this vase he found by searching, he have to call the "make_bid" function, described here :
  280.  
  281. Parameters :
  282.  
  283. Bid submission json (ex: { "ItemId": "ER63H...", "Price": 42 } )
  284.  
  285. Payload :
  286. With a successful bidding, the functions returns the bid ID.
  287.  
  288. The json sent as parameter contains ItemId, the identifier of the item our user want to bid on, and the Price he is willing to pay.
  289.  
  290. In our case, let's make him bid 10 :
  291.  
  292. {"ItemId" : "26b9627a57ab138a8aef5231b044f5e442cb544866468ca93e4160ede213d0f1", "Price" : 10 }
  293.  
  294. Curl call :
  295.  
  296. curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'X-request-username: testuser2' --header 'X-request-password: 456789' --header 'params: {"ItemId" : "26b9627a57ab138a8aef5231b044f5e442cb544866468ca93e4160ede213d0f1", "Price" : 10 }' 'http://api.plastictwist.com/ledger/ptwist/marketplace/make_bid'
  297.  
  298. Response :
  299.  
  300. {
  301. "status": "200",
  302. "payload": "f4baa07efafc8e23308e7ddb991229c9188ea9aa1db7f663468da2355d3d4a54",
  303. "message": ""
  304. }
  305.  
  306. The returned payload is the bid Id.
  307.  
  308. Note that now if we run again our search command, we now get this result :
  309.  
  310. {
  311. "status": "200",
  312. "response": [
  313. {
  314. "Key": "26b9627a57ab138a8aef5231b044f5e442cb544866468ca93e4160ede213d0f1",
  315. "Record": {
  316. "BidList": [
  317. "f4baa07efafc8e23308e7ddb991229c9188ea9aa1db7f663468da2355d3d4a54"
  318. ],
  319. "Biddable": true,
  320. "CreationDate": 1554456251,
  321. "Detail": "Lorem ipsum dolores sin amet...",
  322. "DocType": "ShopItem",
  323. "ExpireDate": 1555061051,
  324. "Latitude": 48.9070592,
  325. "Longitude": 2.3371776,
  326. "MinQuantity": 0,
  327. "Name": "3d printed vase",
  328. "Pictures": [
  329. "https://assets.pinshape.com/uploads/image/file/139445/container_spiral-vase-3d-printing-139445.jpg"
  330. ],
  331. "Price": 3,
  332. "Quantity": 1,
  333. "Shipping": [
  334. {
  335. "Fee": 12,
  336. "To": "anywhere"
  337. },
  338. {
  339. "Fee": 3,
  340. "To": "fr"
  341. }
  342. ],
  343. "ShopId": "2aea532a1b39238024a047da52096ef048bfcca6c06d892c4964c5c7734867aa",
  344. "Weight": 45,
  345. "Winner": "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAELLR99w0PTlbvTjiorSN/Q7MeR9kAxmRBXqJUiW204qNs8Njzbjlm+9D0//mjKfUnUhNBUMl2CvE726ewpi4cfg=="
  346. }
  347. }
  348. ],
  349. "message": ""
  350. }
  351.  
  352. The bid is present in Bidlist field, the winner is testuser2 public key, and the price has increased. (not to ten but previous price + 1, since the bindding mechanism works the same way as ebay works)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement