Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/simonw/github-contents
Python class for reading and writing data to a GitHub repository
https://github.com/simonw/github-contents
github githubapi
Last synced: 26 days ago
JSON representation
Python class for reading and writing data to a GitHub repository
- Host: GitHub
- URL: https://github.com/simonw/github-contents
- Owner: simonw
- Created: 2019-06-09T19:10:59.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-02-24T18:34:49.000Z (over 3 years ago)
- Last Synced: 2024-05-01T23:17:31.999Z (6 months ago)
- Topics: github, githubapi
- Language: Python
- Homepage:
- Size: 26.4 KB
- Stars: 20
- Watchers: 2
- Forks: 3
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# github-contents
[![PyPI](https://img.shields.io/pypi/v/github-contents.svg)](https://pypi.org/project/github-contents/)
[![Changelog](https://img.shields.io/github/v/release/simonw/github-contents?include_prereleases&label=changelog)](https://github.com/simonw/github-contents/releases)
[![Tests](https://github.com/simonw/github-contents/workflows/Test/badge.svg)](https://github.com/simonw/github-contents/actions?query=workflow%3ATest)
[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/simonw/github-contents/blob/main/LICENSE)Read and write both small and large files to Github.
The regular [GitHub Contents API](https://developer.github.com/v3/repos/contents/) can't handle files larger than 1MB - this class knows how to spot that problem and switch to the large-file-supporting low level [Git Data API](https://developer.github.com/v3/git/) instead.
Note that file contents is passed and returned as bytestrings, not regular strings.
## Installation
pip install github-contents
## Usage
You will need a GitHub OAuth token with full repository access.
The easiest way to create one of these is using [https://github.com/settings/tokens](https://github.com/settings/tokens)
```python
from github_contents import GithubContents# For repo simonw/disaster-data:
github = GithubContents(
"simonw",
"disaster-data",
token=GITHUB_OAUTH_TOKEN,
branch="main"
)
```
To read a file:
```python
content_in_bytes, sha = github.read(path_within_repo)
```
To write a file:
```python
content_sha, commit_sha = github.write(
filepath=path_within_repo,
content_bytes=contents_in_bytes,
sha=previous_sha, # Optional
commit_message=commit_message,
committer={
"name": COMMITTER_NAME,
"email": COMMITTER_EMAIL,
},
)
```