cpp-ipfs-http-client
IPFS C++ client library
Loading...
Searching...
No Matches
client.h
1/* Copyright (c) 2016-2023, The C++ IPFS client library developers
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of
4this software and associated documentation files (the "Software"), to deal in
5the Software without restriction, including without limitation the rights to
6use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7the Software, and to permit persons to whom the Software is furnished to do so,
8subject to the following conditions:
9
10The above copyright notice and this permission notice shall be included in all
11copies or substantial portions of the Software.
12
13THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18CONNECTION 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 <memory>
27#include <nlohmann/json.hpp>
28#include <string>
29#include <utility>
30#include <vector>
31
33namespace ipfs {
34
37using Json = nlohmann::json;
38
48class Client {
49 public:
56 Client(
58 const std::string& host,
60 long port,
63 const std::string& timeout = "",
65 const std::string& protocol = "http://",
67 const std::string& apiPath = "/api/v0",
69 bool verbose = false);
70
72 Client(
74 const Client&);
75
77 Client(
79 Client&&) noexcept;
80
85 const Client&);
86
91 Client&&) noexcept;
92
96
108 void Id(
111 Json* id);
112
124 void Version(
127 Json* version);
128
140 void ConfigGet(
143 const std::string& key,
145 Json* config);
146
158 void ConfigSet(
160 const std::string& key,
162 const Json& value);
163
175 void ConfigReplace(
177 const Json& config);
178
190 void DhtFindPeer(
192 const std::string& peer_id,
194 Json* addresses);
195
207 void DhtFindProvs(
209 const std::string& hash,
211 Json* providers);
212
224 void BlockGet(
226 const std::string& block_id,
229 std::iostream* block);
230
242 void BlockPut(
244 const http::FileUpload& block,
246 Json* stat);
247
259 void BlockStat(
261 const std::string& block_id,
263 Json* stat);
264
276 void FilesGet(
279 const std::string& path,
282 std::iostream* response);
283
295 void FilesAdd(
297 const std::vector<http::FileUpload>& files,
301 Json* result);
302
314 void FilesLs(
316 const std::string& path,
344 Json* result);
345
357 void KeyGen(
359 const std::string& key_name,
361 const std::string& key_type,
363 size_t key_size,
365 std::string* key_id);
366
378 void KeyList(
380 Json* key_list);
381
393 void KeyRm(
395 const std::string& key_name);
396
408 void KeyRename(
410 const std::string& old_key,
412 const std::string& new_key);
413
425 void NamePublish(
427 const std::string& object_id,
430 const std::string& key_name,
442 const Json& options,
444 std::string* name_id);
445
457 void NameResolve(
459 const std::string& name_id,
462 std::string* path_string);
463
475 void ObjectNew(
477 std::string* object_id);
478
490 void ObjectPut(
492 const Json& object,
495 Json* object_stored);
496
508 void ObjectGet(
510 const std::string& object_id,
514 Json* object);
515
527 void ObjectData(
529 const std::string& object_id,
531 std::string* data);
532
544 void ObjectLinks(
546 const std::string& object_id,
549 Json* links);
550
562 void ObjectStat(
564 const std::string& object_id,
567 Json* stat);
568
582 const std::string& source,
584 const std::string& link_name,
586 const std::string& link_target,
588 std::string* cloned);
589
604 const std::string& source,
606 const std::string& link_name,
608 std::string* cloned);
609
623 const std::string& source,
625 const http::FileUpload& data,
627 std::string* cloned);
628
642 const std::string& source,
644 const http::FileUpload& data,
646 std::string* cloned);
647
659 void PinAdd(
661 const std::string& object_id);
662
674 void PinLs(
676 Json* pinned);
677
689 void PinLs(
691 const std::string& object_id,
693 Json* pinned);
694
696 enum class PinRmOptions {
700 RECURSIVE,
701 };
702
716 void PinRm(
718 const std::string& object_id,
720 PinRmOptions options);
721
733 void StatsBw(
742 Json* bandwidth_info);
743
755 void StatsRepo(
765 Json* repo_stats);
766
778 void SwarmAddrs(
780 Json* addresses);
781
793 void SwarmConnect(
797 const std::string& peer);
798
810 void SwarmDisconnect(
814 const std::string& peer);
815
827 void SwarmPeers(
829 Json* peers);
830
844 void Abort();
845
859 void Reset();
860
861 private:
863 void FetchAndParseJson(
866 const std::string& url,
868 Json* response);
869
872 void FetchAndParseJson(
874 const std::string& url,
876 const std::vector<http::FileUpload>& files,
878 Json* response);
879
884 static void ParseJson(
886 const std::string& input,
888 Json* result);
889
894 template <class PropertyType>
895 static void GetProperty(
897 const Json& input,
899 const std::string& property_name,
901 size_t line_number,
903 PropertyType* property_value);
904
911 std::string MakeUrl(
913 const std::string& path,
915 const std::vector<std::pair<std::string, std::string>>& parameters = {});
916
919 std::string url_prefix_;
920
922 std::unique_ptr<http::Transport> http_;
923
925 std::string timeout_value_;
926};
927} /* namespace ipfs */
928
929#endif /* IPFS_CLIENT_H */
IPFS client.
Definition client.h:48
void BlockStat(const std::string &block_id, Json *stat)
Get information for a raw IPFS block.
Definition client.cc:202
void FilesLs(const std::string &path, Json *result)
List directory contents for Unix filesystem objects.
Definition client.cc:269
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:378
void BlockPut(const http::FileUpload &block, Json *stat)
Store a raw block in IPFS.
Definition client.cc:198
void PinAdd(const std::string &object_id)
Pin a given IPFS object.
Definition client.cc:412
void PinRm(const std::string &object_id, PinRmOptions options)
Unpin an object.
Definition client.cc:439
void SwarmConnect(const std::string &peer)
Open a connection to a given address.
Definition client.cc:462
void ObjectStat(const std::string &object_id, Json *stat)
Get stats about a MerkleDAG node.
Definition client.cc:360
void SwarmPeers(Json *peers)
List the peers that we have connections with.
Definition client.cc:472
void KeyList(Json *key_list)
List all the keys.
Definition client.cc:284
void BlockGet(const std::string &block_id, std::iostream *block)
Get a raw IPFS block.
Definition client.cc:194
void ConfigGet(const std::string &key, Json *config)
Query the current config of the peer.
Definition client.cc:92
void KeyRename(const std::string &old_key, const std::string &new_key)
Rename an existing key.
Definition client.cc:295
void PinLs(Json *pinned)
List all the objects pinned to local storage.
Definition client.cc:431
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:364
void Reset()
Resets the abort call, allowing to execute new API requests again.
Definition client.cc:483
void NameResolve(const std::string &name_id, std::string *path_string)
Resolve an IPNS name.
Definition client.cc:317
~Client()
Destructor.
void ObjectNew(std::string *object_id)
Create a new MerkleDAG node.
Definition client.cc:325
void FilesGet(const std::string &path, std::iostream *response)
Get a file from IPFS.
Definition client.cc:206
void ObjectPut(const Json &object, Json *object_stored)
Store a MerkleDAG node.
Definition client.cc:333
void KeyRm(const std::string &key_name)
Remove a key.
Definition client.cc:290
void ObjectLinks(const std::string &object_id, Json *links)
Get links of a MerkleDAG node.
Definition client.cc:352
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:301
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:401
void DhtFindProvs(const std::string &hash, Json *providers)
Retrieve the providers for a content that is addressed by a hash.
Definition client.cc:163
void SwarmAddrs(Json *addresses)
List of known addresses of each peer connected.
Definition client.cc:458
void ConfigReplace(const Json &config)
Replace the entire config at the peer.
Definition client.cc:124
void ObjectData(const std::string &object_id, std::string *data)
Get the data field of a MerkleDAG node.
Definition client.cc:344
void Abort()
Abort any current running IPFS API request.
Definition client.cc:476
Client & operator=(const Client &)
Copy assignment operator.
Definition client.cc:55
void ConfigSet(const std::string &key, const Json &value)
Add or replace a config knob at the peer.
Definition client.cc:118
void ObjectGet(const std::string &object_id, Json *object)
Get a MerkleDAG node.
Definition client.cc:340
void FilesAdd(const std::vector< http::FileUpload > &files, Json *result)
Add files to IPFS.
Definition client.cc:210
void StatsBw(Json *bandwidth_info)
Get IPFS bandwidth (bw) information.
Definition client.cc:450
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:273
void Version(Json *version)
Return the implementation version of the peer.
Definition client.cc:88
PinRmOptions
Options to control the PinRm() method.
Definition client.h:696
@ RECURSIVE
Recursively unpin the objects.
@ NON_RECURSIVE
Just unpin the specified object.
void Id(Json *id)
Return the identity of the peer.
Definition client.cc:86
void StatsRepo(Json *repo_stats)
Get IPFS Repo Stats.
Definition client.cc:454
void DhtFindPeer(const std::string &peer_id, Json *addresses)
Retrieve the peer info of a reachable node in the network.
Definition client.cc:132
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:390
void SwarmDisconnect(const std::string &peer)
Close a connection on a given address.
Definition client.cc:467
IPFS namespace.
Definition client.h:33
nlohmann::json Json
Type of the output of some methods, aliased for convenience.
Definition client.h:37
HTTP file upload.
Definition transport.h:33