cpp-ipfs-http-client
IPFS C++ client library
client.h
1 /* Copyright (c) 2016-2019, 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);
59 
61  Client(
63  const Client&);
64 
66  Client(
68  Client&&);
69 
74  const Client&);
75 
80  Client&&);
81 
84  ~Client();
85 
97  void Id(
100  Json* id);
101 
113  void Version(
116  Json* version);
117 
129  void ConfigGet(
132  const std::string& key,
134  Json* config);
135 
147  void ConfigSet(
149  const std::string& key,
151  const Json& value);
152 
164  void ConfigReplace(
166  const Json& config);
167 
179  void DhtFindPeer(
181  const std::string& peer_id,
183  Json* addresses);
184 
196  void DhtFindProvs(
198  const std::string& hash,
200  Json* providers);
201 
213  void BlockGet(
215  const std::string& block_id,
218  std::iostream* block);
219 
231  void BlockPut(
233  const http::FileUpload& block,
235  Json* stat);
236 
248  void BlockStat(
250  const std::string& block_id,
252  Json* stat);
253 
265  void FilesGet(
268  const std::string& path,
271  std::iostream* response);
272 
284  void FilesAdd(
286  const std::vector<http::FileUpload>& files,
290  Json* result);
291 
303  void FilesLs(
305  const std::string& path,
333  Json* result);
334 
346  void KeyGen(
348  const std::string& key_name,
350  const std::string& key_type,
352  size_t key_size,
354  std::string* key_id);
355 
367  void KeyList(
369  Json* key_list);
370 
382  void KeyRm(
384  const std::string& key_name);
385 
397  void NamePublish(
399  const std::string& object_id,
402  const std::string& key_name,
414  const Json& options,
416  std::string* name_id);
417 
429  void NameResolve(
431  const std::string& name_id,
434  std::string* path_string);
435 
447  void ObjectNew(
449  std::string* object_id);
450 
462  void ObjectPut(
464  const Json& object,
467  Json* object_stored);
468 
480  void ObjectGet(
482  const std::string& object_id,
486  Json* object);
487 
499  void ObjectData(
501  const std::string& object_id,
503  std::string* data);
504 
516  void ObjectLinks(
518  const std::string& object_id,
521  Json* links);
522 
534  void ObjectStat(
536  const std::string& object_id,
539  Json* stat);
540 
552  void ObjectPatchAddLink(
554  const std::string& source,
556  const std::string& link_name,
558  const std::string& link_target,
560  std::string* cloned);
561 
574  void ObjectPatchRmLink(
576  const std::string& source,
578  const std::string& link_name,
580  std::string* cloned);
581 
595  const std::string& source,
597  const http::FileUpload& data,
599  std::string* cloned);
600 
612  void ObjectPatchSetData(
614  const std::string& source,
616  const http::FileUpload& data,
618  std::string* cloned);
619 
631  void PinAdd(
633  const std::string& object_id);
634 
646  void PinLs(
648  Json* pinned);
649 
661  void PinLs(
663  const std::string& object_id,
665  Json* pinned);
666 
668  enum class PinRmOptions {
672  RECURSIVE,
673  };
674 
688  void PinRm(
690  const std::string& object_id,
692  PinRmOptions options);
693 
705  void SwarmAddrs(
707  Json* addresses);
708 
720  void SwarmConnect(
724  const std::string& peer);
725 
737  void SwarmDisconnect(
741  const std::string& peer);
742 
754  void SwarmPeers(
756  Json* peers);
757 
758  private:
760  void FetchAndParseJson(
763  const std::string& url,
765  Json* response);
766 
769  void FetchAndParseJson(
771  const std::string& url,
773  const std::vector<http::FileUpload>& files,
775  Json* response);
776 
781  static void ParseJson(
783  const std::string& input,
785  Json* result);
786 
791  template <class PropertyType>
792  static void GetProperty(
794  const Json& input,
796  const std::string& property_name,
798  size_t line_number,
800  PropertyType* property_value);
801 
809  std::string MakeUrl(
811  const std::string& path,
813  const std::vector<std::pair<std::string, std::string>>& parameters = {});
814 
817  std::string url_prefix_;
818 
820  http::Transport* http_;
821 };
822 } /* namespace ipfs */
823 
824 #endif /* IPFS_CLIENT_H */
~Client()
Destructor.
Definition: client.cc:75
nlohmann::json Json
Type of the output of some methods, aliased for convenience.
Definition: client.h:36
void ObjectNew(std::string *object_id)
Create a new MerkleDAG node.
Definition: client.cc:310
void DhtFindPeer(const std::string &peer_id, Json *addresses)
Retrieve the peer info of a reachable node in the network.
Definition: client.cc:123
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:375
void SwarmDisconnect(const std::string &peer)
Close a connection on a given address.
Definition: client.cc:444
void ConfigGet(const std::string &key, Json *config)
Query the current config of the peer.
Definition: client.cc:83
void SwarmPeers(Json *peers)
List the peers that we have connections with.
Definition: client.cc:449
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:363
void ObjectStat(const std::string &object_id, Json *stat)
Get stats about a MerkleDAG node.
Definition: client.cc:345
void ObjectGet(const std::string &object_id, Json *object)
Get a MerkleDAG node.
Definition: client.cc:325
void BlockPut(const http::FileUpload &block, Json *stat)
Store a raw block in IPFS.
Definition: client.cc:189
void ObjectPut(const Json &object, Json *object_stored)
Store a MerkleDAG node.
Definition: client.cc:318
void PinAdd(const std::string &object_id)
Pin a given IPFS object.
Definition: client.cc:397
void BlockStat(const std::string &block_id, Json *stat)
Get information for a raw IPFS block.
Definition: client.cc:193
void FilesGet(const std::string &path, std::iostream *response)
Get a file from IPFS.
Definition: client.cc:197
void PinLs(Json *pinned)
List all the objects pinned to local storage.
Definition: client.cc:416
void ConfigReplace(const Json &config)
Replace the entire config at the peer.
Definition: client.cc:115
void DhtFindProvs(const std::string &hash, Json *providers)
Retrieve the providers for a content that is addressed by a hash.
Definition: client.cc:154
void BlockGet(const std::string &block_id, std::iostream *block)
Get a raw IPFS block.
Definition: client.cc:185
void SwarmConnect(const std::string &peer)
Open a connection to a given address.
Definition: client.cc:439
Recursively unpin the objects.
void ObjectData(const std::string &object_id, std::string *data)
Get the data field of a MerkleDAG node.
Definition: client.cc:329
void SwarmAddrs(Json *addresses)
List of known addresses of each peer connected.
Definition: client.cc:435
void PinRm(const std::string &object_id, PinRmOptions options)
Unpin an object.
Definition: client.cc:424
void NameResolve(const std::string &name_id, std::string *path_string)
Resolve an IPNS name.
Definition: client.cc:302
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:264
void FilesAdd(const std::vector< http::FileUpload > &files, Json *result)
Add files to IPFS.
Definition: client.cc:201
Client & operator=(const Client &)
Copy assignment operator.
Definition: client.cc:47
Client(const std::string &host, long port)
Constructor.
Definition: client.cc:34
void FilesLs(const std::string &path, Json *result)
List directory contents for Unix filesystem objects.
Definition: client.cc:260
IPFS client.
Definition: client.h:46
void Id(Json *id)
Return the identity of the peer.
Definition: client.cc:77
void KeyList(Json *key_list)
List all the keys.
Definition: client.cc:275
IPFS namespace.
Definition: client.h:32
Just unpin the specified object.
void ObjectLinks(const std::string &object_id, Json *links)
Get links of a MerkleDAG node.
Definition: client.cc:337
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:286
void ConfigSet(const std::string &key, const Json &value)
Add or replace a config knob at the peer.
Definition: client.cc:109
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:386
HTTP file upload.
Definition: transport.h:32
void Version(Json *version)
Return the implementation version of the peer.
Definition: client.cc:79
void KeyRm(const std::string &key_name)
Remove a key.
Definition: client.cc:281
PinRmOptions
Options to control the PinRm() method.
Definition: client.h:668
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:349