├── pythoncosmicjs ├── __init__.py ├── .gitattributes ├── .gitignore └── api.py ├── MANIFEST ├── setup.py ├── .gitattributes ├── README.md └── .gitignore /pythoncosmicjs/__init__.py: -------------------------------------------------------------------------------- 1 | from .api import Api -------------------------------------------------------------------------------- /MANIFEST: -------------------------------------------------------------------------------- 1 | # file GENERATED by distutils, do NOT edit 2 | setup.py 3 | pythoncosmicjs\__init__.py 4 | pythoncosmicjs\api.py 5 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup 2 | 3 | 4 | setup(name='pythoncosmicjs', 5 | version='1.0', 6 | description='Python Cosmicjs API', 7 | author='Pavel Uskavan', 8 | author_email='uskavan@gmail.com', 9 | url='https://github.com/uskavan/pythoncosmicjs', 10 | packages=['pythoncosmicjs'], 11 | package_dir={'pythoncosmicjs': 'pythoncosmicjs'}, 12 | requires=['requests'] 13 | ) 14 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /pythoncosmicjs/.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Issue Count](https://codeclimate.com/github/cosmicjs/cosmicjs-python/badges/issue_count.svg)](https://codeclimate.com/github/cosmicjs/cosmicjs-python) 2 | 3 | ## Install 4 | 1. `git clone https://github.com/uskavan/cosmicjs-python.git` 5 | 2. `cd cosmicjs-python` 6 | 3. `python setup.py install` 7 | 8 | ### Usage 9 | ```python 10 | from pythoncosmicjs import Api 11 | # Configure 12 | api = Api(bucket='bucket-slug', read_key='read_key', write_key='write_key') 13 | # Get all contents of bucket including objects and media 14 | print(api.get_bucket()) 15 | # Get all objects 16 | print(api.objects(limit=10, skip=5)) # limit, skip the default is None 17 | # Get objects by type 18 | print(api.object_type(type_slug='pages', limit=10, skip=5)) # limit, skip the default is None 19 | # Get object 20 | print(api.object(object_slug='object-slug')) # object_slug mandatory variable 21 | # Get media 22 | print(api.media(limit=10, skip=5)) # limit, skip the default is None 23 | # Add object 24 | print(api.add_object(title='object title', content='object content')) # title, content required variables 25 | # Edit object 26 | print(api.edit_object(object_slug='object-slug', title='change to the title', content='change to the content')) # title, content required variables 27 | # Delete object 28 | print(api.delete_object(object_slug='object-slug')) # the name of the object you want to delete 29 | # Search object 30 | print(api.search_object(object_type='', limit=1, skip=10, metafield_keys='bob', metafield_value='bob')) 31 | ``` 32 | ## Link 33 | 34 | [Documentation](https://github.com/uskavan/pythoncosmicjs/wiki) 35 | 36 | [Official website Сosmicjs](https://cosmicjs.com/) 37 | 38 | [Referral Link](https://cosmicjs.com/?ref=S1G_ALN9x) 39 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | *.idea 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | local_settings.py 55 | 56 | # Flask instance folder 57 | instance/ 58 | 59 | # Scrapy stuff: 60 | .scrapy 61 | 62 | # Sphinx documentation 63 | docs/_build/ 64 | 65 | # PyBuilder 66 | target/ 67 | 68 | # IPython Notebook 69 | .ipynb_checkpoints 70 | 71 | # pyenv 72 | .python-version 73 | 74 | # celery beat schedule file 75 | celerybeat-schedule 76 | 77 | # dotenv 78 | .env 79 | 80 | # virtualenv 81 | venv/ 82 | ENV/ 83 | 84 | # Spyder project settings 85 | .spyderproject 86 | 87 | # Rope project settings 88 | .ropeproject 89 | 90 | # ========================= 91 | # Operating System Files 92 | # ========================= 93 | 94 | # OSX 95 | # ========================= 96 | 97 | .DS_Store 98 | .AppleDouble 99 | .LSOverride 100 | 101 | # Thumbnails 102 | ._* 103 | 104 | # Files that might appear in the root of a volume 105 | .DocumentRevisions-V100 106 | .fseventsd 107 | .Spotlight-V100 108 | .TemporaryItems 109 | .Trashes 110 | .VolumeIcon.icns 111 | 112 | # Directories potentially created on remote AFP share 113 | .AppleDB 114 | .AppleDesktop 115 | Network Trash Folder 116 | Temporary Items 117 | .apdisk 118 | 119 | # Windows 120 | # ========================= 121 | 122 | # Windows image file caches 123 | Thumbs.db 124 | ehthumbs.db 125 | 126 | # Folder config file 127 | Desktop.ini 128 | 129 | # Recycle Bin used on file shares 130 | $RECYCLE.BIN/ 131 | 132 | # Windows Installer files 133 | *.cab 134 | *.msi 135 | *.msm 136 | *.msp 137 | 138 | # Windows shortcuts 139 | *.lnk 140 | -------------------------------------------------------------------------------- /pythoncosmicjs/.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | local_settings.py 55 | 56 | # Flask instance folder 57 | instance/ 58 | 59 | # Scrapy stuff: 60 | .scrapy 61 | 62 | # Sphinx documentation 63 | docs/_build/ 64 | 65 | # PyBuilder 66 | target/ 67 | 68 | # IPython Notebook 69 | .ipynb_checkpoints 70 | 71 | # pyenv 72 | .python-version 73 | 74 | # celery beat schedule file 75 | celerybeat-schedule 76 | 77 | # dotenv 78 | .env 79 | 80 | # virtualenv 81 | venv/ 82 | ENV/ 83 | 84 | # Spyder project settings 85 | .spyderproject 86 | 87 | # Rope project settings 88 | .ropeproject 89 | 90 | # ========================= 91 | # Operating System Files 92 | # ========================= 93 | 94 | # OSX 95 | # ========================= 96 | 97 | .DS_Store 98 | .AppleDouble 99 | .LSOverride 100 | 101 | # Thumbnails 102 | ._* 103 | 104 | # Files that might appear in the root of a volume 105 | .DocumentRevisions-V100 106 | .fseventsd 107 | .Spotlight-V100 108 | .TemporaryItems 109 | .Trashes 110 | .VolumeIcon.icns 111 | 112 | # Directories potentially created on remote AFP share 113 | .AppleDB 114 | .AppleDesktop 115 | Network Trash Folder 116 | Temporary Items 117 | .apdisk 118 | 119 | # Windows 120 | # ========================= 121 | 122 | # Windows image file caches 123 | Thumbs.db 124 | ehthumbs.db 125 | 126 | # Folder config file 127 | Desktop.ini 128 | 129 | # Recycle Bin used on file shares 130 | $RECYCLE.BIN/ 131 | 132 | # Windows Installer files 133 | *.cab 134 | *.msi 135 | *.msm 136 | *.msp 137 | 138 | # Windows shortcuts 139 | *.lnk 140 | -------------------------------------------------------------------------------- /pythoncosmicjs/api.py: -------------------------------------------------------------------------------- 1 | import requests 2 | 3 | class Api(object): 4 | def __init__(self, bucket, read_key=None, write_key=None, base_url=None): 5 | self.bucket = bucket 6 | self.base_url = 'https://api.cosmicjs.com/v1' or base_url 7 | self.read_key = '?read_key=%s' % read_key if read_key else '' 8 | self.write_key = '?write_key=%s' % write_key if write_key else '' 9 | 10 | def get_bucket(self): 11 | url = '%s/%s%s' % (self.base_url, self.bucket, self.read_key) 12 | return self.deserialization(url) 13 | 14 | def objects(self, limit=None, skip=None): 15 | payload = {'limit': limit, 'skip': skip} 16 | url = '%s/%s/objects%s' % (self.base_url, self.bucket, self.read_key) 17 | return self.deserialization(url, payload) 18 | 19 | def object_type(self, type_slug, limit=None, skip=None): 20 | payload = {'limit': limit, 'skip': skip} 21 | url = '%s/%s/object-type/%s/%s' % (self.base_url, self.bucket, type_slug, self.read_key) 22 | return self.deserialization(url, payload) 23 | 24 | def object(self, object_slug): 25 | url = '%s/%s/object/%s/%s' % (self.base_url, self.bucket, object_slug, self.read_key) 26 | return self.deserialization(url) 27 | 28 | def media(self, limit=None, skip=None): 29 | if limit and skip: 30 | query_parameters = '?limit=%s&?skip=%s' % (limit, skip) 31 | else: 32 | query_parameters = '%s%s' % ('?limit=' + str(limit) if limit else '', '?skip=' + str(skip) if skip else '') 33 | url = '%s/%s/media%s%s' % (self.base_url, self.bucket, self.read_key, query_parameters) 34 | return self.deserialization(url) 35 | 36 | def search_object(self, object_type=None, limit=None, skip=None, metafield_keys=None, metafield_value=None): 37 | payload = {'limit': limit, 'skip': skip, 'metafield_key': metafield_keys, 'metafield_value': metafield_value} 38 | url = '%s/%s/object-type/%s/search' % (self.base_url, self.bucket, object_type) 39 | return self.deserialization(url, payload) 40 | 41 | def add_object(self, title, content): 42 | url = '%s/%s/add-object%s' % (self.base_url, self.bucket, self.write_key) 43 | r = requests.post(url, data={'title': title, 'type_slug': title.replace(' ', '-'), 'content': content, 44 | 'write_key': self.write_key}) 45 | return r.json() 46 | 47 | def edit_object(self, slug, title, content): 48 | url = '%s/%s/edit-object' % (self.base_url, self.bucket) 49 | r = requests.put(url, data={'slug': slug, 'title': title, 'content': content}) 50 | return r.json() 51 | 52 | def delete_object(self, object_slug): 53 | url = '%s/%s/%s' % (self.base_url, self.bucket, object_slug) 54 | r = requests.delete(url, data={'write_key': self.write_key}) 55 | return r.json() 56 | 57 | @staticmethod 58 | def deserialization(url, params=None): 59 | r = requests.get(url, params) 60 | if r.status_code == 200: 61 | return r.json() 62 | --------------------------------------------------------------------------------