Skip to content
Open
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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,35 @@ Sign up for an API key [here](https://open-platform.theguardian.com/access)
The primary `Client\GuardianApi` class is a factory class that creates objects for each of the API modules, allowing you to make requests to any of them with your desired request parameters. You have to first create an object for it, then access your desired API module via the object. See the code snippets below:

```java
GuardianApi guardianApi = this.guardianApi = new GuardianApi(THE_GUARDIAN_API_KEY);
GuardianApi guardianApi = new GuardianApi(THE_GUARDIAN_API_KEY);
```

**For Content:**

```java
ContentResponse response = this.guardianApi.content().setQuery("12 years a slave").setTag("film/film,tone/reviews").setFromDate("2023-03-20").setShowTags("contributor").setShowFields("starRating,headline,thumbnail,short-url").setOrderBy("relevance").fetch();
ContentResponse response = (ContentResponse)this.guardianApi.content().setQuery("12 years a slave").setTag("film/film,tone/reviews").setFromDate("2023-03-20").setShowTags("contributor").setShowFields("starRating,headline,thumbnail,short-url").setOrderBy("relevance").fetch();
```

**For Tags:**

```java
TagsResponse response = this.guardianApi.tags().setQuery("apple").setSection("technology").setShowReferences("all").fetch();
TagsResponse response = (TagsResponse)this.guardianApi.tags().setQuery("apple").setSection("technology").setShowReferences("all").fetch();
```

**For Sections:**

```java
SectonsResponse response = tthis.guardianApi.sections().setQuery("business").fetch();
SectonsResponse response = (SectonsResponse)this.guardianApi.sections().setQuery("business").fetch();
```

**For Editions:**

```java
EditionsResponse response = this.guardianApi.editions().setQuery("uk").fetch();
EditionsResponse response = (EditionsResponse)this.guardianApi.editions().setQuery("uk").fetch();
```

**For Single Item:**

```java
SingleItemResponse response = this.guardianApi.singleItem().setId("/sport/2022/oct/07/cricket-jos-buttler-primed-for-england-comeback-while-phil-salt-stays-focused").setShowStoryPackage(true).setShowEditorsPicks(true).setShowMostViewed(true).setShowRelated(true).fetch();
SingleItemResponse response = (SingleItemResponse)this.guardianApi.singleItem().setId("/sport/2022/oct/07/cricket-jos-buttler-primed-for-england-comeback-while-phil-salt-stays-focused").setShowStoryPackage(true).setShowEditorsPicks(true).setShowMostViewed(true).setShowRelated(true).fetch();
```
31 changes: 19 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@

<groupId>io.github.niyiomotoso</groupId>
<artifactId>the-guardian-api-java-client</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
<packaging>jar</packaging>

<name>the-guardian-api-java-client</name>
<url>https://github.com/niyiomotoso/the-guardian-api-java-client</url>
<description>Java client library for the Guardian APIs. All available API modules are supported - Content, Tags, Sections, Editions, and Single Item.</description>
<description>Java client library for The Guardian Open Platform API. All available API modules are supported - Content, Tags, Sections, Editions, and Single Item.
Sign up for an API key on The Guardian Open Platform.
</description>

<organization>
<name>open-platform.theguardian.com/</name>
<name>open-platform.theguardian.com</name>
<url>https://open-platform.theguardian.com/</url>
</organization>

Expand All @@ -30,18 +32,19 @@
</scm>

<developers>
<developer>
<name>Nonso Mgbechi</name>
<email>mgbechinonso@gmail.com</email>
<organization>kingdageek.github.io</organization>
<organizationUrl>https://kingdageek.github.io</organizationUrl>
</developer>
<developer>
<name>Niyi Omotoso</name>
<email>omoniyiomotoso@gmail.com</email>
<organization>niyiomotoso.github.io</organization>
<organizationUrl>https://niyiomotoso.github.io</organizationUrl>
</developer>

<developer>
<name>Nonso Mgbechi</name>
<email>mgbechinonso@gmail.com</email>
<organization>kingdageek.github.io</organization>
<organizationUrl>https://kingdageek.github.io</organizationUrl>
</developer>
</developers>

<distributionManagement>
Expand All @@ -60,7 +63,7 @@
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.8</version>
<version>1.6.13</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
Expand All @@ -86,13 +89,17 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.3.0</version>
<version>3.2.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
<id>main-javadoc</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<release>11</release>
</configuration>
</execution>
</executions>
</plugin>
Expand Down
150 changes: 150 additions & 0 deletions pom.xml.versionsBackup
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.github.niyiomotoso</groupId>
<artifactId>the-guardian-api-java-client</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<name>the-guardian-api-java-client</name>
<url>https://github.com/niyiomotoso/the-guardian-api-java-client</url>
<description>Java client library for the Guardian APIs. All available API modules are supported - Content, Tags, Sections, Editions, and Single Item.
Sign up for an API key on The Guardian Open Platform.
</description>

<organization>
<name>open-platform.theguardian.com/</name>
<url>https://open-platform.theguardian.com/</url>
</organization>

<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0</url>
</license>
</licenses>

<scm>
<url>https://github.com/niyiomotoso/the-guardian-api-java-client</url>
<connection>scm:git:git://github.com/niyiomotoso/the-guardian-api-java-client.git</connection>
<developerConnection>scm:git:git@github.com/niyiomotoso/the-guardian-api-java-client.git</developerConnection>
</scm>

<developers>
<developer>
<name>Niyi Omotoso</name>
<email>omoniyiomotoso@gmail.com</email>
<organization>niyiomotoso.github.io</organization>
<organizationUrl>https://niyiomotoso.github.io</organizationUrl>
</developer>

<developer>
<name>Nonso Mgbechi</name>
<email>mgbechinonso@gmail.com</email>
<organization>kingdageek.github.io</organization>
<organizationUrl>https://kingdageek.github.io</organizationUrl>
</developer>
</developers>

<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.13</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>main-javadoc</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<release>11</release>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>1.4.9</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
57 changes: 45 additions & 12 deletions src/main/java/the/guardian/api/entity/Content.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,94 @@
import com.mashape.unirest.http.exceptions.UnirestException;
import com.mashape.unirest.request.HttpRequest;
import the.guardian.api.http.AbstractResponse;
import the.guardian.api.http.content.ContentResponse;
import the.guardian.api.http.content.ContentResponseWrapper;

import java.io.UnsupportedEncodingException;

/**
* Class Content
* The content guardian API entity
* Represents a Content entity for interacting with content-related operations of The Guardian API.
*/

public class Content extends ContentApiEntity {
private String queryFields = "";

/**
* Constructs a Content instance with the provided API URL.
*
* @param _apiUrl The API URL for content-related operations.
*/
public Content(String _apiUrl) {

super(_apiUrl);
this.queryFields = "";
}


/**
* Retrieves the query fields.
*
* @return The query fields.
*/
public String getQueryFields() {
return this.queryFields;
}

/**
* Retrieves the query.
*
* @return The query.
*/
public String getQuery() {
return this.query;
}


/**
* Sets the query.
*
* @param query The query.
* @return The updated Content instance.
*/
public Content setQuery(String query) {
this.query = query;

return this;
}


/**
* Sets the query fields.
*
* @param queryFields The query fields.
* @return The updated Content instance.
*/
public Content setQueryFields(String queryFields) {
this.queryFields = queryFields;

return this;
}

/**
* Builds the URL for API requests.
*
* @return The built URL.
* @throws UnsupportedEncodingException If encoding URL components fails.
*/
public String buildUrl() throws UnsupportedEncodingException {
super.buildUrl();
this.appendToBaseUrl("q", this.getQuery())
.appendToBaseUrl("query-fields", this.getQueryFields());

return this.baseUrl;
}


/**
* Fetches data from the API and returns the response.
*
* @return The API response as an AbstractResponse object.
* @throws UnsupportedEncodingException If encoding URL components fails.
* @throws UnirestException If a Unirest HTTP request exception occurs.
*/
public AbstractResponse fetch() throws UnsupportedEncodingException, UnirestException {
this.buildUrl();
String url = this.getBaseUrl();
System.out.println("urlx "+url);
HttpRequest request = Unirest.get(url).header("accept", "application/json");
HttpResponse<ContentResponseWrapper> response = request.asObject(ContentResponseWrapper.class);

return response.getBody().getResponse();
}
}
Loading