Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Brewery search #5

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
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
74 changes: 74 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,77 @@
.project
.settings
target/

# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/modules.xml
# .idea/*.iml
# .idea/modules

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

.idea/compiler.xml
.idea/encodings.xml
.idea/misc.xml
.idea/modules.xml
.idea/vcs.xml
untappd4j.iml
32 changes: 24 additions & 8 deletions src/main/java/br/com/thiagomoreira/untappd/Untappd.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,12 @@
import java.io.IOException;
import java.lang.annotation.Annotation;

import br.com.thiagomoreira.untappd.model.*;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import br.com.thiagomoreira.untappd.gson.ResponseDeserializaer;
import br.com.thiagomoreira.untappd.model.Beer;
import br.com.thiagomoreira.untappd.model.Beers;
import br.com.thiagomoreira.untappd.model.Brewery;
import br.com.thiagomoreira.untappd.model.Response;
import br.com.thiagomoreira.untappd.model.User;
import br.com.thiagomoreira.untappd.model.Venue;
import br.com.thiagomoreira.untappd.gson.ResponseDeserializer;
import br.com.thiagomoreira.untappd.security.UntappdAuthorizationInterceptor;
import br.com.thiagomoreira.untappd.service.BeerService;
import br.com.thiagomoreira.untappd.service.BreweryService;
Expand Down Expand Up @@ -68,7 +63,7 @@ public Untappd(String clientId, String clientSecret, String accessToken,
String baseUrl, boolean debug) {
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.registerTypeAdapter(new TypeToken<Response>() {
}.getType(), new ResponseDeserializaer()).create();
}.getType(), new ResponseDeserializer()).create();

OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();

Expand Down Expand Up @@ -108,6 +103,27 @@ public Brewery getBrewery(long breweryId) throws IOException {
}
}

/**
* This will allow you to search exclusively for breweries in the Untappd system.
*
* @param breweryQuery (required) The search term that you want to search
* @param offset (optional) The numeric offset that you what results to start
* @param limit (optional) The number of results to return, max of 50, default is 25
* @return {@link BrewerySearch}
* @throws IOException IOException
*/
public BrewerySearch brewerySearch(String breweryQuery, Integer offset, Integer limit) throws IOException {
Call<Response> call = breweryService.searchBrewery(breweryQuery, offset, limit);
retrofit2.Response<Response> response = call.execute();
Response response2 = response.body();

if (response.isSuccessful()) {
return (BrewerySearch) response2.getResponse();
} else {
throw handleError(response);
}
}

public Beer getBeer(long beerId) throws IOException {
Call<Response> call = beerService.getBeer(beerId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,15 @@

import java.lang.reflect.Type;

import br.com.thiagomoreira.untappd.model.*;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

import br.com.thiagomoreira.untappd.model.Beer;
import br.com.thiagomoreira.untappd.model.Beers;
import br.com.thiagomoreira.untappd.model.Brewery;
import br.com.thiagomoreira.untappd.model.Meta;
import br.com.thiagomoreira.untappd.model.Response;
import br.com.thiagomoreira.untappd.model.User;
import br.com.thiagomoreira.untappd.model.Venue;

public class ResponseDeserializaer implements JsonDeserializer<Response> {
public class ResponseDeserializer implements JsonDeserializer<Response> {

@Override
public Response deserialize(JsonElement json, Type typeOfT,
Expand All @@ -42,14 +35,21 @@ public Response deserialize(JsonElement json, Type typeOfT,
Meta meta = context.deserialize(rootJsonObject.get("meta"), Meta.class);
Response response = new Response();

response.setMeta(meta);;
response.setMeta(meta);

JsonElement responseJsonElement = rootJsonObject.get("response");
if (responseJsonElement.isJsonObject()) {
JsonObject responseJsonObject = responseJsonElement
.getAsJsonObject();

if (responseJsonObject.has("brewery")) {

if (responseJsonObject.has("brewery") && responseJsonObject.has("engine")) {
BrewerySearch breweryInfo = context.deserialize(
responseJsonObject, BrewerySearch.class);

response.setResponse(breweryInfo);
}
if (responseJsonObject.has("brewery") && !responseJsonObject.has("engine")) {
Brewery breweryInfo = context.deserialize(
responseJsonObject.get("brewery"), Brewery.class);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Copyright © 2016 Thiago Moreira ([email protected])
*
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -19,10 +19,15 @@
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;

public interface BreweryService {

@GET("/v4/brewery/info/{breweryId}")
Call<Response> getBrewery(@Path("breweryId") long breweryId);
@GET("/v4/brewery/info/{breweryId}")
Call<Response> getBrewery(@Path("breweryId") long breweryId);

@GET("/v4/search/brewery")
Call<Response> searchBrewery(@Query("q") String query,
@Query("offset") Integer offset, @Query("limit") Integer limit);

}
21 changes: 21 additions & 0 deletions src/main/schema/brewery_results.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"count": {
"type": "integer"
},
"items": {
"type": "array",
"items": {
"javaName": "BrewerySearchItem",
"type": "object",
"properties": {
"brewery": {
"javaType": "br.com.thiagomoreira.untappd.model.Brewery"
}
}
}
}
}
}
31 changes: 31 additions & 0 deletions src/main/schema/brewery_search.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"engine": {
"type": "string"
},
"page": {
"type": "integer"
},
"search_type": {
"type": "string"
},
"sort": {
"type": "string"
},
"term": {
"type": "string"
},
"key": {
"type": "string"
},
"found": {
"type": "integer"
},
"brewery": {
"javaName": "Breweries",
"javaType": "br.com.thiagomoreira.untappd.model.BreweryResults"
}
}
}
18 changes: 14 additions & 4 deletions src/test/java/br/com/thiagomoreira/untappd/UntappdTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@

import java.io.IOException;

import br.com.thiagomoreira.untappd.model.*;
import org.junit.Assert;
import org.junit.Test;

import br.com.thiagomoreira.untappd.model.Beers;
import br.com.thiagomoreira.untappd.model.Brewery;
import br.com.thiagomoreira.untappd.model.User;
import br.com.thiagomoreira.untappd.model.Venue;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okio.Buffer;
Expand Down Expand Up @@ -55,6 +52,19 @@ public void getBrewery() throws IOException {
Assert.assertEquals(breweryId, brewery.getBreweryId());;
}

@Test
public void searchBrewery() throws IOException {
Untappd untappd = new Untappd(null, null, null, setup(
"/get-search-brewery.json", 200), true);

String query = "grist";
BrewerySearch brewerySearch = untappd.brewerySearch(query, null, null);

Assert.assertNotNull(brewerySearch);
Assert.assertEquals(query, brewerySearch.getTerm());

}

@Test
public void getBeersByUsername() throws IOException {
Untappd untappd = new Untappd(null, null, null, setup(
Expand Down
Loading