<commit_before>//
//  RedditAPI.swift
//  Reddit
//
//  Created by <NAME> on 02/03/16.
//  Copyright © 2016 Faber Ventures. All rights reserved.
//

import Foundation
import Moya

enum RedditAPI {

  case AccessToken(code: String, redirectURL: String, clientId: String)
  case RefreshToken(refreshToken: String, clientId: String)
  case SubredditListing(token: String, after: String?)
  case MultiredditListing(token: String)
  case LinkDetails(token: String?, permalink: String)
  case LinkListing(token: String?, subredditName: String, listing: ListingType, after: String?)
  case UserDetails(token: String?, username: String)
  case UserMeDetails(token: String)

}

extension RedditAPI: TargetType {

  var baseURL: NSURL {
    guard let _ = token else {
      return NSURL(string: "https://www.reddit.com")!
    }
    return NSURL(string: "https://oauth.reddit.com")!
  }

  var path: String {
    switch self {
    case .AccessToken, RefreshToken:
      return "/api/v1/access_token"
    case .SubredditListing:
      return "/api/multi/mine"
    case .MultiredditListing:
      return "/subreddits/mine"
    case .LinkListing(_, let subredditName, let listing, _):
      return "/r/\(subredditName)/\(listing.path)"
    case .LinkDetails(_, let permalink):
      return permalink
    case .UserMeDetails(_):
      return "/api/v1/me"
    case .UserDetails(_, let username):
      return "/user/\(username)/about"
    }
  }

  var method: Moya.Method {
    switch self {
    case .AccessToken, .RefreshToken:
      return .POST
    default:
      return .GET
    }
  }

  var parameters: [String: AnyObject]? {
    switch self {
    case .AccessToken(let code, let redirectURL, _):
      return [
        "grant_type": "authorization_code",
        "code": code,
        "redirect_uri": redirectURL]
    case .RefreshToken(let refreshToken, _):
      return [
        "grant_type": "refresh_token",
        "refresh_token": refreshToken
      ]
    case .SubredditListing(_, let after):
      guard let after = after else {
        return nil
      }
      return ["after": after]
    case .LinkListing(_, _, _, let after):
      guard let after = after else {
        return nil
      }
      return ["after": after]
    default:
      return nil
    }
  }

  var sampleData: NSData {
    switch self {
    case .AccessToken, .RefreshToken:
      return JSONReader.readJSONData("AccessToken")
    case .SubredditListing:
      return JSONReader.readJSONData("SubredditListing")
    case .LinkListing:
      return JSONReader.readJSONData("LinkListing")
    case .MultiredditListing:
      return JSONReader.readJSONData("MultiredditListing")
    case .LinkDetails:
      return JSONReader.readJSONData("LinkDetails")
    case .UserDetails, .UserMeDetails:
      return JSONReader.readJSONData("UserDetails")
    }
  }

  var headers: [String: String]? {
    guard let token = token else {
      return nil
    }
    return ["Authorization": "bearer \(token)"]
  }

  var parameterEncoding: ParameterEncoding {<commit_msg>Added DefaultSubredditListing API endpoint for non-logged in Users<commit_after>//
//  RedditAPI.swift
//  Reddit
//
//  Created by <NAME> on 02/03/16.
//  Copyright © 2016 Faber Ventures. All rights reserved.
//

import Foundation
import Moya

enum RedditAPI {

  case AccessToken(code: String, redirectURL: String, clientId: String)
  case RefreshToken(refreshToken: String, clientId: String)
  case SubredditListing(token: String, after: String?)
  case DefaultSubredditListing(after: String?)
  case MultiredditListing(token: String)
  case LinkDetails(token: String?, permalink: String)
  case LinkListing(token: String?, subredditName: String, listing: ListingType, after: String?)
  case UserDetails(token: String?, username: String)
  case UserMeDetails(token: String)

}

extension RedditAPI: TargetType {

  var baseURL: NSURL {
    guard let _ = token else {
      return NSURL(string: "https://www.reddit.com")!
    }
    return NSURL(string: "https://oauth.reddit.com")!
  }

  var path: String {
    switch self {
    case .AccessToken, RefreshToken:
      return "/api/v1/access_token"
    case .MultiredditListing:
      return "/api/multi/mine"
    case .SubredditListing:
      return "/subreddits/mine"
    case .DefaultSubredditListing:
      return "/subreddits/default"
    case .LinkListing(_, let subredditName, let listing, _):
      return "/r/\(subredditName)/\(listing.path)"
    case .LinkDetails(_, let permalink):
      return permalink
    case .UserMeDetails(_):
      return "/api/v1/me"
    case .UserDetails(_, let username):
      return "/user/\(username)/about"
    }
  }

  var method: Moya.Method {
    switch self {
    case .AccessToken, .RefreshToken:
      return .POST
    default:
      return .GET
    }
  }

  var parameters: [String: AnyObject]? {
    switch self {
    case .AccessToken(let code, let redirectURL, _):
      return [
        "grant_type": "authorization_code",
        "code": code,
        "redirect_uri": redirectURL]
    case .RefreshToken(let refreshToken, _):
      return [
        "grant_type": "refresh_token",
        "refresh_token": refreshToken
      ]
    case .SubredditListing(_, let after):
      guard let after = after else {
        return nil
      }
      return ["after": after]
    case .DefaultSubredditListing(let after):
      guard let after = after else {
        return nil
      }
      return ["after": after]
    case .LinkListing(_, _, _, let after):
      guard let after = after else {
        return nil
      }
      return ["after": after]
    default:
      return nil
    }
  }

  var sampleData: NSData {
    switch self {
    case .AccessToken, .RefreshToken:
      return JSONReader.readJSONData("AccessToken")
    case .SubredditListing:
      return JSONReader.readJSONData("SubredditListing")
    case .DefaultSubredditListing:
      return JSONReader.readJSONData("SubredditListing")
    case .LinkListing:
      return JSONReader.readJSONData("LinkListing")
    case .MultiredditListing:
      return JSONReader.readJSONData("MultiredditListing")
    case .LinkDetails:
      return JSONReader.readJSONData("LinkDetails")
    case .UserDetails, .UserMeDetails:
      return JSONReader.readJSONData("UserDetails")
    }
  }

  var headers: [String: String]? {
    guard let token = token else {
      return nil
    }
    return ["Authorization": "bearer \(token)"]
  }

  var parameterEncoding: ParameterEncoding {
{'fraction_non_alphanumeric': 0.10935563816604708, 'fraction_numerical': 0.0037174721189591076, 'mean_word_length': 2.889759036144578, 'pattern_counts': {'":': 17, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4532632', 'n_tokens_mistral': 1983, 'n_tokens_neox': 1964, 'n_words': 513}