kcrw.apple_news classes

Library for making Apple News API requests

class kcrw.apple_news.api.API(key_id, key_secret, channel_id)[source]

Apple News API object

__init__(key_id, key_secret, channel_id)[source]
Parameters:
  • key_id (str) – Apple News API Key Id
  • key_secret (str) – Apple News API Secret
  • channel_id (str) – Apple News Channel Id
channel_id = None
create_article(article, metadata=None, assets=None)[source]

Creates and uploads a multi-part article. See Create an Article

article and metadata should contain JSON-serializable dictionaries according the API specification for article data and optional article creation metadata. assets should contain a dict mapping of filenames to file data for all supplemental assets needed for the article.

Parameters:
  • article (dict -- or None) – Article data in JSON serializable python dict
  • metadata – Optional metadata in JSON serializable python dict
  • assets (dict -- or None) – Dict with filename -> data mapping of additional assets
Returns:

dict – the JSON data returned by the API.

Raises:

AppleNewsException

delete_article(identifier)[source]

Deletes an existing a article. See Delete an Article.

Requires an identifier for an existing article.

Parameters:identifier (str) – An identifier for an existing Apple News article
Returns:dict – the JSON data returned by the API.
Raises:AppleNewsException
key_id = None
key_secret = None
read_article(identifier)[source]

Retrieves information about an existing a article. See Read Article Information.

Requires an identifier for an existing article.

Parameters:identifier (str) – An identifier for an existing Apple News article
Returns:dict – the JSON data returned by the API.
Raises:AppleNewsException
read_channel()[source]

Read basic information about the current Apple News Channel

Returns:dict – the JSON data returned by the API.
Raises:AppleNewsException
send_request(method, route, body=None, content_type=None)[source]

Sends a signed request to the Apple News Publisher API.

Parameters:
  • method (str) – The HTTP method for the request (e.g. GET, POST, …)
  • route (str) – The API route for the request
  • body (str) – The request body
  • content_type (str) – The request content type
Returns:

dict – the JSON data returned by the API.

Raises:

AppleNewsException

update_article(identifier, metadata, article=None, assets=None)[source]

Updates an existing a article. See Update an Article.

Requires an identifier for an existing article, and metadata containing the current article revision identifier. All other arguments are optional.

Parameters:
  • identifier (str) – An identifier for an existing Apple News article
  • metadata (dict) – JSON formatted article metadata
  • article (dict -- or None) – Article data in JSON serializable python dict
  • assets (dict -- or None) – Dict with filename -> data mapping of additional assets
Returns:

dict – the JSON data returned by the API.

Raises:

AppleNewsException

url_base = 'https://news-api.apple.com'
exception kcrw.apple_news.api.AppleNewsError(*args, **kw)[source]

Exception class for errors related to Apple News API requests

__init__(*args, **kw)[source]

Initialize self. See help(type(self)) for accurate signature.

code = None
data = None
kcrw.apple_news.api.ensure_binary(s, encoding='utf8')[source]
kcrw.apple_news.api.ensure_text(s, encoding='utf8')[source]