REST APIs disguised as Django ORM Models

mbylstra, updated 🕥 2022-01-21 19:30:18

django-wham

REST APIs disguised as Django ORM Models

With django-wham you can query REST APIs in exactly the same way you query Django Models:

```

artist = SpotifyArtist.objects.get(id='5Z1XZyEFY0dewG8faEIiEx') fetching https://api.spotify.com/v1/artists/5Z1XZyEFY0dewG8faEIiEx print artist.name Django Reinhardt ```

```

twitter_user = TwitterUser.objects.get(screen_name='djangoproject') fetching https://api.twitter.com/1.1/users/show.json?screen_name=djangoproject for tweet in twitter_user.tweets.all(): print tweet.text fetching https://api.twitter.com/1.1/statuses/user_timeline.json?user_id=191225303 Django 1.7 release candidate 2 - It's almost here! Today we're pleased to announce the second release-candidate pa... Django 1.7 release candidate 1 - It's almost here! Tonight we're pleased to announce the first release-candidate p... ... ```

Creating Django Wham Models

In order to query a REST API (eg: Twitter, Flickr, Youtube, etc), you need a Wham Model. A Wham Model corresponds to an API Endpoint and has a special inner WhamMeta class that describes how the REST API should map to the Django Model.

This simple Wham Model that enables you too look up a Spotify Artist by its Spotify ID and retrieve some information about that artist. In this case the names of the fields match the property names of the retrieved JSON data, so very little configuration is required.

``` from wham.models import WhamModel

class SpotifyArtist(WhamModel):

id = CharField(max_length=100, primary_key=True) 
    # the primary key field should be of the same name and type as the API Endpoint ID
    # rather than the default auto-incrementing key Django provides. For example,
    # '5Z1XZyEFY0dewG8faEIiEx' is the Spotify ID for Django Reinhardt.
name = TextField()
href = TextField()
popularity = IntegerField()
uri = UriField()

class WhamMeta(SpotifyMeta):
    base_url = 'https://api.spotify.com/v1/'
    endpoint = 'artists'

```

Now we add another Spotify Endpoint, this time both of the Models' WhamMeta class inherit from a single class, to share commonalities between endpoints such as the base url ``` class SpotifyWhamMeta: base_url = 'https://api.spotify.com/v1/'

class SpotifyArtist(WhamModel): ...

class WhamMeta(SpotifyWhamMeta):
    endpoint = 'artists'

class SpotifyAlbum(WhamModel):

id = WhamCharField(max_length=255, primary_key=True)
name = WhamTextField()
release_date = WhamTextField(null=True)
popularity = WhamIntegerField(null=True, wham_detailed=True)

class WhamMeta(SpotifyWhamMeta):
    endpoint = 'albums'

```

Many To Many Endpoints

Django Wham supports many to many endpoints such as https://api.spotify.com/v1/artists/{id}/albums. This is possible by specifying a WhamManyToMany field and doing a little configuration.

To create a many to many field between Artists and Albums:

``` class SpotifyArtist(WhamModel): ...

albums = WhamManyToManyField(
    'SpotifyAlbum',
    wham_endpoint='artists/{{id}}/albums', #django template syntax is used to substitue the value of thd id
    wham_results_path=['items'] 
        # This is the 'path' where to find the list of results in the retreived JSON data.
        # The list or tuple represents a path in the JSON tree. A hypothetical example might be ['photos', 'results']
)

...

```

Wham Model Repository

Have a look at https://github.com/mbylstra/django-wham/tree/master/wham/apis for some more examples of Wham Models. We're hoping this will become an ever growing repository of Wham Models for the multitude of public REST APIs on the web, so you won't have to write your own. If you have ever wanted to contribute to open source, but have been too daunted, creating a Wham Model is a fun and easy way to get started. Only basic Django experience is required.

Installation and use of Wham Models

django-wham can be installed via PyPI: ``` pip install django-wham

