AreaFiftyLAN / toornament-client

A java api client for Toornament. Currently not under active development.
MIT License
1 stars 1 forks source link

Expand Tournaments Endpoints #3

Closed skamoen closed 7 years ago

skamoen commented 7 years ago

Following Toornament documentation: https://developer.toornament.com/doc/tournaments

The Requests and Responses should be complete, though getters and setters should be added where needed.

BrentonPoke commented 7 years ago

@skamoen In order to get any of this working, doesn't the discipline portion of the API need to work first? The request body reqires a lot of stuff you would need to request via a Discipline before hand. I've been trying to do that but can't get print any disciplines. I'd like it to work similar to this:

ToornamentClient Client = new ToornamentClient(API,id,secret);
        Client.authorize();
        Disciplines discipline = new Disciplines(Client);

            List<Discipline> details = null;
        try {
           details = new ArrayList<>(discipline.getDisciplines());
        } catch (Exception e) {
            System.out.println(e.getCause());
        }
        Iterator<Discipline> iterator = details.iterator();
    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }

But the things that Discipline is supposed to hold don't seem to be exposed, and I'm having trouble exposing them myself in testing.

skamoen commented 7 years ago

You'll need the Discipline endpoint for a feature-complete client, yes. To start, I was just going to use a hardcoded discipline String in my application, but implementing the Discipline endpoint might be good to do first.

Anyway, the discipline endpoint would work simiar to the tournament endpoint

// Create new client (note that authorization is done at initialization
ToornamentClient client = new ToornamentClient(API, id, secret);

// To be built:

// Get a list of all disciplines
List<Discipline> disciplines = client.disciplines().getAllDisciplines();

// Or a single one
String disciplineId = "cod4";
Discipline discipline = client.disciplines().get(disciplineId);

The Discipline class methods would be very similar to the Tournament class, with the getAllDisciplines() something like this:

    public List<Discipline> getAllDiscipline() {
        Request request = client.getRequestBuilder().get().url("https://api.toornament.com/v1/discplines").build();
        String responseBody = client.executeRequest(request).body().string();

        return mapper.readValue(responseBody, mapper.getTypeFactory().constructCollectionType(List.class, Discipline.class));
    }

and add the disciplines() methods to the TournamentClient.class like this:

public Disciplines disciplines() {
        return new Disciplines(this);
    }

You also might need some getters and setters in the model classes...

Does that make any sense?

BrentonPoke commented 7 years ago

@skamoen That's exactly what i've been trying to do, except that I would pass a client object to the Disciplines class so it could make the request rather than putting a disciplines() method in the TournamentClient.class, like this:

Disciplines discipline = new Disciplines(Client);
discipline.getDisciplines(); 

Are you saying the API calls to the endpoints should be made by the TournamentClient.class rather than the Disciplines.class?

skamoen commented 7 years ago

I don't really like that way of initializing, but you're correct that it's effectively the same thing. Did you add getters and setters in the relevant model classes? The code you posted before wouldn't work without a toString() method either. Can you run it in debug mode to check if the values just aren't printed or if they're not stored in the object at all?

edit: I just looked at your fork, the problem might be that there are no setters defined, which might be required by the objectmapper to store the values

BrentonPoke commented 7 years ago

I got it working your way last night. The thing i'm trying to do now is get getDiscipline(id) working the same way getDisciplines() does.

BrentonPoke commented 7 years ago

Ok, I need a sanity check. This should work, but i'm not getting any of the disciplines back, and instead get an exception thrown at the point highlighted in the image. I can create a pull request of this code, but I didn't want to until I got it working in testing. Shouldn't I get a list of all the games Toornament has tournaments for? alt text

public List<Discipline> getDisciplines() throws IOException {
        // TODO: PLACEHOLDER
        Request request = client.getAuthenticatedRequestBuilder().get().url("https://api.toornament.com/v1/disciplines").build();
         String responseBody = client.executeRequest(request).body().string();
        return mapper.readValue(responseBody, mapper.getTypeFactory().constructCollectionType(List.class, Discipline.class));
    }

    public DisciplineDetails getDiscipline(String id) throws IOException {
        // TODO: PLACEHOLDER
        Request request = client.getAuthenticatedRequestBuilder().get().url("https://api.toornament.com/v1/disciplines"+ id ).build();
         String responseBody = client.executeRequest(request).body().string();

        return mapper.readValue(responseBody, mapper.getTypeFactory().constructType(DisciplineDetails.class));
    }
skamoen commented 7 years ago

I checked out your code for a testrun. I'm not sure why you're getting an exception there, as that part is for authentication. Is your api key still valid?

I don't get that error, but I needed to add getters and setters for all fields in the Discipline class. After that, the getAllDisciplines() method works.

Do you want to get help in fixing this yourself or do you want me to implement it for you?

BrentonPoke commented 7 years ago

The key is still valid because I can get tournament stuff with it. That's why I'm so confused.