11use super :: {
22 entity:: {
3- publish_hosted_response:: PublishHostedResponse , publish_ipfs_response:: PublishIpfsResponse ,
3+ publish_hosted_response:: PublishHostedResponse , publish_ipfs_response:: PublishIpfsResponse , publish_ipns_response :: PublishIpnsResponse ,
44 } ,
55 AvailabilityError ,
66} ;
@@ -79,6 +79,40 @@ impl<H: Client> AvailabilityService<H> {
7979 . await
8080 . map_err ( |e| AvailabilityError :: RetrieveError ( e. to_string ( ) ) . into ( ) )
8181 }
82+
83+ pub async fn publish_ipns ( & self , record : Record , key_id : String ) -> BloockResult < String > {
84+ let url = format ! (
85+ "{}/hosting/v1/ipns/upload?key={}" ,
86+ self . config_service. get_api_base_url( ) ,
87+ key_id,
88+ ) ;
89+
90+ let response: PublishIpnsResponse = self
91+ . http
92+ . post_file (
93+ url,
94+ vec ! [ ( "payload" . to_owned( ) , record. serialize( ) ?. to_vec( ) ) ] ,
95+ vec ! [ ] ,
96+ None ,
97+ None ,
98+ )
99+ . await
100+ . map_err ( |e| AvailabilityError :: PublishError ( e. to_string ( ) ) ) ?;
101+ Ok ( response. id )
102+ }
103+
104+ pub async fn retrieve_ipns ( & self , id : String ) -> BloockResult < Vec < u8 > > {
105+ let url = format ! (
106+ "{}/hosting/v1/ipns/{}" ,
107+ self . config_service. get_cdn_base_url( ) ,
108+ id
109+ ) ;
110+
111+ self . http
112+ . get ( url, None )
113+ . await
114+ . map_err ( |e| AvailabilityError :: RetrieveError ( e. to_string ( ) ) . into ( ) )
115+ }
82116}
83117
84118#[ cfg( test) ]
@@ -88,6 +122,7 @@ mod tests {
88122 use std:: sync:: Arc ;
89123
90124 use crate :: availability:: entity:: publish_ipfs_response:: PublishIpfsResponse ;
125+ use crate :: availability:: entity:: publish_ipns_response:: PublishIpnsResponse ;
91126 use crate :: availability:: { self , entity:: publish_hosted_response:: PublishHostedResponse } ;
92127 use crate :: record:: document:: Document ;
93128 use crate :: record:: entity:: record:: Record ;
@@ -217,4 +252,69 @@ mod tests {
217252 "Retrieved payload should match expected"
218253 )
219254 }
255+
256+ #[ tokio:: test]
257+ async fn ipns_publish_file ( ) {
258+ let payload: Vec < u8 > = vec ! [ 1 , 2 , 3 , 4 , 5 ] ;
259+ let response = PublishIpnsResponse {
260+ id : "bafzbeigkhrj52va6uud2eeavfnfsr7zgo6sf7wg6bq76iqf37c274q2dy4" . to_owned ( ) ,
261+ } ;
262+ let key_id: String = "b012eaf2-e2ed-4196-8a68-172238852a5f" . to_owned ( ) ;
263+
264+ let mut http = MockClient :: default ( ) ;
265+ let url = format ! ( "https://api.bloock.com/hosting/v1/ipns/upload?key={}" , key_id) ;
266+ http. expect_post_file :: < String , PublishIpnsResponse > ( )
267+ . with (
268+ eq ( url. to_owned ( ) ) ,
269+ eq ( vec ! [ ( "payload" . to_owned( ) , payload. to_vec( ) ) ] ) ,
270+ eq ( vec ! [ ] ) ,
271+ eq ( None ) ,
272+ eq ( None ) ,
273+ )
274+ . return_once ( |_, _, _, _, _| Ok ( response) ) ;
275+
276+ let service = availability:: configure_test ( Arc :: new ( http) ) ;
277+
278+ let record = Record :: new (
279+ Document :: new (
280+ & payload,
281+ service. config_service . get_api_base_url ( ) ,
282+ service. config_service . get_api_key ( ) ,
283+ )
284+ . unwrap ( ) ,
285+ ) ;
286+
287+ let result = service. publish_ipns ( record. unwrap ( ) , key_id) . await . unwrap ( ) ;
288+
289+ assert_eq ! (
290+ & result, "bafzbeigkhrj52va6uud2eeavfnfsr7zgo6sf7wg6bq76iqf37c274q2dy4" ,
291+ "Should not return an empty result"
292+ )
293+ }
294+
295+ #[ tokio:: test]
296+ async fn ipns_retrieve_file ( ) {
297+ let payload = [ 1 , 2 , 3 , 4 , 5 ] ;
298+ let id = "bafzbeigkhrj52va6uud2eeavfnfsr7zgo6sf7wg6bq76iqf37c274q2dy4" ;
299+
300+ let mut http = MockClient :: default ( ) ;
301+ http. expect_get :: < String > ( )
302+ . with (
303+ eq ( format ! ( "https://cdn.bloock.com/hosting/v1/ipns/{id}" ) ) ,
304+ eq ( None ) ,
305+ )
306+ . return_once ( move |_, _| Ok ( payload. to_vec ( ) ) ) ;
307+
308+ let service = availability:: configure_test ( Arc :: new ( http) ) ;
309+ let result = service
310+ . retrieve_ipns ( "bafzbeigkhrj52va6uud2eeavfnfsr7zgo6sf7wg6bq76iqf37c274q2dy4" . to_owned ( ) )
311+ . await
312+ . unwrap ( ) ;
313+
314+ assert_eq ! (
315+ payload,
316+ result. as_slice( ) ,
317+ "Retrieved payload should match expected"
318+ )
319+ }
220320}
0 commit comments