```

Add some entries to your INSTALLED_APPS in your settings.py file. See https://github.com/mbylstra/django-wham/tree/master/wham/apis for a list of example apis. ``` INSTALLED_APPS = ( ... 'wham', #required 'wham.apis.spotify', #an optional provided API 'wham.apis.twitter', #optional 'your_custom_wham_app' #this should be a Django app with one or more Wham Models in its models.py file ... )

You must then run syncdb before you can start using any wham models python manage.py syncdb

Django wham models can be used just like regular Django Models from wham.apis.spotify import SpotifyArtist SpotifyArtist.objects.filter(name__icontains="green") ```

demo

This git repository also comes with a demo django project and some Ipython notebook examples that can be run with django manage.py shell_plus --notebook. Your environment must have the packages in ipython-requirements.txt installed for this to work.

More Info

Disclaimer

Django Wham is currently in an Alpha/Proof-of-Concept stage. While I am using it to great success in my project http://kaleidoscope.fm, there are probably many edge cases that it does not handle very well. I would not recommend using this in production just yet. If you are interested in using this, please contact me and I won't hestitate to help out. Any kind of feedback would also be highly appreciated (bug reports, suggestions for improvements, requests for more APIs to be supported) - please submit a Git issue!

Issues

Bump ipython from 2.1.0 to 7.16.3

opened on 2022-01-21 19:30:18 by dependabot[bot]

Bumps ipython from 2.1.0 to 7.16.3.

Release notes

Sourced from ipython's releases.

7.9.0

No release notes provided.

7.8.0

No release notes provided.

7.7.0

No release notes provided.

7.6.1

No release notes provided.

7.6.0

No release notes provided.

7.5.0

No release notes provided.

7.4.0

No release notes provided.

7.3.0

No release notes provided.

7.2.0

No release notes provided.

7.1.1

No release notes provided.

7.1.0

No release notes provided.

7.0.1

No release notes provided.

7.0.0

No release notes provided.

7.0.0-doc

No release notes provided.

7.0.0rc1

No release notes provided.

7.0.0b1

No release notes provided.

6.2.1

No release notes provided.

... (truncated)

Commits


Dependabot compatibility score

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


Dependabot commands and options
You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) - `@dependabot use these labels` will set the current labels as the default for future PRs for this repo and language - `@dependabot use these reviewers` will set the current reviewers as the default for future PRs for this repo and language - `@dependabot use these assignees` will set the current assignees as the default for future PRs for this repo and language - `@dependabot use this milestone` will set the current milestone as the default for future PRs for this repo and language You can disable automated security fix PRs for this repo from the [Security Alerts page](https://github.com/mbylstra/django-wham/network/alerts).

Bump django from 1.6.5 to 2.2.24

opened on 2021-06-10 19:40:39 by dependabot[bot]

Bumps django from 1.6.5 to 2.2.24.

Commits
  • 2da029d [2.2.x] Bumped version for 2.2.24 release.
  • f27c38a [2.2.x] Fixed CVE-2021-33571 -- Prevented leading zeros in IPv4 addresses.
  • 053cc95 [2.2.x] Fixed CVE-2021-33203 -- Fixed potential path-traversal via admindocs'...
  • 6229d87 [2.2.x] Confirmed release date for Django 2.2.24.
  • f163ad5 [2.2.x] Added stub release notes and date for Django 2.2.24.
  • bed1755 [2.2.x] Changed IRC references to Libera.Chat.
  • 63f0d7a [2.2.x] Refs #32718 -- Fixed file_storage.test_generate_filename and model_fi...
  • 5fe4970 [2.2.x] Post-release version bump.
  • 61f814f [2.2.x] Bumped version for 2.2.23 release.
  • b8ecb06 [2.2.x] Fixed #32718 -- Relaxed file name validation in FileField.
  • Additional commits viewable in compare view


Dependabot compatibility score

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


Dependabot commands and options
You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) - `@dependabot use these labels` will set the current labels as the default for future PRs for this repo and language - `@dependabot use these reviewers` will set the current reviewers as the default for future PRs for this repo and language - `@dependabot use these assignees` will set the current assignees as the default for future PRs for this repo and language - `@dependabot use this milestone` will set the current milestone as the default for future PRs for this repo and language You can disable automated security fix PRs for this repo from the [Security Alerts page](https://github.com/mbylstra/django-wham/network/alerts).

Bump jinja2 from 2.7.3 to 2.11.3

opened on 2021-03-19 22:37:51 by dependabot[bot]

Bumps jinja2 from 2.7.3 to 2.11.3.

Release notes

Sourced from jinja2's releases.

2.11.3

This contains a fix for a speed issue with the urlize filter. urlize is likely to be called on untrusted user input. For certain inputs some of the regular expressions used to parse the text could take a very long time due to backtracking. As part of the fix, the email matching became slightly stricter. The various speedups apply to urlize in general, not just the specific input cases.

2.11.2

2.11.1

This fixes an issue in async environment when indexing the result of an attribute lookup, like {{ data.items[1:] }}.

2.11.0

This is the last version to support Python 2.7 and 3.5. The next version will be Jinja 3.0 and will support Python 3.6 and newer.

2.10.3

2.10.2

2.10.1

2.10

Primary changes

Install or upgrade

Install from PyPI with pip:

... (truncated)

Changelog

Sourced from jinja2's changelog.

Version 2.11.3

Released 2021-01-31

  • Improve the speed of the urlize filter by reducing regex backtracking. Email matching requires a word character at the start of the domain part, and only word characters in the TLD. :pr:1343

Version 2.11.2

Released 2020-04-13

  • Fix a bug that caused callable objects with __getattr__, like :class:~unittest.mock.Mock to be treated as a :func:contextfunction. :issue:1145
  • Update wordcount filter to trigger :class:Undefined methods by wrapping the input in :func:soft_str. :pr:1160
  • Fix a hang when displaying tracebacks on Python 32-bit. :issue:1162
  • Showing an undefined error for an object that raises AttributeError on access doesn't cause a recursion error. :issue:1177
  • Revert changes to :class:~loaders.PackageLoader from 2.10 which removed the dependency on setuptools and pkg_resources, and added limited support for namespace packages. The changes caused issues when using Pytest. Due to the difficulty in supporting Python 2 and :pep:451 simultaneously, the changes are reverted until 3.0. :pr:1182
  • Fix line numbers in error messages when newlines are stripped. :pr:1178
  • The special namespace() assignment object in templates works in async environments. :issue:1180
  • Fix whitespace being removed before tags in the middle of lines when lstrip_blocks is enabled. :issue:1138
  • :class:~nativetypes.NativeEnvironment doesn't evaluate intermediate strings during rendering. This prevents early evaluation which could change the value of an expression. :issue:1186

Version 2.11.1

Released 2020-01-30

  • Fix a bug that prevented looking up a key after an attribute ({{ data.items[1:] }}) in an async template. :issue:1141

... (truncated)

Commits


Dependabot compatibility score

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


Dependabot commands and options
You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) - `@dependabot use these labels` will set the current labels as the default for future PRs for this repo and language - `@dependabot use these reviewers` will set the current reviewers as the default for future PRs for this repo and language - `@dependabot use these assignees` will set the current assignees as the default for future PRs for this repo and language - `@dependabot use this milestone` will set the current milestone as the default for future PRs for this repo and language You can disable automated security fix PRs for this repo from the [Security Alerts page](https://github.com/mbylstra/django-wham/network/alerts).

How to auth with username:password and without token?

opened on 2017-04-24 10:01:29 by Hann1bal

How to auth with username:password and without token? I can't find how auth. without fast authenficate with twitter, facebook, google, and etc.... ?

Still active?

opened on 2016-08-20 13:54:32 by andybak

I thought django-wham was a great idea. I am about to integrate with justgiving and I wondered if the project was still active?

Michael Bylstra
GitHub Repository