|
| 1 | +# py-ipfs-api |
| 2 | + |
| 3 | +[](http://ipn.io) |
| 4 | +[](http://ipfs.io/) |
| 5 | +[](http://webchat.freenode.net/?channels=%23ipfs) |
| 6 | +[](https://github.com/RichardLitt/standard-readme) |
| 7 | + |
| 8 | +[](https://travis-ci.org/ipfs/py-ipfs-api) |
| 9 | + |
| 10 | +> A python client library for the IPFS API |
| 11 | +
|
| 12 | +Check out [ipfs](http://ipfs.io/) and [the API command reference](http://ipfs.io/docs/commands/) for more information about the IPFS API. |
| 13 | + |
| 14 | +## Table of Contents |
| 15 | + |
| 16 | +- [Install](#install) |
| 17 | +- [Usage](#usage) |
| 18 | +- [Contribute](#contribute) |
| 19 | + - [IRC](#irc) |
| 20 | + - [Bug reports](#bug-reports) |
| 21 | + - [Pull requests](#pull-requests) |
| 22 | +- [License](#license) |
| 23 | + |
| 24 | +## Install |
| 25 | + |
| 26 | +Install with pip: |
| 27 | + |
| 28 | +```sh |
| 29 | +pip install ipfs-api |
| 30 | +``` |
| 31 | + |
| 32 | +## Usage |
| 33 | + |
| 34 | +Basic use-case (requires a running instance of IPFS daemon): |
| 35 | + |
| 36 | +```py |
| 37 | +>>> import ipfsApi |
| 38 | +>>> api = ipfsApi.Client('127.0.0.1', 5001) |
| 39 | +>>> res = api.add('test.txt') |
| 40 | +>>> res |
| 41 | +{'Hash': 'QmWxS5aNTFEc9XbMX1ASvLET1zrqEaTssqt33rVZQCQb22', 'Name': 'test.txt'} |
| 42 | +>>> api.cat(res['Hash']) |
| 43 | +'fdsafkljdskafjaksdjf\n' |
| 44 | +``` |
| 45 | + |
| 46 | +Administrative functions: |
| 47 | + |
| 48 | +```py |
| 49 | +>>> api.id() |
| 50 | +{'Addresses': ['/ip4/127.0.0.1/tcp/4001/ipfs/QmS2C4MjZsv2iP1UDMMLCYqJ4WeJw8n3vXx1VKxW1UbqHS', |
| 51 | + '/ip6/::1/tcp/4001/ipfs/QmS2C4MjZsv2iP1UDMMLCYqJ4WeJw8n3vXx1VKxW1UbqHS'], |
| 52 | + 'AgentVersion': 'go-ipfs/0.3.8-dev', |
| 53 | + 'ID': 'QmS2C4MjZsv2iP1UDMMLCYqJ4WeJw8n3vXx1VKxW1UbqHS', |
| 54 | + 'ProtocolVersion': 'ipfs/0.1.0', |
| 55 | + 'PublicKey': 'CAASpgIwgg ... 3FcjAgMBAAE='} |
| 56 | +``` |
| 57 | + |
| 58 | +Pass in API options: |
| 59 | + |
| 60 | +```py |
| 61 | +>>> api.pin_ls(opts={'type':'all'}) |
| 62 | +{'Keys': {'QmNMELyizsfFdNZW3yKTi1SE2pErifwDTXx6vvQBfwcJbU': {'Count': 1, |
| 63 | + 'Type': 'indirect'}, |
| 64 | + 'QmNQ1h6o1xJARvYzwmySPsuv9L5XfzS4WTvJSTAWwYRSd8': {'Count': 1, |
| 65 | + 'Type': 'indirect'}, |
| 66 | + ... |
| 67 | +``` |
| 68 | + |
| 69 | +Add a directory and match against a filename pattern: |
| 70 | + |
| 71 | +```py |
| 72 | +>>> api.add('photos', match='*.jpg') |
| 73 | +[{'Hash': 'QmcqBstfu5AWpXUqbucwimmWdJbu89qqYmE3WXVktvaXhX', |
| 74 | + 'Name': 'photos/photo1.jpg'}, |
| 75 | + {'Hash': 'QmSbmgg7kYwkSNzGLvWELnw1KthvTAMszN5TNg3XQ799Fu', |
| 76 | + 'Name': 'photos/photo2.jpg'}, |
| 77 | + {'Hash': 'Qma6K85PJ8dN3qWjxgsDNaMjWjTNy8ygUWXH2kfoq9bVxH', |
| 78 | + 'Name': 'photos/photo3.jpg'}] |
| 79 | +``` |
| 80 | + |
| 81 | +Or add a directory recursively: |
| 82 | + |
| 83 | +```py |
| 84 | +>>> api.add('fake_dir', recursive=True) |
| 85 | +[{'Hash': 'QmQcCtMgLVwvMQGu6mvsRYLjwqrZJcYtH4mboM9urWW9vX', |
| 86 | + 'Name': 'fake_dir/fsdfgh'}, |
| 87 | + {'Hash': 'QmNuvmuFeeWWpxjCQwLkHshr8iqhGLWXFzSGzafBeawTTZ', |
| 88 | + 'Name': 'fake_dir/test2/llllg'}, |
| 89 | + {'Hash': 'QmX1dd5DtkgoiYRKaPQPTCtXArUu4jEZ62rJBUcd5WhxAZ', |
| 90 | + 'Name': 'fake_dir/test2'}, |
| 91 | + {'Hash': 'Qmenzb5J4fR9c69BbpbBhPTSp2Snjthu2hKPWGPPJUHb9M', |
| 92 | + 'Name': 'fake_dir'}] |
| 93 | +``` |
| 94 | + |
| 95 | +This module also contains some helper functions for adding strings, json, and even python objects to IPFS: |
| 96 | + |
| 97 | +```py |
| 98 | +>>> lst = [1, 77, 'lol'] |
| 99 | +>>> api.add_pyobj(lst) |
| 100 | +'QmRFqz1ABQtbMBDfjpMubTaginvpVnf58Y87gheRzGfe4i' |
| 101 | +>>> api.get_pyobj(_) |
| 102 | +[1, 77, 'lol'] |
| 103 | +``` |
| 104 | + |
| 105 | +## Contribute |
| 106 | + |
| 107 | +### IRC |
| 108 | + |
| 109 | +Join us on IRC at `#ipfs` on [chat.freenode.net](https://webchat.freenode.net) if you have any suggestions or questions, |
| 110 | +or if you just want to discuss IPFS and python. |
| 111 | + |
| 112 | +### Bug reports |
| 113 | + |
| 114 | +You can submit bug reports using the [GitHub issue tracker](https://github.com/ipfs/python-ipfs-api/issues). |
| 115 | + |
| 116 | +### Pull requests |
| 117 | + |
| 118 | +Pull requests are welcome. Before submitting a new pull request, please |
| 119 | +make sure that your code passes both the [pep8](https://www.python.org/dev/peps/pep-0008/) formatting check: |
| 120 | + |
| 121 | + $ tox -e pep8 |
| 122 | + |
| 123 | +And the unit tests: |
| 124 | + |
| 125 | + $ tox |
| 126 | + |
| 127 | +You can arrange to run the pep8 tests automatically before each commit by |
| 128 | +installing a `pre-commit` hook: |
| 129 | + |
| 130 | + $ ./tools/pre-commit --install |
| 131 | + |
| 132 | +Please make sure to include new unit tests for new features or changes in |
| 133 | +behavior. |
| 134 | + |
| 135 | +## License |
| 136 | + |
| 137 | +This code is distributed under the terms of the [MIT license](https://opensource.org/licenses/MIT). Details can be found in the file |
| 138 | +[LICENSE](LICENSE) in this repository. |
0 commit comments