<commit_before>require 'faraday' # HTTP Client
require 'logger'
require 'faraday_middleware'
require 'spaceship/ui'
require 'spaceship/helper/plist_middleware'
require 'spaceship/helper/net_http_generic_request'



module Spaceship
  class Client
    PROTOCOL_VERSION = "QH65B2"

    attr_reader :client
    attr_accessor :cookie

    # The logger in which all requests are logged
    # /tmp/spaceship.log by default
    attr_accessor :logger

    class InvalidUserCredentialsError < StandardError; end
    class UnexpectedResponse < StandardError; end

    # Authenticates with Apple's web services. This method has to be called once
    # to generate a valid session. The session will automatically be used from then
    # on.
    # 
    # This method will automatically use the username from the Appfile (if available)
    # and fetch the password from the Keychain (if available)
    # 
    # @param user (String) (optional): The username (usually the email address)
    # @param password (String) (optional): The password
    # 
    # @raise InvalidUserCredentialsError: raised if authentication failed
    # 
    # @return (Spaceship::Client) The client the login method was called for
    def self.login(user = nil, password = nil)
      instance = self.new
      if instance.login(user, password)
        instance
      else
        raise InvalidUserCredentialsError.new
      end
    end

    def initialize
      @client = Faraday.new("https://developer.apple.com/services-account/#{PROTOCOL_VERSION}/") do |c|
        c.response :json, :content_type => /\bjson$/
        c.response :xml, :content_type => /\bxml$/
        c.response :plist, :content_type => /\bplist$/
        c.adapter Faraday.default_adapter

        if ENV['DEBUG']
          # for debugging only
          # This enables tracking of networking requests using Charles Web Proxy
          require 'openssl'
          OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
          c.response :logger
          c.proxy "https://127.0.0.1:8888"
        end
      end
    end

    def api_key
      page = @client.get("https://developer.apple.com/devcenter/ios/index.action").body
      if page =~ %r{<a href="https://idmsa.apple.com/IDMSWebAuth/login\?.*appIdKey=(\h+)}
        return $1
      end<commit_msg>Added logging of api key request<commit_after>require 'faraday' # HTTP Client
require 'logger'
require 'faraday_middleware'
require 'spaceship/ui'
require 'spaceship/helper/plist_middleware'
require 'spaceship/helper/net_http_generic_request'



module Spaceship
  class Client
    PROTOCOL_VERSION = "QH65B2"

    attr_reader :client
    attr_accessor :cookie

    # The logger in which all requests are logged
    # /tmp/spaceship.log by default
    attr_accessor :logger

    class InvalidUserCredentialsError < StandardError; end
    class UnexpectedResponse < StandardError; end

    # Authenticates with Apple's web services. This method has to be called once
    # to generate a valid session. The session will automatically be used from then
    # on.
    # 
    # This method will automatically use the username from the Appfile (if available)
    # and fetch the password from the Keychain (if available)
    # 
    # @param user (String) (optional): The username (usually the email address)
    # @param password (String) (optional): The password
    # 
    # @raise InvalidUserCredentialsError: raised if authentication failed
    # 
    # @return (Spaceship::Client) The client the login method was called for
    def self.login(user = nil, password = nil)
      instance = self.new
      if instance.login(user, password)
        instance
      else
        raise InvalidUserCredentialsError.new
      end
    end

    def initialize
      @client = Faraday.new("https://developer.apple.com/services-account/#{PROTOCOL_VERSION}/") do |c|
        c.response :json, :content_type => /\bjson$/
        c.response :xml, :content_type => /\bxml$/
        c.response :plist, :content_type => /\bplist$/
        c.adapter Faraday.default_adapter

        if ENV['DEBUG']
          # for debugging only
          # This enables tracking of networking requests using Charles Web Proxy
          require 'openssl'
          OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
          c.response :logger
          c.proxy "https://127.0.0.1:8888"
        end
      end
    end

    def api_key
      landing_url = "https://developer.apple.com/devcenter/ios/index.action"
      logger.info("GET: " + landing_url)
      page = @client.get(landing_url).body
      if page =~ %r{<a href="https://idmsa.apple.com/IDMSWebAuth/login\?.*appIdKey=(\h+)}
        return $1
      end
{'fraction_non_alphanumeric': 0.09387055293613374, 'fraction_numerical': 0.006000857265323617, 'mean_word_length': 2.8254098360655737, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 2, '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': '7185283', 'n_tokens_mistral': 1394, 'n_tokens_neox': 1318, 'n_words': 402}