cpp-ipfs-http-client
IPFS C++ client library
client.h
1 /* Copyright (c) 2016-2021, The C++ IPFS client library developers
2 
3 Permission is hereby granted, free of charge, to any person obtaining a copy of
4 this software and associated documentation files (the "Software"), to deal in
5 the Software without restriction, including without limitation the rights to
6 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7 the Software, and to permit persons to whom the Software is furnished to do so,
8 subject to the following conditions:
9 
10 The above copyright notice and this permission notice shall be included in all
11 copies or substantial portions of the Software.
12 
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
19 
20 #ifndef IPFS_CLIENT_H
21 #define IPFS_CLIENT_H
22 
23 #include <ipfs/http/transport.h>
24 
25 #include <iostream>
26 #include <nlohmann/json.hpp>
27 #include <string>
28 #include <utility>
29 #include <vector>
30 
32 namespace ipfs {
33 
36 using Json = nlohmann::json;
37 
46 class Client {
47  public:
54  Client(
56  const std::string& host,
58  long port,
60  const std::string& timeout = "",
62  const std::string& protocol = "http://",
64  const std::string& apiPath = "/api/v0");
65 
67  Client(
69  const Client&);
70 
72  Client(
74  Client&&);
75 
80  const Client&);
81 
86  Client&&);
87 
90  ~Client();
91 
103  void Id(
106  Json* id);
107 
119  void Version(
122  Json* version);
123 
135  void ConfigGet(
138  const std::string& key,
140  Json* config);
141 
153  void ConfigSet(
155  const std::string& key,
157  const Json& value);
158 
170  void ConfigReplace(
172  const Json& config);
173 
185  void DhtFindPeer(
187  const std::string& peer_id,
189  Json* addresses);
190 
202  void DhtFindProvs(
204  const std::string& hash,
206  Json* providers);
207 
219  void BlockGet(
221  const std::string& block_id,
224  std::iostream* block);
225 
237  void BlockPut(
239  const http::FileUpload& block,
241  Json* stat);
242 
254  void BlockStat(
256  const std::string& block_id,
258  Json* stat);
259 
271  void FilesGet(
274  const std::string& path,
277  std::iostream* response);
278 
290  void FilesAdd(
292  const std::vector<http::FileUpload>& files,
296  Json* result);
297 
309  void FilesLs(
311  const std::string& path,
339  Json* result);
340 
352  void KeyGen(
354  const std::string& key_name,
356  const std::string& key_type,
358  size_t key_size,
360  std::string* key_id);
361 
373  void KeyList(
375  Json* key_list);
376 
388  void KeyRm(
390  const std::string& key_name);
391 
403  void NamePublish(
405  const std::string& object_id,
408  const std::string& key_name,
420  const Json& options,
422  std::string* name_id);
423 
435  void NameResolve(
437  const std::string& name_id,
440  std::string* path_string);
441 
453  void ObjectNew(
455  std::string* object_id);
456 
468  void ObjectPut(
470  const Json& object,
473  Json* object_stored);
474 
486  void ObjectGet(
488  const std::string& object_id,
492  Json* object);
493 
505  void ObjectData(
507  const std::string& object_id,
509  std::string* data);
510 
522  void ObjectLinks(
524  const std::string& object_id,
527  Json* links);
528 
540  void ObjectStat(
542  const std::string& object_id,
545  Json* stat);
546 
558  void ObjectPatchAddLink(
560  const std::string& source,
562  const std::string& link_name,
564  const std::string& link_target,
566  std::string* cloned);
567 
580  void ObjectPatchRmLink(
582  const std::string& source,
584  const std::string& link_name,
586  std::string* cloned);
587 
601  const std::string& source,
603  const http::FileUpload& data,
605  std::string* cloned);
606 
618  void ObjectPatchSetData(
620  const std::string& source,
622  const http::FileUpload& data,
624  std::string* cloned);
625 
637  void PinAdd(
639  const std::string& object_id);
640 
652  void PinLs(
654  Json* pinned);
655 
667  void PinLs(
669  const std::string& object_id,
671  Json* pinned);
672 
674  enum class PinRmOptions {
678  RECURSIVE,
679  };
680 
694  void PinRm(
696  const std::string& object_id,
698  PinRmOptions options);
699 
711  void SwarmAddrs(
713  Json* addresses);
714 
726  void SwarmConnect(
730  const std::string& peer);
731 
743  void SwarmDisconnect(
747  const std::string& peer);
748 
760  void SwarmPeers(
762  Json* peers);
763 
764  private:
766  void FetchAndParseJson(
769  const std::string& url,
771  Json* response);
772 
775  void FetchAndParseJson(
777  const std::string& url,
779  const std::vector<http::FileUpload>& files,
781  Json* response);
782 
787  static void ParseJson(
789  const std::string& input,
791  Json* result);
792 
797  template <class PropertyType>
798  static void GetProperty(
800  const Json& input,
802  const std::string& property_name,
804  size_t line_number,
806  PropertyType* property_value);
807 
815  std::string MakeUrl(
817  const std::string& path,
819  const std::vector<std::pair<std::string, std::string>>& parameters = {});
820 
823  std::string url_prefix_;
824 
826  http::Transport* http_;
827 
829  std::string timeout_value_;
830 };
831 } /* namespace ipfs */
832 
833 #endif /* IPFS_CLIENT_H */
ipfs::Client::ObjectNew
void ObjectNew(std::string *object_id)
Create a new MerkleDAG node.
Definition: client.cc:312
ipfs::Client::KeyList
void KeyList(Json *key_list)
List all the keys.
Definition: client.cc:277
ipfs::Client::ConfigGet
void ConfigGet(const std::string &key, Json *config)
Query the current config of the peer.
Definition: client.cc:85
ipfs::Client::ObjectGet
void ObjectGet(const std::string &object_id, Json *object)
Get a MerkleDAG node.
Definition: client.cc:327
ipfs::Client::Client
Client(const std::string &host, long port, const std::string &timeout="", const std::string &protocol="http://", const std::string &apiPath="/api/v0")
Constructor.
Definition: client.cc:34
ipfs::Client::ObjectStat
void ObjectStat(const std::string &object_id, Json *stat)
Get stats about a MerkleDAG node.
Definition: client.cc:347
ipfs::Client::PinRmOptions
PinRmOptions
Options to control the PinRm() method.
Definition: client.h:674
ipfs::Client::ObjectPatchRmLink
void ObjectPatchRmLink(const std::string &source, const std::string &link_name, std::string *cloned)
Create a new object from an existing MerkleDAG node and remove one of its links.
Definition: client.cc:365
ipfs::Client
IPFS client.
Definition: client.h:46
ipfs::Client::FilesLs
void FilesLs(const std::string &path, Json *result)
List directory contents for Unix filesystem objects.
Definition: client.cc:262
ipfs::Client::SwarmAddrs
void SwarmAddrs(Json *addresses)
List of known addresses of each peer connected.
Definition: client.cc:437
ipfs::Client::ConfigSet
void ConfigSet(const std::string &key, const Json &value)
Add or replace a config knob at the peer.
Definition: client.cc:111
ipfs::Client::ObjectPatchSetData
void ObjectPatchSetData(const std::string &source, const http::FileUpload &data, std::string *cloned)
Create a new object from an existing MerkleDAG node and set its data.
Definition: client.cc:388
ipfs::Client::FilesGet
void FilesGet(const std::string &path, std::iostream *response)
Get a file from IPFS.
Definition: client.cc:199
ipfs::Client::PinAdd
void PinAdd(const std::string &object_id)
Pin a given IPFS object.
Definition: client.cc:399
ipfs::Client::FilesAdd
void FilesAdd(const std::vector< http::FileUpload > &files, Json *result)
Add files to IPFS.
Definition: client.cc:203
ipfs::Client::ConfigReplace
void ConfigReplace(const Json &config)
Replace the entire config at the peer.
Definition: client.cc:117
ipfs::Client::DhtFindPeer
void DhtFindPeer(const std::string &peer_id, Json *addresses)
Retrieve the peer info of a reachable node in the network.
Definition: client.cc:125
ipfs::Client::BlockStat
void BlockStat(const std::string &block_id, Json *stat)
Get information for a raw IPFS block.
Definition: client.cc:195
ipfs::Client::ObjectData
void ObjectData(const std::string &object_id, std::string *data)
Get the data field of a MerkleDAG node.
Definition: client.cc:331
ipfs::Client::SwarmPeers
void SwarmPeers(Json *peers)
List the peers that we have connections with.
Definition: client.cc:451
ipfs::Client::BlockGet
void BlockGet(const std::string &block_id, std::iostream *block)
Get a raw IPFS block.
Definition: client.cc:187
ipfs::Client::KeyRm
void KeyRm(const std::string &key_name)
Remove a key.
Definition: client.cc:283
ipfs::Client::PinRmOptions::RECURSIVE
@ RECURSIVE
Recursively unpin the objects.
ipfs::Client::ObjectPut
void ObjectPut(const Json &object, Json *object_stored)
Store a MerkleDAG node.
Definition: client.cc:320
ipfs::Json
nlohmann::json Json
Type of the output of some methods, aliased for convenience.
Definition: client.h:36
ipfs::Client::~Client
~Client()
Destructor.
Definition: client.cc:77
ipfs::Client::ObjectLinks
void ObjectLinks(const std::string &object_id, Json *links)
Get links of a MerkleDAG node.
Definition: client.cc:339
ipfs::Client::PinLs
void PinLs(Json *pinned)
List all the objects pinned to local storage.
Definition: client.cc:418
ipfs::Client::SwarmConnect
void SwarmConnect(const std::string &peer)
Open a connection to a given address.
Definition: client.cc:441
ipfs::Client::NameResolve
void NameResolve(const std::string &name_id, std::string *path_string)
Resolve an IPNS name.
Definition: client.cc:304
ipfs::Client::KeyGen
void KeyGen(const std::string &key_name, const std::string &key_type, size_t key_size, std::string *key_id)
Generate a new key.
Definition: client.cc:266
ipfs::Client::operator=
Client & operator=(const Client &)
Copy assignment operator.
Definition: client.cc:49
ipfs::Client::PinRmOptions::NON_RECURSIVE
@ NON_RECURSIVE
Just unpin the specified object.
ipfs::Client::ObjectPatchAddLink
void ObjectPatchAddLink(const std::string &source, const std::string &link_name, const std::string &link_target, std::string *cloned)
Create a new object from an existing MerkleDAG node and add to its links.
Definition: client.cc:351
ipfs::Client::DhtFindProvs
void DhtFindProvs(const std::string &hash, Json *providers)
Retrieve the providers for a content that is addressed by a hash.
Definition: client.cc:156
ipfs::Client::NamePublish
void NamePublish(const std::string &object_id, const std::string &key_name, const Json &options, std::string *name_id)
Publish an IPNS name attached to a given value.
Definition: client.cc:288
ipfs::Client::Id
void Id(Json *id)
Return the identity of the peer.
Definition: client.cc:79
ipfs::Client::BlockPut
void BlockPut(const http::FileUpload &block, Json *stat)
Store a raw block in IPFS.
Definition: client.cc:191
ipfs::Client::ObjectPatchAppendData
void ObjectPatchAppendData(const std::string &source, const http::FileUpload &data, std::string *cloned)
Create a new object from an existing MerkleDAG node and append data to it.
Definition: client.cc:377
ipfs::Client::SwarmDisconnect
void SwarmDisconnect(const std::string &peer)
Close a connection on a given address.
Definition: client.cc:446
ipfs
IPFS namespace.
Definition: client.h:32
ipfs::Client::PinRm
void PinRm(const std::string &object_id, PinRmOptions options)
Unpin an object.
Definition: client.cc:426
ipfs::Client::Version
void Version(Json *version)
Return the implementation version of the peer.
Definition: client.cc:81
ipfs::http::FileUpload
HTTP file upload.
Definition: transport.h:32