\documentclass{article}
\usepackage{ulem}
\pagestyle{headings}
\begin{document}
\tableofcontents

\part{ Project Name}
Project to rewrite parts of Horde\_Rpc and the Horde Core Registry inter-app API for a new major release

\section{ Bugs}
List any tickets on http://bugs.horde.org/\footnote{http://bugs.horde.org/} that cover this issue or are relevant to it.

\section{ People}
Ralf Lang

\section{ Description}
Main Design Goals:

- Facilitate mapping of free form URIs to API calls (for REST)\newline
- Facilitate customizing/amending APIs by administrator/downstream\newline
- Allow passing PHP Objects when using the API internally -> Need a defined way to fold them to array/scalar representations on RPC\newline
- Allow separate authentication mechanisms for external calls (REST, RPC) like using API keys, even limiting them.\newline
- Allow unauthenticated usage of select RPC API calls.\newline
- Ability for administrators to turn off remote access to some APIs\newline
- Avoid boilerplate inside Api method implementation\newline
- Little or no setup for default case\newline
- Allow but don't require versioning\newline
- keep code upgrade costs from H5 to H6 reasonable

Side Goal:\newline
- Allow routing API calls to external horde instances

\section{ Current H5 API}
API routing

An API is a named collection of methods

H5 registry allows to route an API to an app and to override this decision for certain methods\newline
For example, ingo is the default target for "filter" api and the defined target for certain methods of the "mail" api, which defaults to imp

'provides' => array(\newline
            'filter',\newline
            'mail/blacklistFrom',\newline
            'mail/showBlacklist',\newline
            'mail/whitelistFrom',\newline
            'mail/showWhitelist',\newline
            'mail/applyFilters',\newline
            'mail/canApplyFilters',\newline
            'mail/showFilters',\newline
            'mail/newEmailFilter'\newline
        ),

However, currently an App cannot provide separate implementations for the same method name in different APIs. Calling filter/newEmailFilter would yield the mail/newEmailFilter method due to all methods being part of one collective Api.php

The current API also does not allow named parameters, even if some RPC mechanisms would deal with it

If an API provides an evolving protocol, discerning versions and supporting older input and output formats would be the method implementation's task

The registry allows limited runtime introspection (hasMethod, listMethods, listApis) but no hints on input/output formats (parameter list and type)

