Skip to content

A Java wrapper around https://tvtime.com Rest API using Retrofit 2 and Moshi for json serialization.

License

Notifications You must be signed in to change notification settings

srgsf/tvtime-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tvtime-client

Tests License maven
A Java wrapper around https://tvtime.com Rest API using Retrofit 2 and Moshi for json serialization.

Pull requests are welcome.

Usage

Add the following dependency to your Gradle project:

implementation 'com.github.srgsf:tvtime-client:1.0'

Or for Maven:

<dependency>
  <groupId>com.github.srgsf</groupId>
  <artifactId>tvtime-client</artifactId>
  <version>1.0</version>
</dependency>

Authorization

The TV Time API can only be accessed via OAuth 2.0.

Current version supports Web Application flow and Limited Device flow. See TvTimeAuthClient for details.

Example of Limited Device flow token polling.

     TvTimeAuthClient authClient = new TvTimeAuthClient.Builder()
                    .clientCredentials("APIClientId", "APIClientSecret")
                    .build();
    
            DeviceCode code = null;
            Response<DeviceCode> codeResponse = authClient.deviceCode();
            if (codeResponse.isSuccessful()) {
                code = codeResponse.body();
                showToTheUser(code.userCode, code.verificationUrl);
                String token = token(code);
            }
            
...

String token(DeviceCode code) throws IOException, InterruptedException {
        Instant expire = Instant.now().plusSeconds(code.expiresIn);
        //polling
        while (true) {
            Thread.sleep(code.interval * 1000);
            Response<String> tokenResponse = authClient.accessToken(code.deviceCode);
            if (tokenResponse.isSuccessful()) {
                return tokenResponse.body();
            }

            if (Instant.now().isAfter(expire)) {
                return null;
            }

        }
    }

Example

Use like any other retrofit2 based service.
Optionally you can share OkHttp client and Retrofit instances to keep single request pooling, disk cache, routing logic, etc.

TvTime client = new TvTime.Builder()
                .tokenProvider(() -> "<TOKEN>")
                .build();
TvTimeInfo api = client.info();
Response<User> response = api.user().execute();
    if (response.isSuccessful()) {
        User u = response.body();
        //use user
    }

See test cases in src/test/ for more examples.

Known API Issues

About

A Java wrapper around https://tvtime.com Rest API using Retrofit 2 and Moshi for json serialization.

Resources

License

Stars

Watchers

Forks

Languages