Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 37 additions & 69 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,93 +12,61 @@ Provides convenience functionality to query every endpoint in the Epub Library.

# Using it

## Creating a client
See [usage description](/doc/use.md) for detailed examples.

Use the provided factory methods to initialise a new client.
## Gradle

```Java
EpubClient client = EpubClientFactory.newDefaultClient("<url of your epub library api>");
```

There are also other factory methods for different kinds of clients

## Caching credentials

Every entity queriable in the api is cacheable. Credentials are not special in this regard, though they are special in the way that caching them is highly recommended.

The Epub Library API works with JSON Web Tokens. Those tokens expire after some time, not reusing them creates a lot of unnecessary traffic, as every operation needs a new login though.
First add the following in the `repositories` block in your `build.gradle`:

To make caching easier, the `EpubClient` does it automatically, if you configure it that way.

The simples way to create a client that caches credentials is using the factory method `EpubClientFactory.newCredentialCacheClient(String)` and saving your credentials to the cache:

```Java
EpubClient client = EpubClientFactory.newCredentialCacheClient("<url>");
client.cacheValue(CacheType.CREDENTIALS, CredentialCacheKeys.USER, "username");
client.cacheValue(CacheType.CREDENTIALS, CredentialCacheKeys.PASSWORD, "pw");
```Gradle
repositories {
maven {
name = "github"
url = 'https://maven.pkg.github.com/GeKoppe/epub_library-client'
credentials {
username = findProperty('gpr.user') ?: ''
password = findProperty('gpr.key') ?: ''
}
}
```

Afterwards every operation will be automatically authenticated against the api.

## Querying

For every endpoint, there are two convenience methods in the EpubClient class to query said endpoint. One for clients that cache credentials, the other one for creating a new session every time. It is recommended to use a client that caches credentials in order to not create too many json web tokens.
Your GitHub credentials must be stored in the `~/.gradle/settings.gradle` file for this to work.

### Example of getting an entity
Then add the dependency:

Querying an entity is as simple as calling it's respective `.get` method in the `EpubClient`. This example will demonstrate that with an Epub, it works the same with every other entity though.

To get an epub for a specified id, you can just call `EpubClient.getEpub(long, HttpQuery)`. Depending on whether your client caches credentials or not, you can also call `EpubClient.getEpub(String, String, long, HttpQuery)` and provide username and password.

The `HttpQuery` parameter is used to define what parts of the given entity is returned (e.g. just the basics; authors; genres etc.). For every entity, a builder class for Http queries exist to simplify the filtering.

This is an example for getting the epub with id 1, including all authors and genres, with a client that does not cache the credentials:

```Java
EpubClient client = EpubClientFactory.newDefaultClient("<url>");

HttpQuery query = new EpubQueryBuilder()
.withAuthors(true)
.withGenres(true)
.build();

try {
// Contains epub info, authors and genres
EpubDto epub = client.getEpub("user", "password", 1L, query);
} catch (Exception ex) {
// If the api call fails, an exception representing the reason is thrown
```Gradle
dependencies {
implementation 'org.koppe.epub.client:epub-lib-client:<version-number>'
}
```

## Caching

Caching has been discussed in the chapter [Caching Credentials](#caching-credentials). Other types of entities might be cached as well though. This will again be demonstrated with epubs but works the same with every other entity too.
## Maven

If you just want a default cache (15 minute retention of entities, no refresh except for credentials), just use the provided factory method:
Add the following to your `pom.xml`:

```Java
EpubClient cachingClient = EpubClientFactory.newCachingClient("url", new CacheType[]{ CacheType.EPUBS, CacheType.CREDENTIALS });
```XML
<repositories>
<repository>
<id>github</id>
<url>https://maven.pkg.github.com/GeKoppe/epub_library-client</url>
</repository>
</repositories>
```

Before and after every call, the client will check the corresponding cache to see, whether an entity already exists, needs to be updated or deleted.

If you want custom caches in your client, you can also register a new cache manually:
Your GitHub credentials must be stored in the `~/.m2/settings.xml` file for this to work.

```Java
EpubClient client = EpubClientFactory.newDefaultClient("url");
Then add the following dependency:

EpubCache cache = new EpubCache();
cache.setMaxElements(10); // Set maximum number of elements in cache
cache.setRetention(10L, TimeUnit.MINUTES); // Set time after which elements are ejected or refreshed
cache.refreshFunction((key) -> {
// Some custom refresh logic
});

client.registerCache(CacheType.EPUBS, cache);
```XML
<dependencies>
<dependency>
<groupId>org.koppe.epub.client</groupId>
<artifactId>epub-lib-client</artifactId>
<version>[version-number]</version>
</dependency>
</dependencies>
```

If you now do an epub operation, the client will use the cache you supplied.


# Changelog

Expand Down
18 changes: 18 additions & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# Changelog

## 0.0.3

**Type**: Pre-release

**Description**: Secondary test of publishing. This release was published to test implementation in another project. Don't use this!

**Features**:

More author functions have been added.

**Fixes**:

None

**Known Issues**:

- Many (as I said, don't use this.)

## 0.0.2

**Type**: Pre-release
Expand Down
88 changes: 88 additions & 0 deletions doc/use.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# How to use

## Creating a client

Use the provided factory methods to initialise a new client.

```Java
EpubClient client = EpubClientFactory.newDefaultClient("<url of your epub library api>");
```

There are also other factory methods for different kinds of clients

## Caching credentials

Every entity queriable in the api is cacheable. Credentials are not special in this regard, though they are special in the way that caching them is highly recommended.

The Epub Library API works with JSON Web Tokens. Those tokens expire after some time, not reusing them creates a lot of unnecessary traffic, as every operation needs a new login though.

To make caching easier, the `EpubClient` does it automatically, if you configure it that way.

The simples way to create a client that caches credentials is using the factory method `EpubClientFactory.newCredentialCacheClient(String)` and saving your credentials to the cache:

```Java
EpubClient client = EpubClientFactory.newCredentialCacheClient("<url>");
client.cacheValue(CacheType.CREDENTIALS, CredentialCacheKeys.USER, "username");
client.cacheValue(CacheType.CREDENTIALS, CredentialCacheKeys.PASSWORD, "pw");
```

Afterwards every operation will be automatically authenticated against the api.

## Querying

For every endpoint, there are two convenience methods in the EpubClient class to query said endpoint. One for clients that cache credentials, the other one for creating a new session every time. It is recommended to use a client that caches credentials in order to not create too many json web tokens.

### Example of getting an entity

Querying an entity is as simple as calling it's respective `.get` method in the `EpubClient`. This example will demonstrate that with an Epub, it works the same with every other entity though.

To get an epub for a specified id, you can just call `EpubClient.getEpub(long, HttpQuery)`. Depending on whether your client caches credentials or not, you can also call `EpubClient.getEpub(String, String, long, HttpQuery)` and provide username and password.

The `HttpQuery` parameter is used to define what parts of the given entity is returned (e.g. just the basics; authors; genres etc.). For every entity, a builder class for Http queries exist to simplify the filtering.

This is an example for getting the epub with id 1, including all authors and genres, with a client that does not cache the credentials:

```Java
EpubClient client = EpubClientFactory.newDefaultClient("<url>");

HttpQuery query = new EpubQueryBuilder()
.withAuthors(true)
.withGenres(true)
.build();

try {
// Contains epub info, authors and genres
EpubDto epub = client.getEpub("user", "password", 1L, query);
} catch (Exception ex) {
// If the api call fails, an exception representing the reason is thrown
}
```

## Caching

Caching has been discussed in the chapter [Caching Credentials](#caching-credentials). Other types of entities might be cached as well though. This will again be demonstrated with epubs but works the same with every other entity too.

If you just want a default cache (15 minute retention of entities, no refresh except for credentials), just use the provided factory method:

```Java
EpubClient cachingClient = EpubClientFactory.newCachingClient("url", new CacheType[]{ CacheType.EPUBS, CacheType.CREDENTIALS });
```

Before and after every call, the client will check the corresponding cache to see, whether an entity already exists, needs to be updated or deleted.

If you want custom caches in your client, you can also register a new cache manually:

```Java
EpubClient client = EpubClientFactory.newDefaultClient("url");

EpubCache cache = new EpubCache();
cache.setMaxElements(10); // Set maximum number of elements in cache
cache.setRetention(10L, TimeUnit.MINUTES); // Set time after which elements are ejected or refreshed
cache.refreshFunction((key) -> {
// Some custom refresh logic
});

client.registerCache(CacheType.EPUBS, cache);
```

If you now do an epub operation, the client will use the cache you supplied.
2 changes: 1 addition & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = 'org.koppe.epub.client'
version = '0.0.2'
version = '0.0.3'
description = 'Client library for an epub library application'

repositories {
Expand Down
Loading
Loading