\section{ Proposed H6 API changes}
\subsection{ implementation strategy}
A runner checks all known ApiTypes\footnote{http://example.com/index.php?page=ApiTypes} (Jsonrpc, Rest, Xmlrpc, \ldots) if it can handle the request until it finds a suitable candidate\newline
- Each ApiType\footnote{http://example.com/index.php?page=ApiType} decides by request headers, URI scheme, request method, and request body content to rule out api types as fast/cheap as possible\newline
- A formally suitable apitype checks its router for known command implementations which match any parameter set (path, method, subdomain\ldots)\newline
- ApiType\footnote{http://example.com/index.php?page=ApiType} unpacks request parameters from apitype specific format\newline
- Perform any prefilter tasks like authentication, permission checking, if required by router configuration.\newline
- Delegate execution to the implementation class and method found from the router\newline
- pack return value into backend specific format (if not directly returned by implementation class)\newline
- Apply post filters if configured (API accounting, gzip postprocessing, \newline
- The runner writes the response to the stdout\newline
OPTIONAL\newline
- A simplified ApiType\footnote{http://example.com/index.php?page=ApiType} for internal/interapp calls allows to skip most detection magic but use the extra features is desired - no runner needed here

\subsection{ misc ideas}
\begin{itemize}
\item{Split \$App\_Api class into \$App\_Api\_\$Api classes - Apis still need to be registered in registry. (MEDIUM)}
\end{itemize}
-> Limited support with fallback to \$App\_Api exists in https://github.com/ralflang/Core/tree/class-per-api\footnote{https://github.com/ralflang/Core/tree/class-per-api}\newline
-> needs cleanup

\begin{itemize}
\item{Implement parameter and return hints -- TBD -> only needed for external interfaces? (Needed for REST, maybe SOAP)}
\end{itemize}
-> Generate openapi data from these hints

\begin{itemize}
\item{move DAV browsing/CRUD code to API methods -> Does this also make sense for ActiveSync\footnote{http://example.com/index.php?page=ActiveSync}? (OPTIONAL)}
\begin{itemize}
\item{Factor method routing out of Horde\_Rpc\_* into a loadable module to decouple Horde\_Rpc from the Horde ecosystem (HIGH)}
\end{itemize}
\end{itemize}
-> The Rest feature will not use the Horde\_Rpc package, at least in its current form. Lessons Learned and helper infrastructure should go into a rewrite for Horde\_Rpc backends with some smooth transition path.

-> For any meaningful Rest support, Horde\_Controller needs to support raw request body. \newline
https://github.com/horde/Controller/pull/1\footnote{https://github.com/horde/Controller/pull/1}

A simple boilerplate router should be part of Horde\_Rpc to facilitate unit testing\newline
A Horde-specific router Horde\_Core\_Rpc\_Router should be part of Horde\_Core and interact with registry

Call internal api \$App\_Api\_\$Api to actually run a command\newline
should check what returns for PHP objects and reduce them to arrays (\_\_sleep or Serializable)\newline
allow plugging separate authentication, accounting \ldots\newline
may have to be amended by specific classes or config to support some RPC types (REST)

In Question: Does it make sense to also wrap internal calls into a Runner\_Internal ?

\$registry->calendar->addEvent(\$args);\newline
\$registry->call('calendar/addEvent', \$args);

The Internal runner simply moves setting up the execution environment and calling the actual class and method from the registry class.\newline
As of now, no additional functionality is considered but it may be handy for debugging.

\section{ Accounting}
Accounting is optional and will probably only be implemented if somebody is interested. Accounting defines a set of limits (value per timespan) and stores usage timestamps in a backend (user, timestamp, action). If the number of usages inside a sliding window hits the limit, the request is denied. \newline
Asking for a past window (like first of month, first of next month) allows for billing/reporting, if this is of any interest.\newline
Implementation could use horde\_histories, horde\_locks (no billing due to cleanup) or a text file backend (probably inefficient).

The accounting lib can probably be reused for other things.

\section{ CODE UPGRADE}
For internal app calls, git mv \$app/lib/Api.php to \$app/lib/Api/\$Api.php and change class name. Move overrides into a separate class. Done.\newline
For most external calls, additionally extend to-be-written Horde\_Core\_Api\_Rpc into \$App\_Api\_\$Api\_Rpc - probably the defaults will be just fine for upgrade.

\section{ API Protection}
Protection modules may be combined, probably at first we only want Horde Auth (like Login, for ActiveSync\footnote{http://example.com/index.php?page=ActiveSync}, DAV) and Extra Auth (API keys or SSL)

No Auth\newline
Horde Default Auth\newline
Admin Only\newline
Need Permission (implies any auth)\newline
Extra Auth (how to map to Horde Users for permissions?)

Per-User global accounting\newline
Per-User Per-Api accounting\newline
Per-User Per-Method accounting

\section{ CONFIG:}
Horde Level: \newline
Rpc\newline
Decide which RPC Types are generally enabled. Default?\newline
Decide which RPC Types use Horde Auth, Separate Auth or both

Accounting\newline
Set up accounting system at all (yes/no)\newline
Set up global per-user accounting (yes/no)\newline
Set up anonymous accounting (yes/no)

App Level:\newline
Upstream provides defaults on which RPC methods are available and how they are protected (multiple available).\newline
Admin / Config may override, empty config uses defaults

\section{ API characteristics}
LOCAL: INTERNAL

- use registry to discover API methods and their implementations\newline
- any api/method combination may be served by any application (registry knows)\newline
- no real metadata on parameter count and formats available\newline
- currently no named parameters\newline
- can directly use objects as parameters or return types (rarely used yet, breaks external apis as of now)

RPC: JSON-RPC, XML-RPC

- use registry to discover API methods and their implementations\newline
- wraps INTERNAL API in specific formats (JSON, XML)\newline
- Currently breaks for INTERNAL apis which use objects\newline
- Requires authentication, but any authorization/limits beyond that is left to the INTERNAL method implementation\newline
- Needs custom serialization for API objects which don't implement \_\_toString\newline
- In theory, named parameters are possible, but the Horde infrastructure can't handle them\newline
- HTTP-wise, it's POST to a well-known endpoint\newline
- need to parse data to find the appropriate api/method, hence implementation

Webdav, Caldav, Carddav:\newline
- Fixed set of methods\newline
- Webdav implemented for multiple resources, Caldav/Carddav backends are really designed to work for one app each\newline
- No need to discover or route methods, can use fixed class layout for implementation (browse, delete, \ldots)

REST:\newline
- Resource-centric\newline
- Should we assume each registry API is a resource?\newline
- Any URL formats and lengths possible, \newline
- methods per resource should be limited to http verbs\newline
- multiple input and output formats may be governed by content type\newline
- Supports multiple API versions in parallel (either by URL or by content type)\newline
- each request must be authenticated, no sessions.

Horde AJAX Framework\newline
- Not currently part of the RPC family\newline
- separate endpoint\newline
- URL exposes application and method\newline
- Uses cookies/sessions/tokens\newline
- mostly JSON

SOAP:\newline
- Unsure if it really still works\newline
- similar to XML-RPC\newline
- but SHOULD generate meta information for the client (where? how?)

Common:\newline
- We can discern each http remote protocol by URL and/or content type. What is not covered, should be assumed REST.\newline
- Once we know the protocol, we know how to find out API and method (and, via registry, implementation)

\section{ Resources}

\noindent\rule{\textwidth}{1pt}
Back to the Project List\footnote{http://example.com/index.php?page=Project}

\end{document}
